The k-th moment of a random variable X is E[Xk], the average of its k-th power. The first is the mean μ. With the second you get the variance, Var(X)=E[X2]−μ2 (lesson 5.3.2), and the third and fourth give the skewness and kurtosis of lesson 5.3.3.
For a fair die X, LOTUS (lesson 5.3.1) gives E[X]=61+2+⋯+6=3.5 and E[X2]=61+4+⋯+36=691≈15.1667. So Var(X)=691−3.52=1235≈2.9167.
Each moment is a separate sum or integral. This lesson packs all of them into one function.
The moment generating function
Rule: Moment generating function
MX(t)=E[etX]
For each number t, the average of etX, by LOTUS. At t=0 it is always E[1]=1. The product tX must have no units, so t is measured per unit of X.
A coin. A Bernoulli variable is 1 with probability p and 0 otherwise (lesson 5.2.2). So M(t)=(1−p)e0+pet=1−p+pet.
A die. Each face k adds 61etk, so M(t)=6et+e2t+⋯+e6t. At t=0.3 it is 3.247172, and 100,000 seeded rolls give an average of e0.3X of 3.2544. It is not e0.3×3.5≈2.857651: the average of etX is not e to the average (lesson 5.3.1).
An exponential. With rate λ, LOTUS gives an integral (lesson 2.6.3):
M(t)=∫0∞etxλe−λxdx=λ∫0∞e−(λ−t)xdx=λ−tλ
This holds only for t<λ. At t≥λ the exponent no longer sinks, and the integral diverges. With λ=2 and t=2.5, the integral up to 10, 20 and 40 is 589.7, 88101.9 and 1940660777.6.
Try it
M is the die's MGF. The first line prints the mean and E[X2] by LOTUS. The second prints M(0), M(0.3) and the wrong guess e0.3×3.5.
Then 100,000 seeded rolls, one run, rounded. The last loops integrate the exponential's MGF with quad, and show it growing without end at t=2.5.
⌘/Ctrl + Enter runs the cell
Moments from the derivatives at 0
The series for ex (lesson 2.7.2), with x=tX, is etX=1+tX+2!t2X2+3!t3X3+⋯. Average each term and the moments appear as coefficients:
MX(t)=1+tE[X]+2!t2E[X2]+3!t3E[X3]+⋯
This is a Taylor series about 0, so its coefficients are the derivatives at 0 divided by k! (lesson 2.7.2). Differentiating once and setting t=0 leaves E[X]; twice leaves E[X2].
Rule: Moments from the MGF
MX(k)(0)=E[Xk]MX′(0)=μ,MX′′(0)=E[X2]
The k-th derivative at t=0 is the k-th moment, when the MGF exists on an interval around 0. On the graph, the slope of M at 0 is the mean.
The die again.M′(t)=6et+2e2t+⋯+6e6t, so M′(0)=3.5. In the same way M′′(0)=61+4+⋯+36=691, and M′′′(0)=2147.
The exponential. Here the series comes for free. λ−tλ=1−t/λ1 is the geometric series of lesson 2.7.2 with ratio λt:
M(t)=1+λt+λ2t2+λ3t3+⋯
Matching each coefficient with k!E[Xk] gives E[Xk]=λkk!. So the mean is λ1 and the variance λ22−λ21=λ21, as in lesson 5.4.2. With λ=2, E[X3]=86=0.75; a million seeded draws give 0.7451.
The series works only for ∣t∣<λ, its radius of convergence (lesson 2.7.2). Its right edge, t=λ, is where the MGF stops existing.
Every MGF passes through 1 at t=0, with slope equal to the mean. The exponential's MGF blows up at t=λ=2, and beyond that its integral diverges.
Try it
SymPy differentiates the die's MGF at 0 once, twice and three times. Central differences (lesson 2.2.2) give the first two again.
sp.series expands the exponential's MGF with λ=2. The last line checks the first three moments with a million seeded draws; rng.exponential takes the mean λ1, not λ.
⌘/Ctrl + Enter runs the cell
The normal's MGF
For a standard normal Z, with density φ, the MGF is ∫etzφ(z)dz. Complete the square in the exponent, as in case study 2.6.5:
tz−2z2=−2(z−t)2+2t2
So etzφ(z)=et2/2φ(z−t): a bell shifted right by t, scaled by et2/2. The shifted bell has area 1, so MZ(t)=et2/2, for every t.
Moving and stretching follows from the definition. E[et(aX+b)]=ebtE[e(at)X], so MaX+b(t)=ebtMX(at). A normal X=μ+σZ (lesson 5.6.1) therefore has MX(t)=eμteσ2t2/2.
Rule: MGF of the normal
Z∼N(0,1):MZ(t)=et2/2X∼N(μ,σ2):MX(t)=eμt+σ2t2/2
It exists for every t. The general rule behind the second line is MaX+b(t)=ebtMX(at).
Moments of Z. Put 2t2 into the series for ex: et2/2=1+2t2+8t4+48t6+⋯. There are no odd powers, so the odd moments are 0, as the bell's symmetry says.
The coefficient of t2 is 2!E[Z2]=21, so E[Z2]=1. That of t4 is 4!E[Z4]=81, so E[Z4]=3: the normal's kurtosis of 3 in lesson 5.3.3. In the same way E[Z6]=48720=15.
An example. For X∼N(1,4), M(t)=et+2t2. Then M′(0)=1 and M′′(0)=5, so the variance is 5−12=4.
At t=1.MX(1)=E[eX]=eμ+σ2/2, the mean of the lognormal eX of lesson 5.4.4. Case study 5.6.5 uses it for a stock price.
Try it
quad integrates etzφ(z), written as one exponential so it cannot overflow; it matches et2/2. A million seeded draws average e0.5Z to 1.1326, against e0.125≈1.133148.
SymPy expands et2/2, lists its first six derivatives at 0 (the moments of Z), and checks the N(1,4) example.
⌘/Ctrl + Enter runs the cell
The characteristic function
Some distributions have no MGF at all. The Student t with 1 degree of freedom, the Cauchy distribution of lesson 5.4.4, has density π(1+x2)1.
Its tail sinks only like x21, and any etx with t>0 outgrows that. Over [−b,b], the integral for E[e0.1X] is 0.9646 at b=10, 10.21 at b=100 and about 8.7×1037 at b=1000.
The fix is an imaginary t. By Euler's formula (lesson 1.7.3), eitx=cos(tx)+isin(tx) is the point at angle tx on the unit circle. Its size is always 1, so its average always exists.
Rule: Characteristic function
ψX(t)=E[eitX]=E[costX]+iE[sintX]
It exists for every distribution and every real t, with ψX(0)=1 and ∣ψX(t)∣≤1. Many books write ϕX; this course keeps φ for the normal density.
For the die, ψ(t) averages six points: eit,e2it,…,e6it, at angles t,2t,…,6t. Their average is their balance point, which lies inside the circle. At t=0.5 it is −0.119777+0.661214i, of size 0.671975; 100,000 seeded rolls give −0.1182+0.6611i.
The characteristic function averages points on the unit circle, weighted by their probabilities. Two distributions with the same mean and variance have characteristic functions that agree near 0 and part further out.
Symmetric distributions give real values. A coin that is +1 or −1 with probability 21 each has ψ(t)=2eit+e−it=cost: the sine parts cancel. The standard normal is symmetric too: ψZ(t)=e−t2/2, which is MZ with it in place of t. It is stated here and checked by quad.
The rule for aX+b carries over, so N(μ,σ2) has ψ(t)=eiμt−σ2t2/2.
No MGF, but a characteristic function. The Student t with 1 degree of freedom has ψ(t)=e−∣t∣, with a corner at 0. That corner matches its missing mean, since ψ′(0)=iE[X] when the mean exists. In general ψ(k)(0)=ikE[Xk].
Try it
psi is the die's characteristic function, with cmath for eiθ (lesson 1.7.3). At t=3π the six points are spread evenly round the circle, so they balance at 0.
Then the normal's real and imaginary parts by quad. weight="cos" makes quad integrate cos(tx) times the Student t density out to infinity; doubling the half-line gives e−∣t∣.
⌘/Ctrl + Enter runs the cell
Either function pins down the distribution
The moments come out of the MGF, but the MGF carries more: the whole distribution. This lesson states the fact without proof.
Rule: Uniqueness
MX=MY near 0, or ψX=ψY⇒the same distribution
Two random variables whose MGFs agree on an interval around 0 have the same distribution. Characteristic functions work the same way, and they exist for every distribution.
So a recognised MGF names the distribution. M(t)=e3t+8t2 is the MGF of N(3,16), because 2σ2=8. M(t)=0.2+0.8et is a coin with p=0.8, and 3et+e2t+e3t is equally likely to be 1, 2 or 3.
A mean and a variance are not enough. The standard normal and the uniform on [−3,3] both have mean 0 and variance 1. Their characteristic functions, e−t2/2 and 3tsin(3t), share 1−2t2 and differ from the t4 term on.
Try it
The loop prints both characteristic functions at four values of t: close at 0.5, far apart at 3. The uniform's is real because the distribution is symmetric.
SymPy expands both. The t4 terms, 403 and 81, are 4!E[X4] for each.
⌘/Ctrl + Enter runs the cell
Lesson 5.6.3 uses MGFs to find the distribution of a sum. Case study 5.6.5 uses the normal's MGF for the mean of a stock price. The Financial Computing with Python course prices options from characteristic functions by Fourier methods.
Common mistakes
M(t) is E[etX], not etE[X]. For the die at t=0.3 they are 3.247172 and 2.857651.
Differentiate, then set t=0.M(1)=E[eX] is not a moment.
M′′(0) is E[X2], not the variance. Subtract the squared mean: 691−3.52=1235 for the die.
An MGF may exist only near 0, or not at all. The exponential's needs t<λ; the Student t has none. The characteristic function always exists.
Move and stretch inside.MaX+b(t)=ebtMX(at), not aMX(t)+b.
The normal's characteristic function sinks. It is e−t2/2, of size at most 1, while its MGF et2/2 grows.
Moment generating and characteristic functions
0 of 12 correct
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
How to type answers
Powers with ^: x^7, 2^(1/3), a^-2.
Multiply with * or by writing side by side: 3x^2, 2(x+1).
ln(x) is the natural log, log(x, 2) is log base 2, e is Euler's number, sqrt(x) is a square root.
sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for sin²x.
Your first answer to each question is saved: it counts towards your score and brings the question back for review.
Exact answers like ln(20)/ln(3) are best. Decimals need about 4 significant figures.
Summary card
Moments: E[Xk]; Var(X)=E[X2]−μ2.
MX(t)=E[etX], with MX(0)=1 and MX(k)(0)=E[Xk]: the moments are k! times its series coefficients.
Coin: 1−p+pet. Exponential: λ−tλ, only for t<λ, so E[Xk]=λkk!. Normal: eμt+σ2t2/2, for every t.
MaX+b(t)=ebtMX(at). For a normal, E[eX]=MX(1)=eμ+σ2/2.
ψX(t)=E[eitX]=E[costX]+iE[sintX]: an average of points on the unit circle, always defined, of size at most 1. Normal: eiμt−σ2t2/2.
An MGF near 0, or a characteristic function, determines the distribution; a mean and variance do not.