Course Notes (Complete)
Course Notes (Complete)
Lecture Notes
Verena Wolf1
1
[Link]@[Link]
ii
Contents
1 Data Description 1
1.1 Important Terms . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Data description . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2.1 Frequency . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2.2 Measures of Location . . . . . . . . . . . . . . . . . . 3
1.2.3 Measures of Dispersion . . . . . . . . . . . . . . . . . . 5
1.2.4 Measures of Shape . . . . . . . . . . . . . . . . . . . . 6
1.2.5 Standardization of Data . . . . . . . . . . . . . . . . . 7
iii
iv CONTENTS
7 Parameter Estimation 69
7.1 Method of Moments . . . . . . . . . . . . . . . . . . . . . . . 71
7.2 Maximum Likelihood Estimation . . . . . . . . . . . . . . . . 74
7.2.1 Variance of MLE (optional content) . . . . . . . . . . 79
7.3 Bayesian Inference . . . . . . . . . . . . . . . . . . . . . . . . 82
7.3.1 Conjugate families of distributions . . . . . . . . . . . 84
7.3.2 Bayesian point-estimators . . . . . . . . . . . . . . . . 86
8 Statistical Testing 87
8.1 Level α tests: general approach . . . . . . . . . . . . . . . . . 88
8.2 Standard Normal Null Distribution (Z-test) . . . . . . . . . . 90
8.3 T-tests for Unknown σ . . . . . . . . . . . . . . . . . . . . . . 93
8.4 p-Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
8.5 Chi-square Tests . . . . . . . . . . . . . . . . . . . . . . . . . 97
8.5.1 Estimated variance . . . . . . . . . . . . . . . . . . . . 97
8.5.2 Observed Counts . . . . . . . . . . . . . . . . . . . . . 98
8.5.3 Testing independence . . . . . . . . . . . . . . . . . . 101
Preface
v
vi CONTENTS
Chapter 1
Data Description
1
1.2. DATA DESCRIPTION
Quantitative data
Attributes may refer to some quantity of something and have numerical
values such as the attribute height, for instance. Quantitative data may
have a discrete range (e.g. subset of N) or a continuous range (e.g. subset
of R≥0 ). Attributes such as height, weight, length, and time are continuous
data while counts of days, successes, etc are discrete data.
Qualtitative data
Qualitative data are any type of data that are not numerical. Examples
are name, gender, country, social security number, etc. If a qualitative
attribute has an order it is called ordinal . For instance, assume that we
consider a person’s educational experience (with values such as elementary
school graduate, high school graduate, and college graduate). These also
can be ordered and we can assign numbers accordingly such as elementary
school (1), high school (2), and college (3). Even though we can order these
from lowest to highest, the spacing between the values may not be the same
(e.g. the gap between elementary school and high school may be larger
than the gap between high school (2), and college). Another example for an
ordinal attribute is shoe size where there is a clear order but size 42 is not
twice as large as size 21.
If attribute values do not have an implied ordering, the attribute is called
nominal . For instance, the attribute color is a nominal one because we want
to distinguish blue, red, yellow, etc but do not order the colors in a certain
way.
fj := hj /n.
Hence
k
X k
X
hj = n, fj = 1.
j=1 j=1
2
1.2. DATA DESCRIPTION
Later, we will discuss frequency tables and how to generate them using
Python.
Assume that we have a sample set of size n = 3 with the attribute ’color’
taking values in A = {red,blue,green} and the samples are x1 = blue, x2 =
red, x3 = red. Then, the absolute frequencies are
hblue = 1
hred = 2
hgreen = 0
fblue = 1/3
fred = 2/3
fgreen = 0
Note that the mean is a very natural measure, but it is sensitive to extreme
values. In the case of highly skewed data, the mean is not a good measure
of location of the data.
The (sample) median is a value that splits the data into two parts of equal
size. To compute the median, we sort the data x1 , x2 , . . . , xn in increasing
order. Given the sorted list x(1) , x(2) , . . . , x(n) and define
(
x( n+1 ) : n is uneven,
xmed := 1
2
3
1.2. DATA DESCRIPTION
Example 3: Mode
The mode of the data set 1,2,2,3,4,4,5,5,5,5,6,7 is 5. We illustrate the
mode together with mean and median in Figure 1.2.
In general, the mode and the median are less sensitive to outliers than the
sample mean. If the last value in our example data set would be 70 (instead
of 7), the mode and the median would be unchanged, while the sample mean
increases to approximately 9.33.
Figure 1.2: Plot of the data including mode, median, and mean.
4
1.2. DATA DESCRIPTION
Example 5: Range
The range of the data set 1,2,2,3,4,4,5,5,5,5,6,7 is 7-1=6. We illustrate
the range together with mean and standard deviation in Figure 1.3.
The Python code for the sample mean, sample variance and range for our
example data set can be found below. We give a naive implementation for
the mean and variance, as well as the built-in functions from numpy. We
always import numpy as np. Note that the variance function from numpy
has an optional parameter ddof, which we choose as 1 at this point. Later,
we will discuss this issue in more detail.
5
1.2. DATA DESCRIPTION
Later, we will also discuss bounds such as Chebychev’s Rule that consider
how much of the data will fall past a certain distance (e.g. k standard
deviations) from the mean.
Figure 1.3: Plot of the data including mean, sample standard deviation, and
range.
The distribution of the data may be symmetric, skewed to the right or left.
In Figure1.4 we show the left- and right-skewed case (plots taken from [2]).
For the sample skewness, different definitions exist, for reasons that will
become clear when we discuss important properties of estimators such as
unbiasedness. For now, we only consider the most convenient formula to
6
1.2. DATA DESCRIPTION
The skewness can be any value, −∞ < g1 < ∞ and its sign indicates the
direction of skewness (negative values correspond to skewness to the left and
positive values to skewness to the right). If g1 ≈ 0, then the distribution is
(nearly) symmetric.
Figure 1.5: Histogram plot of data before (left) and after standardization
(right).
7
1.2. DATA DESCRIPTION
8
Chapter 2
Probabilities and
Combinatorics
“Was ein Punkt, ein rechter Winkel, ein Kreis ist, weiß ich schon
vor der ersten Geometriestunde, ich kann es nur noch nicht präzisieren.
Ebenso weiß ich schon, was Wahrscheinlichkeit ist, ehe ich es
definiert habe.“ (Hans Freudenthal)
2.1 Probabilities
Let us consider chance experiments with a countable number of possible
outcomes ω1 , ω2 , . . .. The set Ω = {ω1 , ω2 , . . .} of all outcomes is called the
9
2.1. PROBABILITIES
sample space. Subsets of Ω are called events and by 2Ω we denote the set
of all events.
10
2.1. PROBABILITIES
P (A ∩ B)
P (A|B) :=
P (B)
11
2.1. PROBABILITIES
For the probability of getting lung cancer under the condition of not being
a smoker, we get
P (A∩B̄) P (B̄|A)P (A) (1−0.9)·0.00036
P (A|B̄) = P (B)
= P (B)
= 0.75 = 0.000048.
Thus, the chance of getting lung cancer is around 27 times higher for
smokers compared to non-smokers.
Multiplication rule
Multiplication rule
Let A1 , A2 , . . . , An be events with P (A1 ∩ A2 ∩ . . . ∩ An−1 ) > 0. Then the
following multiplication rule holds:
P (A1 ∩ A2 ∩ . . . ∩ An )
= P (A1 ) · P (A2 |A1 ) · P (A3 |A1 ∩ A2 ) · · · · · P (An |A1 ∩ A2 ∩ . . . ∩ An−1 )
Suppose you have an urn with 3 red, 2 blue and 1 green ball. You draw
three times without placing the drawn balls back into the urn. The prob-
ability of drawing blue, green, red (in that order) is
2 1 3
· · .
6 5 4
Note that we will discuss scenarios like this in more detail later.
A1 Ω
Law of total probability A2
Assume that we have a finite or countably infi- B
nite number of events A1 , A2 , . . . that are pairwise
disjoint. Assume further Ω = A1 ∪ A2 ∪ . . . and A3
A4
12
Figure 2.2: The law of
total probability.
2.1. PROBABILITIES
In a city on 70 from 100 days the weather is good (G) and on 30 from
100 days the weather is bad (Ḡ). The local meteorologist can predict good
weather with 90% accuracy and bad weather with 60% accuracy. According
to the law of total probability the forecast is correct with a probability of
Thus, for the above problem of computing P (Ai |B) for pairwise disjoint
events Ai , this gives
P (Ai |B) =
P (B∩Ai ) P (B|Ai )·P (Ai )
P (B) = P (B) = PP (B|A i )·P (Ai )
.
i P (B|Ai )·P (Ai )
• B0 : receive 0,
• B1 : receive 1,
Then, P (B1 |A0 ) = P (B0 |A1 ) = p and P (B1 |A1 ) = P (B0 |A0 ) = 1 − p.
We calculate
P (B1 ) = P (B1 |A0 ) · P (A0 ) + P (B1 |A1 ) · P (A1 ) = p · π0 + (1 − p) · π1 ,
P (B0 ) = P (B0 |A0 ) · P (A0 ) + P (B0 |A1 ) · P (A1 ) = (1 − p) · π0 + p · π1 .
and the receiver can determine the probability that the transmission was
correct as
P (B1 |A1 )·P (A1 ) (1−p)·π1
P (A1 |B1 ) = P (B1 ) = p·π0 +(1−p)·π1
P (B0 |A0 )·P (A0 ) (1−p)·π0
P (A0 |B0 ) = P (B0 ) = (1−p)·π0 +p·π1
There are two urns, one containing 7 red and 3 blue balls, the other
containing 3 red and 7 blue balls. We flip a fair coin to determine, from
which urn we draw 12 balls with replacement. As a result, we get 8 times
a red and 4 times a blue ball. What is the probability that it was the first
urn with predominantly red balls?
Let U1 (U2 ) be the event of selecting the first (second) urn, respectively.
Further, let A be the event of getting 8 times a red and 4 times a blue
ball. Some concepts from combinatorics are necessary to determine the
probabilities P (A|U1 ) and P (A|U2 ). They will be discussed in the next
14
2.1. PROBABILITIES
P (A|U1 ) ≈ 0.231
P (A|U2 ) ≈ 0.0078
Hence, given A it is about 29 times more likely that we drew from urn 1.
We get P (U1 |A) by exploiting P (U1 |A)+P (U2 |A) = 1 and the above ratio:
P (U1 |A)
P (U1 |A) = 1 − P (U2 |A) = 1 − 29.642
⇐⇒ P (U1 |A) ≈ 0.967
Thus, with nearly 97%, we drew from urn 1, which is intuitive since we
got many more red balls than blue ones.
P (A ∩ B̄) 1/4
P (A|B̄) = = = 1/2 = P (A).
P (B̄) 2/4
Definition 3: Independence
Let 0 < P (B) < 1. The event A is called independent of B if
P (A|B) = P (A|B̄).
15
2.1. PROBABILITIES
Assume now that Ω = {1, 2, . . . , 6}, A = {5, 6}, and B = {2, 4, 6}. Then
P (A∩B) P ({6}) 1
P (A|B) = P (B) = P ({2,4,6}) =2· 6 = 1/3
and
P (A∩B̄) P ({5}) 1
P (A|B̄) = P (B)
= P ({1,3,5}) =2· 6 = 1/3,
Note that one can use the alternative (and equivalent) condition
P (A ∩ B) = P (A) · P (B)
From (2) we see that independence is a symmetric property. So, if P (A) > 0,
further equivalent conditions can be derived where the roles of A and B are
reversed. In other words: If the events A and B are independent then
• A and B̄ are independent,
16
2.1. PROBABILITIES
Remark:
In information theory, the negative log probability I(A) := − log P (A) of
an event A is interpreted as the information content or level of surprise
(the smaller P (A), the larger − log P (A)). For two independent events, the
information content of the combined event simply adds up since
I(A ∩ B) = − log P (A ∩ B)
= − log(P (A) · P (B))
= − log P (A) − log P (B)
= I(A) + I(B)
Later, we will take a closer look at information contents, entropy and other
concepts from information theory.
Question:
How can the above definition be extended to n events?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
R R R R R B B B B B R R R R B B B B
36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19
Now, let A and B denote the event that a spin of the wheel yields a red
number and an even number, respectively. In addition, let C be the event
that the number is smaller than 19. We check whether A, B, and C
are pairwise independent. We have P (A) = P (B) = 1/2 since half of
the numbers are red and also, half of the numbers are even. Moreover,
P (C) = 1/2 since the numbers from 1 to 18 are the first half of all the
numbers. From the above table, we see that
17
2.2. COMBINATORICS
and thus
P (A ∩ B) = 9/36 = 1/4 = 1/2 · 1/2 = P (A) · P (B),
P (A ∩ C) = 9/36 = 1/4 = 1/2 · 1/2 = P (A) · P (C),
P (B ∩ C) = 9/36 = 1/4 = 1/2 · 1/2 = P (B) · P (C),
Question:
Does pairwise independence follow from mutual independence?
2.2 Combinatorics
To determine the probability of certain events, combinatorial considerations
are often useful. We first focus on the elementary events, that is, all sin-
gleton sets {ω} where ω is an outcome. The union of these events forms
Ω and in many chance experiments, all elementary events have the same
probability. This is the case, for instance, in dice games, lottery, and other
18
2.2. COMBINATORICS
nA |outcomes favorable to A|
P (A) = = .
N |all possible outcomes|
Many chance experiments can be mapped to one of the following urn prob-
lems, where we have an urn containing n distinguishable balls. We draw a
ball from the urn k times. We can do this in two ways: we might replace the
ball drawn each time before drawing the next ball, or we might not replace
the ball (in which case k cannot be bigger than n). We may also consider
the order of balls drawn to matter, or we may consider the draws to be
unordered.
19
2.2. COMBINATORICS
Question:
Suppose we have a neural network with k layers, where each layer has n
nodes. How many ways are there to traverse the network if all layers are
fully connected?
Question:
In tic tac toe there are 9 fields, which can either be empty or filled with an
X or an O. How many possible field configurations are there if we ignore
the rules of the game (i.e., we count all assignments of the three symbols to
the nine fields, regardless of whether they could occur in an actual game)?
We draw without replacement two times from an urn containing three balls
- red, green, and blue. There are 3 × 2 = 6 possible ordered outcomes: RG,
RB, GR, GB, BR, BG.
20
2.2. COMBINATORICS
drawing k balls without replacement ignoring order from an urn with n balls
is
n(n − 1)(n − 2) . . . (n − k + 1) n! n
= = .
k! (n − k)!k! k
This is called “n choose k”. Note that
n n n n
= and = = 1.
k n−k 0 n
We draw without replacement two times from an urn containing three balls
- red, green, and blue. There are (3 × 2)/(1 × 2) = 3 possible unordered
outcomes: {R,G}, {R,B}, {G,B}.
21
2.2. COMBINATORICS
possible outcomes.
This is the same as the number of ways of choosing places for the k Os out
of the k + n − 1 positions, which is k + n − 1 choose k.
22
2.2. COMBINATORICS
nA |outcomes favorable to A|
P (A) = = .
N |all possible outcomes|
Both, the number of outcomes favorable to A and the number of all possible
outcomes may be computed using one of the formulas above.
23
2.2. COMBINATORICS
We assume equal probability for each day of the year. Hence, P (Ac ) =
#(Ac )/#(S). The number of outcomes in the sample space is #(S) = 365n ,
the same as the number of ways of drawing n balls with replacement from an
urn with 365 balls, paying attention to the order. Using the multiplication
principle, the number of outcomes with no birthdays on the same day is
#(Ac ) = 365 · 364 . . . (365 − n + 1), which is the same as the number of
ways of drawing n balls from an urn with 365 balls without replacement,
paying attention to the order. We use these numbers to compute P (A) =
1 − #(Ac )/#(S). For example, if n = 5, then P (A) = 2.7%.
The following Python code computes the probabilties of the birthday problem
(while preventing overflow problems due to large integers).
1 p l t . p l o t ( [ 1 − np . p r o d ( [ ( 3 6 5 − i ) / 365
2 f o r i in range (n) ] )
3 f o r n in range (100) ] )
Indistinguishable Balls
So far, we assumed that the urn contains n distinguishable balls. But what
if the urn contains r red balls and n − r blue balls and balls of the same
color cannot be distinguished?
Question:
Consider the case of drawing multiple balls (possibly more than one ball
of the same color) with replacement, where balls with the same color can
not be distinguished. Assume that we do not care for the order in which
the balls are drawn. Does the number of possible outcomes differ from the
case with only one ball of each color? Why (not)?
Is your claim also true for the probabilities of the outcomes?
Without replacement things are a little bit more complicated. However, for
small examples it is possible to manually count the possible outcomes:
24
2.2. COMBINATORICS
If we now consider more than two different colors or care for the order,
things get even more complicated. Luckily, if we are only interested in
the probability of the outcomes (and not in the total number of possible
outcomes), we can use probability trees to easily calculate the probabilities.
5 4
9 9
R B
1 1 5 3
2 2 8 8
R B R B
3 4 4 3 4 3 5 2
7 7 7 7 7 7 7 7
R B R B R B R B
To get the probability of a certain event, we traverse the tree along the
path that corresponds to the event (from top to bottom) and multiply the
respective probabilities. For example, for the event RBR we get 59 · 21 · 47 .
If we do not care for the order, we go through all suitable paths and add
the resulting probabilities. For {R, R, B}, i.e., two red and one blue ball in
arbitrary order, we have the paths RRB, RBR and BRR which yield the
total probability 59 · 21 · 47 + 59 · 21 · 47 + 49 · 58 · 47 .
Note that if we consider the case with replacement, every edge that leads
to R has the probability 59 and every edge that leads to B the probability 49 .
25
2.2. COMBINATORICS
26
Chapter 3
27
3.1. DISCRETE RANDOM VARIABLES AND PROBABILITY
DISTRIBUTIONS
28
3.1. DISCRETE RANDOM VARIABLES AND PROBABILITY
DISTRIBUTIONS
0.2
1
0.15 0.8
0.6
0.1
0.4
0.05
0.2
0
2 3 4 5 6 7 8 9 10 11 12 0
2 3 4 5 6 7 8 9 10 11 12
• “X < a” . . .
29
3.2. COMBINING AND TRANSFORMING RANDOM VARIABLES
(! !2 )
ω1 , ω X(Ω)
Ω
!1
ω
Z(Ω)
(ω1 , ω2 ) ω1
!1 + ω
ω !2
!2
ω ω1 +ω2
ω2
Y (Ω)
Moreover,
X X X
P (Z = z) = P ({ω}) = P (Ω) = 1.
z∈Z(Ω) z∈Z(Ω) ω∈Ω,X(ω)+Y (ω)=z
30
3.2. COMBINING AND TRANSFORMING RANDOM VARIABLES
Remark:
In the sequel, we often work with combinations of only two random variables.
However, all these properties and definitions carry over in a straightforward
way to more than two, i.e. a finite number of random variables. Exceptions
or additional concepts for the case of more than two random variables are
explicitly mentioned (e.g. for independence we have pairwise and mutual
independence).
General Transformations of X
fY (y) = P (Y = y) = P (g(X) = y)
= P ({ω ∈ Ω | g(X(ω)) = y})
= P ({ω ∈ Ω | X(ω) = g −1 (y)})
= P (X = g −1 (y))
= fX (g −1 (y))
X −1
2 · X, , eX
2
31
3.2. COMBINING AND TRANSFORMING RANDOM VARIABLES
Hence, for a concrete realization D(ω), the volume can be directly deter-
mined by the above transformation.
Assume that X and Y are defined as in Example 30. Then X and Y are
32
3.3. EXPECTATION AND VARIANCE
P (X = a ∧ Y = b) = P (X −1 ({a}) ∩ Y −1 ({b}))
= P ({(ω1 , ω2 ) ∈ Ω | ω1 = a}
∩{(ω1 , ω2 ) ∈ Ω | ω2 = b})
= P ({(a, b)}) = 1/36
and
P (X = a) · P (Y = b) = P (X −1 ({a})) · P (Y −1 ({b}))
= P ({(ω1 , ω2 ) ∈ Ω | ω1 = a})
·P ({(ω1 , ω2 ) ∈ Ω | ω2 = b})
= 6/36 · 6/36 = 1/36.
We remark that the above definition can be extended to more than two
random variables by formulating conditions for mutual and pairwise inde-
pendence.
If X1 , . . . , Xn are (real-valued) random variables on the same probability
space, then we call X1 , . . . , Xn (mutually) independent iff for all a1 , a2 , . . . , an ∈
R
P (X1 = a1 , . . . , Xn = an ) = P (X1 = a1 ) · . . . · P (Xn = an ).
We call the random variables X1 , . . . , Xn (pairwise) independent iff all pairs
Xi , Xj , i 6= j are independent in the sense of Definition 8.
In the sequel, we will often consider random variables that are independent
and identically distributed (i.i.d.). This means that the considered random
variables all follow the same probability distribution and are (mutually)
independent.
respectively. Note that the sum might not converge, in which case the
expectation/variance does not exist. It is common to write E(X) = ∞ or
V (X) p= ∞ in such a case. The standard deviation σX of X is given by
σX = V (X). Note that V (X) (and σX ) are always non-negative.
33
3.3. EXPECTATION AND VARIANCE
Then one can compute that E(IA ) = 1 · P (A) + 0 · P (Ā) = P (A) and
V (IA ) = P (A)(1 − P (A)).
Example 34:
We consider first a standard fair die with 6 sides. For such a die the
P
6
expectation is given by E(X) = k · 16 = 3.5 and the variance is V (X) =
k=1
P
6
2 1
(k − 3.5) · 6 = 2.9. Now let us consider the second fair die where
k=1
number on sides 3 and 4 are substituted by 1 and 6 (so the die has sides
1, 2, 1, 6, 5, 6). For this die the expectation is 3.5 as before but the variance
is much larger, V (Y ) = 4.9. Distribution of both dice are shown on
the figure below (blue colors) together with the corresponding standard
deviations (green colors give expectation +/− standard deviation).
34
3.3. EXPECTATION AND VARIANCE
E(X) E(Y )
1 2 3 4 5 6 1 2 3 4 5 6
2. E(a · X + b) = a · E(X) + b,
Proof. (1.)
X
E(X) = x · P (X = x) (definition of E(X))
x∈X(Ω)
X
= x · P ({ω | X(ω) = x}) (definition of P (X = x))
x∈X(Ω)
X X
= x· P ({ω}) (2nd axiom of Def. 1)
x∈X(Ω) ω∈Ω∧X(ω)=x
X
= X(ω)P ({ω}) (X is a function)
ω∈Ω
Remark:
We saw that expectation is linear. However, in general expectation does not
factor nicely E(XY ) 6= E(X)E(Y )!
35
3.3. EXPECTATION AND VARIANCE
1. V (a · X + b) = a2 · V (X) for a, b ∈ R,
For more than two independent random variables, we generalize the above
properties of the expectation and variance, we have: If X1 , . . . , Xn are (mu-
tually) independent, then
and
V (X1 + . . . + Xn ) = V (X1 ) + . . . + V (Xn ).
In the same way, we could compute the mean of X given Y = y (just switch
the roles of X and Y ).
Consider the rolling of two 6-sided fair dice D1 and D2 and two random
variables X and Y where X = value of D1 + D2 and Y = value of D2 .
We compute
P
E[X | Y = 6] = x x · P (X = x | Y = 6)
1
= 6 (7 + 8 + . . . + 12) = 57/6 = 9.5.
Note that the properties of the expectation carry over to conditional expec-
tations as we are only considering a different probability function (namely,
the conditional probability, which is also a probability in the sense of Def. 1).
36
3.4. HIGHER ORDER MOMENTS, COVARIANCE AND
CORRELATION
(Again the sum might not converge, in which case E(g(X)) does not exist.)
Note that this formula can also be applied if g is not a one-to-one function,
e.g. if g(x) = x2 . In the case g(x) = xi , we call E(X i ) the i-th moment of
X. Note that the moments of a distribution are related to its skewness and
its kurtosis. The former characterizes the degree of asymmetry while the
latter characterizes the flatness or peakedness of the distribution.
37
3.4. HIGHER ORDER MOMENTS, COVARIANCE AND
CORRELATION
38
3.4. HIGHER ORDER MOMENTS, COVARIANCE AND
CORRELATION
tX (tX)2 (tX)3
MX (t) = E[etX ] = E 1 + + + + ... , t ∈ R.
1! 2! 3!
" ∞
# ∞
X X k tk X tk
tX
MX (t) = E[e ]=E = E[X k ]
k! k!
k=0 k=0
dk MX (t)
E[X k ] =
dtk t=0
X
MX (t) = P (X = x) · etx .
x∈X(Ω)
39
3.4. HIGHER ORDER MOMENTS, COVARIANCE AND
CORRELATION
Proof.
MX (t) = E[etX ]
= P (X = 0) · et·0 + P (X = 1) · et·1
= (1 − p)e0 + pet
= 1 − p + pet
Next, we consider
40
3.5. IMPORTANT DISCRETE PROBABILITY DISTRIBUTIONS
distributions have the same moment generating function, then they have
the same distribution, i.e. MX = MY implies PX = PY .
X ∼ Bernoulli(p).
where the first factor counts all possible orderings for k successes
among n trials, the second one gives the probability that in the in-
dependent trials we have k successes and the last factor describes the
probability of (the remaining) n−k failures. Note that one can express
X as the sum of n independent Bernoulli variables X1 , . . . , Xn :
X = X1 + . . . + Xn .
41
3.5. IMPORTANT DISCRETE PROBABILITY DISTRIBUTIONS
and variance V (X) = µ (without proof). Please use the Wikipedia link
to view the plot of the distribution and learn more about the Poisson
distribution.
Note that the Poisson distribution is the limit of the binomial distribution,
when n is large and p is small. This can be shown as follows:
Let µ = np. Starting from a binomial distribution we then get
n k
P (X = k) = p (1 − p)n−k
k
n · (n − 1) · . . . · (n − k + 1) µ k µ n−k
= 1−
k! n n
µ n
n · (n − 1) · . . . · (n − k + 1) µk 1 − n
=
nk k! 1 − µ k
n
µk −µ
≈ e ,
k!
where we used the following limits for fixed k as n → ∞ (with µ = np held
k n
constant): n·(n−1)·...·(n−k+1)
nk
→ 1, 1 − nµ → 1 and 1 − nµ → e−µ .
Suppose a chip is defective in 10% of the time. You have 10 chips and
the number of defective chips is described by the RVs Y or X. We would
like to know the probability that no more than 1 chips are defective. For
the binomial distribution Y ∼ binomial(10, 0.1) we get
P (Y ≤ 1) = P (Y = 0) + P (Y = 1)
10 10
= 0.10 0.910 + 0.11 0.99 ≈ 0.7361.
0 1
42
3.5. IMPORTANT DISCRETE PROBABILITY DISTRIBUTIONS
P (X ≤ 1) = P (X = 0) + P (X = 1)
10 11
= e−1 + e−1 ≈ 0.7358.
0! 1!
Note that even for a moderate size of n = 10 the results are already very
similar.
43
3.5. IMPORTANT DISCRETE PROBABILITY DISTRIBUTIONS
44
Chapter 4
Continuous Random
Variables
So far, we considered only random variables that take discrete values such
as 0, 1, 2, . . .. However, many chance experiments can only be described by
means of continuous random variables. For instance, consider a dartboard
with one foot in radius. The experiment consists of throwing a dart at the
board and the outcome is the point at which it hits. Then, any point in
S = {(x, y) ∈ R2 : x2 + y 2 ≤ 1}
4.1 σ-algebras
Consider a chance experiment where the sample space Ω contains uncount-
ably many elements. In this case, assigning probabilities to all elements in
45
4.1. σ-ALGEBRAS
2. A ∈ F implies Ā ∈ F.
A1 ∪ A2 ∪ . . . ∈ F.
σ(E) = {{2, 6}, {5, 6}, {1, 3, 4, 5}, {1, 2, 3, 4}, {1, 2, 3, 4, 5}, {6}, . . .}.
E = {(a, b] : a, b ∈ R, a ≤ b}.
1
From the two conditions in Def. 1, one can easily derive that P (Ā) = 1 − P (A).
46
4.1. σ-ALGEBRAS
The σ-algebra B := σ(E) is called the Borel algebra on the reals. It contains
all subsets, called Borel sets, of 2R that can be obtained from E by countable
union and complement operations. Note that also intervals of the form
(a, b) or [a, b] are Borel sets. A similar construction is possible for Ω = Rn .
Intuitively, the Borel sets are those sets, for which we can assign a “volume”,
“area” or “size”. Subsets of Rn that are not Borel sets are only of theoretical
interest since for practical applications they are not of importance.
We are now able to give a more general definition of a probability space (i.e.
also for sample sets with uncountably many elements).
• P (Ω) = 1 and,
47
4.2. CONTINUOUS RANDOM VARIABLES
The above definition ensures that if we want to know the probability that X
is in some Borel set A, we can consider the inverse image of A with respect
to X, for which we know its probability.
Clearly, we can define a probability measure PX : B → [0, 1] by setting
PX (A) := P ({ω | X(ω) ∈ A}) = P (X −1 (A)) and use similar notations as in
the discrete case (e.g. P (a < X ≤ b)).
Definition 12: Cumulative Probability Distribution
Let X be a real-valued random variable on (Ω, F, P ). The function F :
R → [0, 1] with x 7→ F (x) := P (X ≤ x) is called the cumulative probability
distribution (CDF) of X.
Clearly, Z ∞
f (y) dy = 1
−∞
48
4.2. CONTINUOUS RANDOM VARIABLES
f (y) F (x)
1
b−a
y x
a b a b
for x ∈ [a, b], F (x) = 0 for x < a, and F (x) = 1 for x > b. Figure 4.2
shows a plot of the functions f and F .
F (x) f (x)
domain R R
codomain [0, 1] R≥0
monotonicity increasing not necessarily
Rx
relation = −∞ 1 f (y)dy = dFdx(x)
49
4.3. IMPORTANT CONTINUOUS DISTRIBUTIONS
Note that these integrals may not exist, in which case the expectation/vari-
ance is undefined.
What from the discrete case carries directly over to the continuous
case?
Many results for discrete random variables carry over to the continuous
setting. For instance, properties of the expectation and variance (e.g. E(X +
Y ) = E(X) + E(Y )) or results concerning the combination/transformation
of random variables, joint distributions, stochastic independence, etc. Also,
we define conditional expectation, covariance, correlation, and higher-order
moments for continuous random variables in an equivalent way. All results
of the previous sections carry over to the continuous case (as long as the
corresponding integrals exist). We do not repeat these definitions here as
the only things that change compared to the discrete case are that we replace
the sum by an integral and the probability of a value x by the density f (x).
Uniform Distribution
We already described the uniform distribution in the examples above. In
summary we get the following properties if X is uniformly distributed on
the interval (a, b) (denoted by X ∼ U (a, b)):
• Its density is constant on the interval (a, b), that is, f (x) = 1/(b − a)
for x ∈ (a, b) and f (x) = 0 otherwise.
50
4.3. IMPORTANT CONTINUOUS DISTRIBUTIONS
Exponential Distribution
Let λ > 0. We say that a continuous random variable X is exponentially
distributed with parameter λ (denoted by X ∼ Exp(λ)) if the density of X
is such that, for t ∈ R,
(
λ · e−λt if t ≥ 0,
f (t) =
0 otherwise.
The cumulative probability distribution of X is then given by
( Rx Rx −λt dt = 1 − e−λx
−∞ f (t) dt = 0 λ · e if x ≥ 0,
F (x) =
0 otherwise.
The exponential distribution is often used to describe waiting times or in-
terarrival times since in many real-world systems where a sequence of rare
events plays an important role, the assumption that the time between these
events is exponentially distributed is used. For instance, the time of ra-
dioactive decay is exponentially distributed. This is related to the fact that
these events are assumed to occur spontaneously and that the exponential
distribution has the so-called memoryless property:
Assume that the random variable X describes a waiting time and we already
know that X ≥ t and ask for the probability that X ≥ t + h (we have to
wait for additional h time units). Then, if X is exponentially distributed we
can verify that
P (X ≥ t + h | X ≥ t) = P (X ≥ h), t, h > 0.
The exponential distribution is the only continuous distribution that is mem-
oryless. In fact, it is possible to derive from the memoryless property that
the distribution of X must be exponential. Similarly, one can show that the
geometric distribution is the only discrete distribution that is memoryless.
Normal distribution
The normal distribution is one of the most important continuous distri-
butions since it naturally arises when we sum up many random variables.
51
4.3. IMPORTANT CONTINUOUS DISTRIBUTIONS
1 −(x − µ)2
f (x) = √ exp ,
σ 2π 2σ 2
The density of the standardized normal distribution with mean µ = 0 and
variance σ 2 = 1 is then defined as
1 −x2
φ(x) = √ e 2
2π
and its cumulative distribution function is
Z x
1 −z 2
Φ(x) = √ e 2 dz.
−∞ 2π
∗ X −µ E(X) − µ
E(X ) = E = =0
σ σ
and
∗ X −µ V (X)
V (X ) = V = = 1.
σ σ2
For X ∼ N (µ, σ 2 ) this means that we can find the cumulative distribution
of X ∗ in a standard normal table and use it to compute that of X. Using
X ∗ = X−µ
σ ⇐⇒ X = X ∗ σ + µ we get
x−µ
P (X ≤ x) = P (X ∗ σ + µ ≤ x) = P (X ∗ ≤ ).
σ
52
4.3. IMPORTANT CONTINUOUS DISTRIBUTIONS
= Φ(0.476) − Φ(−1.524)
Next, we can compute the income limit of the poorest 3% of the employees,
i.e. find x such that P (X < x) = 0.03:
P (X < x) = P X < x−µ σ = Φ( x−µ
σ ) = 0.03
Thus,
Gamma distribution
The Gamma distribution generalizes a number of other distributions (e.g.
exponential) and has a rather flexible shape compared to other continuous
distributions. Therefore it can be used to fit very different types of data.
A random variable X is Gamma distributed with parameters α and β (writ-
ten X ∼ Gamma(α, β)) if its density is given by
1 1 α−1 −x
f (x) = x exp , x > 0, α > 0, β > 0
Γ(α) β α β
53
4.4. MULTIVARIATE RANDOM VARIABLES
X1 , X2 , . . . , Xn : Ω → R
54
4.4. MULTIVARIATE RANDOM VARIABLES
for all j ∈ J. For more than two dimensions, we sum over all variables but
one. For two dimensional RV a well-arranged representation for the joint
55
4.4. MULTIVARIATE RANDOM VARIABLES
Cov(X, Y ) = 0
56
Chapter 5
Generation of Random
Variates
• understand the concept of the inverse transform method for the con-
57
5.1. GENERATING DISCRETE RANDOM VARIATES
1 i m p o r t numpy a s np
2 U = np . random . r a n d ( )
3 X = i n t (U < p )
58
5.1. GENERATING DISCRETE RANDOM VARIATES
1 i m p o r t numpy a s np
2 U = np . random . r a n d ( n )
3 X = np . sum (U < p )
1 i m p o r t numpy a s np
2 X = 1
3 w h i l e ( np . random . r a n d ( ) > p ) :
4 X += 1
5 X
A0 = (0, p0 ),
A1 = [p0 , p0 + p1 ),
A2 = [p0 + p1 , p0 + p1 + p2 ).
...
Then we let X = xi if U falls into interval Ai . Note that since Ai has length
pi the probability that U falls into Ai is exactly pi . Hence X has the desired
distribution.
Algorithmically, the naive approach to find the index i with
p0 + . . . + pi−1 ≤ U < p0 + . . . + pi
is performed as follows:
1. initialize c = p0 for the cumulative value,
59
5.2. INVERSE TRANSFORM METHOD
60
5.2. INVERSE TRANSFORM METHOD
y := F (x) = 1 − e−λx
⇐⇒ 1 − y = e−λx
⇐⇒ ln(1 − y) = −λx
⇐⇒ x = − λ1 ln(1 − y) = F −1 (y).
Next, we formulate the inverse transform sampling algorithm for any given
CDF FX with inverse FX−1 :
2. Return X = F −1 (U ).
P (X ≤ x) = P (F −1 (U ) ≤ x) = P (U ≤ F (x))
where we used that F is monotone in the last step. Finally, we note that
because U is uniformly distributed, P (U ≤ y) = y for any y ∈ [0, 1] and
in particular for y = F (x). Hence, P (U ≤ F (x)) = F (x) and thus P (X ≤
x) = F (x) holds.
61
5.3. REJECTION SAMPLING
Ät
"" ""
=
The numbers U1 and U2 give use points (U2 , U1 ) that are uniformly dis-
tributed within the 2-dimensional rectangle. Whenever the pair (x, y) =
(U2 , U1 ) is below the density f (black points), i.e. y ≤ f (x), we keep x
as a sample and otherwise, we reject x (illustrated as grey pairs (x, y)).
Observe that if x is small, f (x) is large and it is very likely that we keep x
as the point (x, y) falls below the red line. Indeed, it is easy to show that
the samples that we keep have the desired distribution f (x) = e−x (up to
the approximation we made by truncating the x-axis at 8).
For the general method, we first define what the body Bh of a nonnegative
integrable function h on Rd is.
Bh = {(x, y) : x ∈ Rd , 0 ≤ y ≤ h(x)}.
Now, observe the following: if we sample pairs X - h
62
5.3. REJECTION SAMPLING
Using the arguments before, we note that we sample uniformly points (X, U g(X))
on Bg , but partition Bg into Bf and its complement Bg \ Bf . We accept
whenever we hit the area Bf . Hence, the x-components of our samples are
distributed according to f . Moreover, the acceptance probability is
63
5.3. REJECTION SAMPLING
iv) Generate U3 ∼ U (0, 1). Set Z := |Z| if U3 < 1/2 and Z := −|Z|
otherwise.
f (X)
Note that the algorithm can be simplified when noting that g(X) =
2
e−(X−1) /2 and thus
f (X) 2 /2
U g(X) ≤ f (X) ⇐⇒ U≤ g(X) = e−(X−1)
⇐⇒ − ln(U ) ≥ (X − 1)2 /2.
64
Chapter 6
65
6.2. WEAK LAW OF LARGE NUMBERS
µ−a µ µ+a
Let X be a random variable with fi-
nite expectation E(X) and finite vari-
ance V (X). Assume that besides E(X) {ω ∈ Ω | |X(ω) − µ| < a}
and V (X), nothing is known about X
(e.g. the cumulative probability distribu-
tion). Our aim is to reason about the
deviation of X from its expectation. Ac-
cording to Chebyshev’s Inequality (see
figure on the right for an illustration), for any a > 0
V (X)
P (|X − E(X)| ≥ a) ≤ . (6.1)
a2
Note that the proof of the theorem is quite straight forward.
(See also page 36 for the properties of the variance operator.) Thus, Eq. (6.1)
gives us, for any > 0,
σ2
P (|Zn − µ| ≥ ) ≤ .
n · 2
σ2
If is given, we can make n·2
arbitrarily small by increasing n. Thus, for
any > 0,
lim P (|Zn − µ| ≥ ) = 0 (or, equivalently, lim P (|Zn − µ| < ) = 1),
n→∞ n→∞
which is known as the weak law of large numbers 1 . Here, the sequence 1
{Zn }n≥1 of random variables converges “weakly” since only the correspond-
ing probabilities converge.
66
6.3. STRONG LAW OF LARGE NUMBERS
as n → ∞.
Here, we measure the probability of all outcomes ω ∈ Ω with limn→∞ |Zn (ω)−
µ| = 0 and find that this event occurs with probability one. The strong law
of large numbers implies the weak law of large numbers.
We have seen two laws that match our initial intuition that
n
1X
x̄ = xi ≈ µ.
n
i=1
In the following we will see that even more can be derived about sums of
random variables.
Zn − E(Zn ) Zn − µ
Zn∗ = p = √ .
V (Zn ) σ/ n
67
6.4. CENTRAL LIMIT THEOREM.
0 0 0
−4 −2 0 2 4 −4 −2 0 2 4 −4 −2 0 2 4
Figure 6.1: The distribution of Zn∗ approaches the standard normal distri-
bution as n increases.
68
Chapter 7
Parameter Estimation
69
the number of observed photons in the interval [t, t+h] is constant in t, i.e.
we have the same distribution for all intervals of length h. Assume further
that n independent measurements are performed for certain intervals of
length h, i.e. we have n numbers x1 , . . . , xn for the photon flux within
intervals of length h. Clearly, we will in reality never have exact and
truly independent measurements but let us assume for now that
a) the measurements have been taken over n days for, say, h = 1 hour
every day,
as defined in Section 1.2.3 to fit V (X) = λ? Which of the two will give
us a better estimate for λ? What does ”better” mean in this context? We
give answers to these questions in the next sections.
Recall that we use a lower case ’x’ for concrete realizations (real values!)
of a random variable X. When we discuss independent observations that
follow the distribution of X, then we can take two different views:
70
7.1. METHOD OF MOMENTS
• the follow the same distribution, namely the distribution of the random
variable X which represents the random variable that describes a single
data point (e.g. height of a person). Hence, the all have, for instance,
the same expectation:
Note that we often write x̄ for m1 . For k > 1 it is often a good alternative
to consider the k-th sample central moments
n
1X
m̄k = (xi − x̄)k
n
i=1
since non-central moments may become very large. Note that for the sample
variance two definitions exist, the one for m̄2 and the one for s2 . The
difference is that for m̄2 we divide the sum by n while for s2 we divide it by
n−1. The reason is that if we view the sample variance as an estimator of the
true variance of a random variable X based on the data points X1 , . . . , Xn
71
7.1. METHOD OF MOMENTS
(view 2!) where the Xi are i.i.d. (same distribution as X with mean µ and
variance σ 2 ), then the estimator
n
2 1 X 1X
S = (Xi − X̄)2 , where X̄ = Xi ,
n−1 n
i=1 i
is not (E(S̃ 2 ) 6= σ 2 ). We will come back to this issue when we discuss the
pros and cons of the method of moments.
µ1 (θ̂1 , . . . , θ̂K ) = m1
µ̄2 (θ̂1 , . . . , θ̂K ) = m̄2
...
µ̄K (θ̂1 , . . . , θ̂K ) = m̄K
and solve for θ̂1 , . . . , θ̂K . Note that it is also possible to equate the non-
central moments instead. Also note that in general, it may be necessary to
add equations for higher moments if there is no unique solution for θ̂1 , . . . , θ̂K
with K equations (for instance, because some equations are linearly depen-
dent).
72
7.1. METHOD OF MOMENTS
We use the two equations µ = m1 and µ̄2 = m̄2 . Thus, we directly get
√
as a solution that θ1 = m1 = x̄ (from the first equation) and θ2 = m̄2
(from the second equation).
As a variant, assume that it is known that X has mean µ = 0. Then, for
the single remaining parameter σ we do not set K = 1 and consider only
the first equation µ1 (µ, σ) = m1 since the first moment µ1 (µ, σ) is equal to
the first parameter µ and thus does not give any constraints on σ. Instead
we consider the second equation µ̄2 (µ, σ) = m̄2 since µ̄2 (µ, σ) = σ 2 and
√
thus σ = m̄2 .
73
7.2. MAXIMUM LIKELIHOOD ESTIMATION
from the true mean 0 are much larger than the deviations from x̄ and thus
s̃2 is only [Link](x) = 65.3717 while [Link](x,ddof=1) = S 2 = 98.0576
is closer to the true standard deviation (note that the Python command
[Link](x) uses the biased estimator!).
If we could consider the deviations from the (usually unknown) true mean
0 the estimator with the factor n1 becomes unbiased:
1 Pn 1 Pn
E n i=1 (Xi − E(X))2 ) = n i=1 E((Xi − E(X))2 )
1
= nn · V (Xi ) = V (X)
The method of moments has the drawback that it does not take into account
information about more moments than needed to get a unique solution for
the equation system, i.e. if we have a single parameter, we fully rely on only
the mean or only the variance to estimate it. For instance, in Example 56
we only use x̄ and do not additionally take into account the variation of the
data, i.e. s2 or s̃2 .
The generalized method of moments does take into account this information
by considering cost functions instead of equating the population moments
of the distribution and the sample moments.
Lθ (x1 , . . . , xn ) is also called the likelihood of the data and we say that θ̂ is
a maximum likelihood estimator (MLE) of θ if for all possible θ
Note that the MLE of θ is, in general, not unique. Intuitively, the MLE is
the parameter value that “best explains” the observed data.
74
7.2. MAXIMUM LIKELIHOOD ESTIMATION
To maximize the likelihood, we can consider its derivatives w.r.t. θ and find
∂
parameter values where ∂θ Lθ (x1 , . . . , xn ) equals 0 (in some cases such points
do not exist and we find θ̂ at the boundary of the set of possible values for
θ).
Often the maximum of the log-likelihood
which is equal to that of the likelihood (as the logarithm is strictly mono-
tonically increasing), is easier to compute. More concretely, θ̂ maximizes Lθ
if and only if θ̂ maximizes ln Lθ .
d ln L(θ) n (−1) X n 1 X
= + xi = − xi
dθ θ 1−θ θ 1−θ
i i
! n 1 X
0= − xi
θ 1−θ
i
75
7.2. MAXIMUM LIKELIHOOD ESTIMATION
X n(1 − θ) 1 1
⇔ xi = ⇔ x̄ = − 1 ⇔ θ̂ =
θ θ x̄ + 1
i
1 P
n
where x̄ = n xi . Next we find that θ̂ is really a maximizer:
i=1
d2 ln L(θ) n 1 X
= − − xi < 0
dθ2 θ2 (1 − θ)2
i
1
Hence, the MLE of θ is θ̂ = x̄+1 . If we use the alternative definition of the
geometric distribution here, where we count the number of unsuccessful
trials and the first success, how would the MLE change? Hint: this means
that we transform X to X + 1.
Note that in the above example, since the mean of the geometric distribu-
tion is (1 − θ)/θ we matched the mean x̄ of the data and the mean of the
distribution. Note that this is not always the case for a maximum likelihood
estimator, i.e. the estimator of the method of moments is not always the
same as the MLE.
Assume now that we have hypothesized a continuous distribution for our
data. In that case, the likelihood function is
for the outcome xi and some very small h. But this quantity is approximately
equal to 2hfθ (xi ) and thus proportional to the factors of the likelihood de-
fined above. Hence, this approach would result in the same MLE.
76
7.2. MAXIMUM LIKELIHOOD ESTIMATION
! n X 1 1X
0= − xi ⇔ = xi = x̄
λ λ n
i i
d2 n
2
ln L(λ) = − 2 < 0
dλ λ
We remark that for some distributions, the log-likelihood function may not
be useful and also, finding a maximum by setting the derivative to zero is
not always possible. In general, if an analytic solution is not possible, global
optimization methods have to be applied in order to determine, which of
several local optima of the likelihood has the highest value.
We list some important properties of Maximum Likelihood Estimators (some
of the properties require mild “regularity” assumptions, e.g. likelihood func-
tion must be differentiable, support of distribution does not depend on θ.)
77
7.2. MAXIMUM LIKELIHOOD ESTIMATION
lim E[θ̂n ] = θ.
n→∞
L̃
z }| {
L(θ) = L(g −1 (g(θ))
i=1
1 if x = 0,
where χ=0 (x) =
0 otherwise.
∂2 X X
2
L(θ) = − xi /θ2 − (n − xi )/(1 − θ)2 < 0 for any θ ∈ [0, 1]
∂θ
i i
1
A function is one-to-one if every element of the range of the function corresponds to
exactly one element of the domain.
78
7.2. MAXIMUM LIKELIHOOD ESTIMATION
Thus, θ̂ = x̄.
Next, we assume that the variance g(θ) = θ(1 − θ) is our parameter (note
that in the permissible range of θ, g is one-to-one). Define L̃ such that
L̃ (g(θ)) = L(θ), i.e., we consider the same values for the likelihood but
the likelihood function is different as it is a function in g(θ). Setting the
derivative to zero yields
∂ ∂ ∂θ !
L̃(g) = L̃(g) · = 0.
∂g ∂θ |{z} ∂g
=L(θ)
∂ ∂
Since ∂θ L (g(θ)) |θ=θ̂ = 0 it holds that ∂g L̃(g) = 0 if we choose ĝ = g(θ̂) =
x̄(1 − x̄).
One can even show that for any other estimator θ̃, which converges
in distribution to N (θ, σ 2 ), we have δ(θ) ≤ σ 2 (variance is greater or
equal). Therefore, MLEs are called best asymptotically normal. For
large n, we can estimate the probability that θ̂ deviates from the true
value by more than (see confidence intervals; later).
P ( lim θ̂n = θ) = 1.
n→∞
In the case of more than one parameter the MLE is found in a very similar
way, i.e. we find the vector θ̂ = (θ̂1 , ..., θ̂m ) that maximizes L(θ) or ln L(θ).
∂
For instance, for two parameters α and β we try to solve ∂α ln L(θ) = 0 and
∂
∂β ln L(θ) = 0 simultaneously for α and β where θ = (α, β). (See exercises
for an example.)
79
7.2. MAXIMUM LIKELIHOOD ESTIMATION
It is very important to analyze how good the estimated value given by some
estimator θ̂ is. If V (θ̂) is large, then our estimation is of bad quality and
might lead to wrong conclusions about the real system. Typically, when
results ofpparameter estimations are reported, the (estimated) standard de-
viations V (θ) are given as well.
In the previous sections we found that both the method of moments and
the maximum likelihood approach gives the estimator
θ̂ = X̄
for the Poisson distribution, i.e. the best value for the unknown mean of
the Poisson distribution is the sample mean. In Section ?? we already
found that the variance of X̄ is σ 2 /n where σ 2 is the variance of the
distribution of the Xi and thus for the Poisson distribution we get
Often, analytic formulas for V (θ̂) cannot be derived. In this case, one can
either use the bootstrap method or estimate V (θ̂) based on the properties
of the estimator.
80
7.2. MAXIMUM LIKELIHOOD ESTIMATION
Intuitively, the second derivative tells us the curvature of the likelihood and
if it is ’flat’ at θ̂ then our estimated value might not be very accurate and
the variance (negative inverse) is large. Then either we do not have enough
samples or the parameter is difficult to identify (see also the example after
next below). We are not very confident about our estimated value since
perturbing θ̂ slightly also gives us a similar likelihood.
For several parameters, the same approach is used, i.e. the negative diagonal
entries of the inverse of the Hessian matrix give estimates for the variances
of the parameters.
81
7.3. BAYESIAN INFERENCE
Pn
− σn2 −(σ 2 )−2 i=1 (Xi − µ)
= P Pn
−(σ 2 )−2 ni=1 (Xi − µ) n 2 −2
2 (σ ) − (σ 2 )−3 i=1 (Xi − µ)
2
∂2
We have a local maximum at θ̂ = (µ̂, σ̂ 2 ) if ∂µ∂µ ln L < 0 and if the
determinant
∂2 ∂2 ∂2 ∂2
det(H(µ̂, σ̂ 2 )) = ln L · ln L − ln L · ln L
∂µ∂µ ∂σ 2 ∂σ 2 ∂µ∂σ 2 ∂σ 2 ∂µ
(Note that its determinant is indeed positive.) The inverse of the Hessian
is " #
−0.0198 0.0000
.
−0.0000 −0.0392
Thus, the estimated variances are 0.0198 for µ̂ and 0.0392 for σ̂ 2 , yielding
standard errors of 1 = 0.1407 and 2 = 0.1981. Note that the true mean
and variance are elements of the intervals [µ̂−1 , µ̂+1 ] and [σ̂ 2 −2 , σ̂ 2 +
2 ].
If the standard errors are extremely high, this shows that the log- likelihood
function is ’very flat’ around the minimum and thus the estimated values
may not be close to the true values of the parameters. We then have the
problem of parameters that are not identifiable. However, sometimes at
least ratios of parameters are estimated very accurately.
82
7.3. BAYESIAN INFERENCE
In this section, we will follow the Bayesian approach, which differs from the
frequentist approach in that θ is treated as a random variable2 and has a
certain probability distribution. Hence, not only the data is a source of un-
certainty but also θ. Its distribution π(θ), called prior distribution, reflects
our ideas, beliefs, and past experiences about θ before we make use of the
data x1 , . . . , xn , i.e., before we perform inference based on the data.
83
7.3. BAYESIAN INFERENCE
and therefore
84
7.3. BAYESIAN INFERENCE
λα α−1 −λθ
π(θ) = θ e ∼ θα−1 e−λθ ,
Γ(α)
P
Thus, the posterior is a Gamma(α + xi , λ + n) distribution. Note that
the missing constant factor of the distribution can be uniquely determined
as π(θ | ~x) is a proper density.
It is interesting to observe how the mean and the variance of the Gamma
distribution is adjusted, when we take the data into account: For the prior
we Phave E[Θ] = αλ and V P [Θ] = λα2 , while the posterior has E[Θ | ~x] =
α+ xi α+ xi
λ+n and V [Θ | ~x] = (λ+n)2
.
85
7.3. BAYESIAN INFERENCE
π(0.05 | x) ≈ 0.2387,
π(0.10 | x) ≈ 0.7613.
which means that the standard deviation is about 0.02. Obviously, the
MAP estimator would be θMAP = 0.1 as the posterior probability at 0.1
is higher than that at 0.05.
86
Chapter 8
Statistical Testing
Carrying out statistical test that are related to the observations of the real
system and/or the model is very important when we make claims or state-
ments about the system. A popular class of tests are hypothesis tests that
are used to verify statistical hypotheses.
In a statistical hypothesis test, we usually first formulate a (null) hypothesis
H0 and an alternative hypothesis HA . The two statements H0 and HA must
be mutually exclusive and the test can either accept or reject H0 (in favor
of HA ). The null hypothesis is either
• an equality
• the absence of an effect or some relation
Note that this leads to null hypotheses that are in many cases not the
same as the statement that we want to verify. The reason for the above
constraint is that intuitively, we need an equality (or absence of an effect or
some relation) to fix the distribution that we consider during the test.
We begin with a motivating example.
87
266 Probability and Statistics for Computer Scientists
where µ1 is the average number of concurrent users last year, and µ2 is the average number
of concurrent users this year. Depending on the situation, we may replace the two-sided
(1)
alternative HA : µ2 − µ1 ̸= 2000 with a one-sided alternative HA : µ2 − µ1 < 2000 or
(2)
8.1. LEVEL α TESTS: (1)
GENERAL APPROACH
HA : µ2 − µ1 > 2000. The test of H0 against HA evaluates the amount of evidence that
(2)
the mean number of concurrent users changed by fewer than 2000. Testing against HA , we
see if there is sufficient evidence to claim that this number increased by more than 2000. ♦
Example 69: Concurrent Users
Assume that we want to verify the statement that the average number of
concurrent
Example 9.24. To verify users ofofdefective
if the proportion an online PCisgaming
products platform
increased by 2000 this
at most 3%, we test
[Link] define
0 : p = 0.03 vs HA : p > 0.03,
Fromwethe
When testing hypotheses, twothat
realize examples
all we seeabove we see
is a random that Therefore,
sample. there can be two-sided alternatives,
with
all the best statisticsone-sided,
skills, our decision to accept
left-tail or to reject H0(H
alternatives may still
is be
µ wrong.
< µ That
A 0 and one-sided, right-tail
)
would be a sampling error (Section 8.1).
alternatives (HA is µ > µ0 ), where H0 is µ = µ0 .
Four situations are possible,
The outcome of our test depends on a
finite random sample and thus we may
Result of the test
always take a wrong decision. The
Reject H0 Accept H0 four situations depicted on the left are
possible. Our goal is to keep each of
H0 is true Type I error correct
the two errors small. Thus, a good
H0 is false correct Type II error test only results in a wrong decision if
the sample is not very representative
(i.e.
In two of the four cases, the test results in a correct decision. Either we extreme).
accepted a true Often the type I error
hypothesis, or we rejected a false hypothesis. The other two situations are sampling errors.
is seen as more dangerous since it corresponds to ’convicting an innocent
defendant’ or ’sending a healthy patient to a surgery’. Therefore, we fix the
DEFINITION 9.7
probability α of a type I error which is also called the significance level of
A type I error occurs when we reject the true null hypothesis.
the test.
A type II error occurs when we accept the false null hypothesis.
α = P {reject H0 | H0 is true}
The probability of rejecting a false hypothesis (avoid a type II error) is the
Each error occurs with a certain
power of probability
the test that
andweahope to keep small.
function of theA good test resultsθ
parameter
about which we make
in an erroneous decision only if the observed data are somewhat extreme.
our hypothesis:
p(θ) = P {reject H0 | θ; HA is true}
Typically, α is chosen very small, e.g. α ∈ {0.01, 0.05, 0.10} such that the
type I error is kept small and we only reject H0 with a lot of confidence.
88
8.1. LEVEL α TESTS: GENERAL APPROACH
and
Let us now P {Tin∈detail reason
rejection region about
| H0 } = finding
α. the rejection region with area α
since there are many regions with area α below the density curve. The best
choice is an area that ensures that the type II error is small. Thus, we
Step 3: Result and its interpretation
choose the rejection region such that it is likely that T falls into this region
if HA isH0true.
Accept the hypothesis if the This will maximize
test statistic T belongs tothe
the power
acceptanceof region.
the test, i.e. the probability
Reject
H0 in favor of of
the rejecting
alternative H HA0ifgiven HAtoisthetrue.
T belongs Often,
rejection [Link] results in the a choice of the
Our acceptancerejection region
and rejection regionsasguarantee
illustrated insignificance
that the Figure 8.1 levelfor a normally
of our test is distributed test
statistic. Usually, the test statistic is defined such that
Significance level = P { Type I error }
= P { Reject | H0 }
• the right-tail alternative forces T to be large,
= P {T ∈ R | H0 }
= α. (9.13)
• the left-tail alternative forces T to be small,
Therefore, indeed, we have a level α test!
• the two-sided alternative forces T to be either large or small.
89
8.2. 270
STANDARD NORMAL NULL DISTRIBUTION (Z-TEST)
Probability and Statistics for Computer Scientists
Reject
if T is here
Accept Accept
if T is here if T is here
✢ ✲ ❫ ✲
0 zα T −zα 0 T
(a) Right-tail Z-test (b) Left-tail Z-test
Reject Reject
if T is here Accept if T is here
if T is here
❯ ☛ ✲
−zα/2 0 zα/2 T
FIGURE 9.7: Acceptance and rejection regions for a Z-test with (a) a one-sided right-tail
Figure 8.1: Acceptance
alternative; (b) a one-sidedand rejection
left-tail regions
alternative; for a normally
(c) a two-sided alternative. distributed test
statistic. a) one-sided right-tail alternative; b) one-sided left-tail alternative;
c) two-sided alternative.
9.4.5 Standard Normal null distribution (Z-test)
An important case, in terms of a large number of applications, is when the null distribution
of the test statistic is Standard Normal.
8.2The test
Standard Normal
in this case is called Null
a Z-test, and the testDistribution (Z-test)
statistic is usually denoted by Z.
(a) A level α test with a right-tail alternative should
For a large number of applications,
! the null distribution of T (the distribu-
reject H0 if Z ≥ zα
tion of T given H0 is true) is standard
accept H0 if normal.
Z < zα Then the test is (9.14)
called a
Z-test. Usually, one the following cases applies:
The rejection region in this case consists of large values of Z only,
• we consider sample R
means of normally
= [zα , +∞), distributed
A = (−∞, zα ) data,
•(see
weFigure 9.7a).
consider sample means of arbitrarily distributed data where the
Under
number of samplesZ is
the null hypothesis, belongs to A and we reject the null hypothesis with probability
large,
P {T ≥ zα | H0 } = 1 − Φ(zα ) = α,
• we consider sample proportions of arbitrarily distributed data where
making the probability
the number of false rejection
of samples (type I error) equal α .
is large,
For example, we use this acceptance region to test the population mean,
• we consider differences of sample means or sample proportions where
H0 : µ = µ0 vs HA : µ > µ0 .
the number of samples is large.
90
8.2. STANDARD NORMAL NULL DISTRIBUTION (Z-TEST)
X̄ − µ0 5200 − 5000
Z= √ = √ = 2.5.
σ/ n 800/ 100
Note that if, for instance, X̄ = 5100, we would get Z = 1.25 and not have
enough evidence to reject H0 and believe in the alternative hypothesis that
µ0 > 5000.
We test H0 : pA = pB , or H0 : pA − pB = 0, against HA : pA 6= pB
where pA (pB ) is the portion of defective parts from manufacturer A (B),
91
8.2. STANDARD NORMAL NULL DISTRIBUTION (Z-TEST)
respectively.
This is a two-sided test because no direction of the alternative has been
indicated.
2. The critical value is zα/2 = 1.96 (from the table of the standard
normal distribution). Since this is a two-sided test we should reject
H0 if |Z| ≥ 1.96 and accept it otherwise.
92
8.3. T-TESTS FOR UNKNOWN σ
Statistical Inference I 273
Null Parameter,
If H0 is true: Test statistic
hypothesis estimator
θ̂ − θ0
H0 θ, θ̂ E(θ̂) Var(θ̂) Z=!
Var(θ̂)
σ2 X̄ − µ0
µ = µ0 µ, X̄ µ0 √
n σ/ n
p0 (1 − p0 ) p̂ − p0
p = p0 p, p̂ p0 !
n p0 (1−p0 )
n
µX − µY , 2
σX σ2 X̄ − Ȳ − D
µX −µY = D D + Y !
X̄ − Ȳ n m 2
σX 2
σY
n + m
p1 − p2 , p1 (1 − p1 ) p2 (1 − p2 ) p̂1 − p̂2 − D
p1 −p2 = D D + !
p̂1 − p̂2 n m p̂1 (1−p̂1 ) p̂2 (1−p̂2 )
n + m
$ p̂1 − p̂2
" # " #
p1 − p2 , 1 1 1 1
p(1 − p) + , p̂(1 − p̂) +
p1 = p2 0 n m n m
p̂1 − p̂2 where p = p1 = p2
np̂1 + mp̂2
where p̂ = n+m
In the previous section we used an estimator for the unknown true variance
σ 2 . In the special case that our data X1 , . . . , Xn is normally distributed
93
8.3. T-TESTS FOR UNKNOWN σ
with mean µ and variance σ 2 and we estimate the unknown mean with
n
1X
X̄n = Xi
n
i=1
we can make use of a result from statistics that tells us the following:
We know that E[X̄n ] = µ and thus
X̄n − µ
Z= p
σ 2 /n
must follow a standard normal distribution (we standardized it by subtract-
ing the mean and dividing by the standard deviation!). However, since σ 2
is unknown we estimate it using
n
1 X
Sn2 = (Xi − X̄n )2 .
n−1
i=1
Sample size n; X̄ − µ0
µ = µ0 t= √ n−1
unknown σ s/ n
Sample sizes n, m;
unknown but equal X̄ − Ȳ − D
µX − µY = D t= ! n+m−2
standard deviations,
sp n1 + m 1
σX = σY
Sample sizes n, m;
Figure 8.3: Aunequal
summary of T-tests Satterthwaite
unknown, X̄ − Ȳ (from
− D [1]).
µX − µY = D t= ! approximation,
standard deviations, s2X s2Y
+ formula (9.12)
σX ̸= σY n m
94
TABLE 9.2: Summary of T-tests.
at a significance level α = 0.01. From Example 9.19, we have sample statistics n = 18,
X̄ = 0.29 and s = 0.074. Compute the T-statistic,
¯
8.4. P-VALUE
the p-value is the lowest significance level α that forces rejection of H0 and
So far, we were testing hypotheses by means of acceptance and rejection regions. In the last
section, we learned how to use confidence intervals for two-sided tests. Either way, we need
also the highest significance level α that forces acceptance of H0 .
to know the significance level α in order to conduct a test. Results of our test depend on it.
How do we choose α, the probability of making type I sampling error, rejecting the true
hypothesis? Of course, when it seems too dangerous to reject true H0 , we choose a low
Usually α ∈ [0.01, 0.1] (although there are exceptions). Then, a P-value
significance level. How low? Should we choose α = 0.01? Perhaps, 0.001? Or even 0.0001?
greater than 0.1 exceeds all natural significance levels, and the null hypoth-
Also, if our observed test statistic Z = Zobs belongs to a rejection region but it is “too
close to call” (see, for example, Figure 9.9), then how do we report the result? Formally,
esis should be accepted. Conversely, if a P-value is less than 0.01, then it is
we should reject the null hypothesis, but practically, we realize that a slightly different
smaller than all natural significance levels, and the null hypothesis should
significance level α could have expanded the acceptance region just enough to cover Zobs
and force us to accept H0 .
Supposebethatrejected. Only
the result of our test is if the important.
crucially P-valueForhappensexample, the to fall
choice of a between 0.01 and 0.1, we
businessreally have to think about the level of significance. This is the ”too close to
strategy for the next ten years depends on it. In this case, can we rely so heavily
on the choice of α? And if we rejected the true hypothesis just because we chose α = 0.05
instead of α = 0.04, then how do we explain to the chief executive officer that the situation
was marginal? What is the statistical term for “too close to call”?
95
Important:
Also, at this border our observed Z-statistic coincides with the critical value zα ,
Figure 8.4: Interpretation of the
A p-value (shaded p-value
green area) isas
thethe probability
probability that we observe
of an observed
Zobs = zα , result assuming that the null hypothesis is true.
a test statistic (or
T more
thatextreme)
is at least as extreme as Tobs given that H0 is true.
and thus,
P = α = P {Z ≥ zα } = P {Z ≥ Zobs } .
In this formula, Z is any Standard Normal random variable, and Zobs is our observed test
statistic, which is a concrete number, computed from data. First, we compute Zobs , then
use Table A4call”. A good decision is to collect more data until a more
to calculate definitive answer
can be obtained. P {Z ≥ Zobs } = 1 − Φ(Zobs ).
We
P-values for compute
the left-tail and forpthe
bytwo-sided
fixingalternatives
Zobs =arezcomputed
α and similarly,
selecting p =
as given α such that for a
in Table 9.3.
one-sided right-tail alternative we have
This table applies to all the Z-tests in this chapter. It can be directly extended to the case
of unknown standard deviations and T-tests (Table 9.4).
p = α = P (Z ≥ zα ) = P (Z ≥ Zobs ) = 1 − Φ(Zobs )
Understanding P-values
where Z is standard normally distributed and Zobs is the test statistic. The
Looking atcomputation
Tables 9.3 and 9.4,ofwepsee
is that
similar
P-valuefor theprobability
is the one-sided left-tail
the two-sided case
of observing and
a test
statistic at least as extreme as Zobs or tobs . Being “extreme” is determined by the alterna-
as well as for T-tests.
tive. For a right-tail alternative, large numbers are extreme; for a left-tail alternative, small
We summarize the com-
Hypothesis Alternative putation of the p-value for
P-value Computation
H0 HA Z-test on the left where
right-tail we distinguish the three
P {Z ≥ Zobs } 1 − Φ(Zobs )
θ > θ0 different cases for the al-
θ = θ0
left-tail
P {Z ≤ Zobs } Φ(Zobs ) ternative hypothesis HA .
θ < θ0
two-sided From the definition of the
P {|Z| ≥ |Zobs |} 2(1 − Φ(|Zobs |))
θ ̸= θ0
p-value, it is also clear
that
TABLE 9.3: P-values for Z-tests.
In Figure 8.4 we illustrate this interpretation (the green shaded area is the
p-value here and Tobs is the observed data point). Thus, it is wrong to say
that the p-value tells us something about the probability that H0 is true
(given the observation)! A high p-value tells us that the observed or even
more extreme values of Zobs is not so unlikely (given H0 ), and therefore, we
96
8.5. CHI-SQUARE TESTS
see no contradiction with H0 and do not reject it. Conversely, a low p-value
signals that such an extreme test statistic is unlikely if H0 is true. Since we
really observed it, our data are not consistent with H0 and we reject it.
This p-value is quite high and indicates that the null hypothesis should not
be rejected. If H0 is true then the chance of observing a value for Z that
is as extreme or more extreme than Zobs is 34%. This is no contradiction
with the assumption that H0 is true.
97
8.5. CHI-SQUARE TESTS
(n − 1)S 2
χ2obs =
σ02
and compare the value χ2obs with the critical values of the Chi-square distri-
bution. More concretely,
where χ2α is such that α = P (χ2 > χ2α ). We summarize the chi-square test
for the population variance below and omit an example here since this test
Statistical Inference I 291
works exactly as the Z and T tests.
Null Alternative Test Rejection
P-value
Hypothesis Hypothesis statistic region
! "
σ 2 > σ02 χ2obs > χ2α P χ2 ≥ χ2obs
(n − 1)s2 ! "
σ 2 = σ02 σ 2 < σ02 χ2obs < χ2α P χ2 ≤ χ2obs
σ02 # ! "
χ2obs ≥ χ2α/2 or 2 min P χ2 ≥ χ2obs ,
σ 2 ̸= σ02 ! "$
χ2obs ≤ χ21−α/2 P χ2 ≤ χ2obs
(n − 1)s2 (5)(6.232)
χ2obs = = = 6.438.
σ02 2.22
Using Table A6 with ν = n − 1 = 5 degrees of freedom, we see that χ20.80 < χ2obs < χ20.20 .
Therefore, % & % &
8.5. CHI-SQUARE TESTS
99
8.5. CHI-SQUARE TESTS
Then, the corresponding expected count is the expected value of this Bi-
nomial distribution, Exp(k) = npk . We compute χ2 as defined in Eq. 8.1
and conduct the test.
Let us now become more concrete: Suppose that after losing a large
amount of money, an unlucky gambler questions whether the game was
fair and the die was really unbiased. The last 90 tosses of this die gave
the following results
Score 1 2 3 4 5 6
Frequency 20 15 12 17 9 17
χ2 = N k=1 Exp(k)
(20−15)2 (15−15)2 (12−15)2 (17−15)2 (9−15)2 (17−15)2
= 15 + 15 + 15 + 15 + 15 + 15 = 5.2
and find (from the table of the chi-square distribution for N − 1 = 5
degrees of freedom) that the p-value
Assume that we have the same situation as in the previous example, i.e., we
suppose that X1 , ..., Xn have distribution F0 but we also used X1 , ..., Xn to
fit the parameters of F0 (e.g. we computed the MLE θ̂). Assume F0 has m
parameters (m ≥ 1).
One can show that in this case, if H0 is true, then χ2 converges to a chi-
square distribution with k − m − 1 degrees of freedom. Thus, we have to
subtract the number of estimated parameters from the degrees of freedom
and defined the rejection region accordingly. This type of test is called
goodness of fit test.
100
8.5. CHI-SQUARE TESTS
101
8.5. CHI-SQUARE TESTS
In the case of small sample sizes and very unequally distributed data (among
the cells), Fisher’s exact test can be used instead to test for independence.
103
8.5. CHI-SQUARE TESTS
104
Bibliography
[1] Michael Baron. Probability and statistics for computer scientists. CRC
Press, 2013.
105