SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 5.6.1 Functions of random variablesConcept · 35 min
      2. 5.6.2 Moment generating and characteristic functionsConcept · 30 min
      3. 5.6.3 Sums of independent random variablesConcept · 25 min
      4. 5.6.4 Practice set: transformationsPractice set · 20 min
      5. 5.6.5 Case study: the lognormal from the normalCase study · 30 min

Level 5, Probability and statistics 5.6 Transformations and generating functions Lesson 2 of 5

ConceptLesson 5.6.2 in Level 5, Probability and statistics. About 30 minutes.

Moment generating and characteristic functions

Moments

The kk-th moment of a random variable XX is E[Xk]\mathbb{E}[X^k], the average of its kk-th power. The first is the mean μ\mu. With the second you get the variance, Var⁡(X)=E[X2]−μ2\operatorname{Var}(X) = \mathbb{E}[X^2] - \mu^2 (lesson 5.3.2), and the third and fourth give the skewness and kurtosis of lesson 5.3.3.

For a fair die XX, LOTUS (lesson 5.3.1) gives E[X]=1+2+⋯+66=3.5\mathbb{E}[X] = \frac{1 + 2 + \cdots + 6}{6} = 3.5 and E[X2]=1+4+⋯+366=916≈15.1667\mathbb{E}[X^2] = \frac{1 + 4 + \cdots + 36}{6} = \frac{91}{6} \approx 15.1667. So Var⁡(X)=916−3.52=3512≈2.9167\operatorname{Var}(X) = \frac{91}{6} - 3.5^2 = \frac{35}{12} \approx 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]M_X(t) = \mathbb{E}\big[e^{tX}\big]

For each number tt, the average of etXe^{tX}, by LOTUS. At t=0t = 0 it is always E[1]=1\mathbb{E}[1] = 1. The product tXtX must have no units, so tt is measured per unit of XX.

A coin. A Bernoulli variable is 1 with probability pp and 0 otherwise (lesson 5.2.2). So M(t)=(1−p)e0+pet=1−p+petM(t) = (1 - p)e^{0} + pe^{t} = 1 - p + pe^t.

A die. Each face kk adds 16etk\frac{1}{6}e^{tk}, so M(t)=et+e2t+⋯+e6t6M(t) = \frac{e^t + e^{2t} + \cdots + e^{6t}}{6}. At t=0.3t = 0.3 it is 3.247172, and 100,000 seeded rolls give an average of e0.3Xe^{0.3X} of 3.2544. It is not e0.3×3.5≈2.857651e^{0.3 \times 3.5} \approx 2.857651: the average of etXe^{tX} is not ee to the average (lesson 5.3.1).

An exponential. With rate λ\lambda, LOTUS gives an integral (lesson 2.6.3):

M(t)=∫0∞etx λe−λx dx=λ∫0∞e−(λ−t)x dx=λλ−t\begin{aligned} M(t) &= \int_0^\infty e^{tx}\,\lambda e^{-\lambda x}\,dx \\ &= \lambda\int_0^\infty e^{-(\lambda - t)x}\,dx = \frac{\lambda}{\lambda - t} \end{aligned}

This holds only for t<λt < \lambda. At t≥λt \ge \lambda the exponent no longer sinks, and the integral diverges. With λ=2\lambda = 2 and t=2.5t = 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]\mathbb{E}[X^2] by LOTUS. The second prints M(0)M(0), M(0.3)M(0.3) and the wrong guess e0.3×3.5e^{0.3 \times 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.5t = 2.5.

⌘/Ctrl + Enter runs the cell

Moments from the derivatives at 0

The series for exe^x (lesson 2.7.2), with x=tXx = tX, is etX=1+tX+t2X22!+t3X33!+⋯e^{tX} = 1 + tX + \frac{t^2X^2}{2!} + \frac{t^3X^3}{3!} + \cdots. Average each term and the moments appear as coefficients:

MX(t)=1+t E[X]+t22! E[X2]+t33! E[X3]+⋯\begin{aligned} M_X(t) = 1 &+ t\,\mathbb{E}[X] + \frac{t^2}{2!}\,\mathbb{E}[X^2] \\ &+ \frac{t^3}{3!}\,\mathbb{E}[X^3] + \cdots \end{aligned}

This is a Taylor series about 0, so its coefficients are the derivatives at 0 divided by k!k! (lesson 2.7.2). Differentiating once and setting t=0t = 0 leaves E[X]\mathbb{E}[X]; twice leaves E[X2]\mathbb{E}[X^2].

Rule: Moments from the MGF

MX(k)(0)=E[Xk]MX′(0)=μ,MX′′(0)=E[X2]\begin{gathered} M_X^{(k)}(0) = \mathbb{E}[X^k] \\[4pt] M_X'(0) = \mu, \quad M_X''(0) = \mathbb{E}[X^2] \end{gathered}

The kk-th derivative at t=0t = 0 is the kk-th moment, when the MGF exists on an interval around 0. On the graph, the slope of MM at 0 is the mean.

The die again. M′(t)=et+2e2t+⋯+6e6t6M'(t) = \frac{e^t + 2e^{2t} + \cdots + 6e^{6t}}{6}, so M′(0)=3.5M'(0) = 3.5. In the same way M′′(0)=1+4+⋯+366=916M''(0) = \frac{1 + 4 + \cdots + 36}{6} = \frac{91}{6}, and M′′′(0)=1472M'''(0) = \frac{147}{2}.

The exponential. Here the series comes for free. λλ−t=11−t/λ\frac{\lambda}{\lambda - t} = \frac{1}{1 - t/\lambda} is the geometric series of lesson 2.7.2 with ratio tλ\frac{t}{\lambda}:

M(t)=1+tλ+t2λ2+t3λ3+⋯M(t) = 1 + \frac{t}{\lambda} + \frac{t^2}{\lambda^2} + \frac{t^3}{\lambda^3} + \cdots

Matching each coefficient with E[Xk]k!\frac{\mathbb{E}[X^k]}{k!} gives E[Xk]=k!λk\mathbb{E}[X^k] = \frac{k!}{\lambda^k}. So the mean is 1λ\frac{1}{\lambda} and the variance 2λ2−1λ2=1λ2\frac{2}{\lambda^2} - \frac{1}{\lambda^2} = \frac{1}{\lambda^2}, as in lesson 5.4.2. With λ=2\lambda = 2, E[X3]=68=0.75\mathbb{E}[X^3] = \frac{6}{8} = 0.75; a million seeded draws give 0.7451.

The series works only for ∣t∣<λ\lvert t \rvert < \lambda, its radius of convergence (lesson 2.7.2). Its right edge, t=λt = \lambda, is where the MGF stops existing.

Every MGF passes through 1 at t=0t = 0, with slope equal to the mean. The exponential's MGF blows up at t=λ=2t = \lambda = 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\lambda = 2. The last line checks the first three moments with a million seeded draws; rng.exponential takes the mean 1λ\frac{1}{\lambda}, not λ\lambda.

⌘/Ctrl + Enter runs the cell

The normal's MGF

For a standard normal ZZ, with density φ\varphi, the MGF is ∫etzφ(z) dz\int e^{tz}\varphi(z)\,dz. Complete the square in the exponent, as in case study 2.6.5:

tz−z22=−(z−t)22+t22tz - \frac{z^2}{2} = -\frac{(z - t)^2}{2} + \frac{t^2}{2}

So etzφ(z)=et2/2 φ(z−t)e^{tz}\varphi(z) = e^{t^2/2}\,\varphi(z - t): a bell shifted right by tt, scaled by et2/2e^{t^2/2}. The shifted bell has area 1, so MZ(t)=et2/2M_Z(t) = e^{t^2/2}, for every tt.

Moving and stretching follows from the definition. E[et(aX+b)]=ebt E[e(at)X]\mathbb{E}[e^{t(aX + b)}] = e^{bt}\,\mathbb{E}[e^{(at)X}], so MaX+b(t)=ebtMX(at)M_{aX + b}(t) = e^{bt}M_X(at). A normal X=μ+σZX = \mu + \sigma Z (lesson 5.6.1) therefore has MX(t)=eμteσ2t2/2M_X(t) = e^{\mu t}e^{\sigma^2 t^2/2}.

Rule: MGF of the normal

Z∼N(0,1):MZ(t)=et2/2X∼N(μ,σ2):MX(t)=eμt+σ2t2/2\begin{gathered} Z \sim \mathcal{N}(0, 1){:}\quad M_Z(t) = e^{t^2/2} \\[6pt] X \sim \mathcal{N}(\mu, \sigma^2){:} \\[2pt] M_X(t) = e^{\mu t + \sigma^2 t^2/2} \end{gathered}

It exists for every tt. The general rule behind the second line is MaX+b(t)=ebtMX(at)M_{aX + b}(t) = e^{bt}M_X(at).

Moments of ZZ. Put t22\frac{t^2}{2} into the series for exe^x: et2/2=1+t22+t48+t648+⋯e^{t^2/2} = 1 + \frac{t^2}{2} + \frac{t^4}{8} + \frac{t^6}{48} + \cdots. There are no odd powers, so the odd moments are 0, as the bell's symmetry says.

The coefficient of t2t^2 is E[Z2]2!=12\frac{\mathbb{E}[Z^2]}{2!} = \frac{1}{2}, so E[Z2]=1\mathbb{E}[Z^2] = 1. That of t4t^4 is E[Z4]4!=18\frac{\mathbb{E}[Z^4]}{4!} = \frac{1}{8}, so E[Z4]=3\mathbb{E}[Z^4] = 3: the normal's kurtosis of 3 in lesson 5.3.3. In the same way E[Z6]=72048=15\mathbb{E}[Z^6] = \frac{720}{48} = 15.

An example. For X∼N(1,4)X \sim \mathcal{N}(1, 4), M(t)=et+2t2M(t) = e^{t + 2t^2}. Then M′(0)=1M'(0) = 1 and M′′(0)=5M''(0) = 5, so the variance is 5−12=45 - 1^2 = 4.

At t=1t = 1. MX(1)=E[eX]=eμ+σ2/2M_X(1) = \mathbb{E}[e^X] = e^{\mu + \sigma^2/2}, the mean of the lognormal eXe^X of lesson 5.4.4. Case study 5.6.5 uses it for a stock price.

Try it

quad integrates etzφ(z)e^{tz}\varphi(z), written as one exponential so it cannot overflow; it matches et2/2e^{t^2/2}. A million seeded draws average e0.5Ze^{0.5Z} to 1.1326, against e0.125≈1.133148e^{0.125} \approx 1.133148.

SymPy expands et2/2e^{t^2/2}, lists its first six derivatives at 0 (the moments of ZZ), and checks the N(1,4)\mathcal{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π(1+x2)\frac{1}{\pi(1 + x^2)}.

Its tail sinks only like 1x2\frac{1}{x^2}, and any etxe^{tx} with t>0t > 0 outgrows that. Over [−b,b][-b, b], the integral for E[e0.1X]\mathbb{E}[e^{0.1X}] is 0.9646 at b=10b = 10, 10.21 at b=100b = 100 and about 8.7×10378.7 \times 10^{37} at b=1000b = 1000.

The fix is an imaginary tt. By Euler's formula (lesson 1.7.3), eitx=cos⁡(tx)+isin⁡(tx)e^{itx} = \cos(tx) + i\sin(tx) is the point at angle txtx on the unit circle. Its size is always 1, so its average always exists.

Rule: Characteristic function

ψX(t)=E[eitX]=E[cos⁡tX]+i E[sin⁡tX]\begin{gathered} \psi_X(t) = \mathbb{E}\big[e^{itX}\big] \\[4pt] = \mathbb{E}[\cos tX] + i\,\mathbb{E}[\sin tX] \end{gathered}

It exists for every distribution and every real tt, with ψX(0)=1\psi_X(0) = 1 and ∣ψX(t)∣≤1\lvert \psi_X(t) \rvert \le 1. Many books write ϕX\phi_X; this course keeps φ\varphi for the normal density.

For the die, ψ(t)\psi(t) averages six points: eit,e2it,…,e6ite^{it}, e^{2it}, \ldots, e^{6it}, at angles t,2t,…,6tt, 2t, \ldots, 6t. Their average is their balance point, which lies inside the circle. At t=0.5t = 0.5 it is −0.119777+0.661214i-0.119777 + 0.661214i, of size 0.671975; 100,000 seeded rolls give −0.1182+0.6611i-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+1 or −1-1 with probability 12\frac{1}{2} each has ψ(t)=eit+e−it2=cos⁡t\psi(t) = \frac{e^{it} + e^{-it}}{2} = \cos t: the sine parts cancel. The standard normal is symmetric too: ψZ(t)=e−t2/2\psi_Z(t) = e^{-t^2/2}, which is MZM_Z with itit in place of tt. It is stated here and checked by quad.

The rule for aX+baX + b carries over, so N(μ,σ2)\mathcal{N}(\mu, \sigma^2) has ψ(t)=eiμt−σ2t2/2\psi(t) = e^{i\mu t - \sigma^2 t^2/2}.

No MGF, but a characteristic function. The Student t with 1 degree of freedom has ψ(t)=e−∣t∣\psi(t) = e^{-\lvert t \rvert}, with a corner at 0. That corner matches its missing mean, since ψ′(0)=i E[X]\psi'(0) = i\,\mathbb{E}[X] when the mean exists. In general ψ(k)(0)=ik E[Xk]\psi^{(k)}(0) = i^k\,\mathbb{E}[X^k].

Try it

psi is the die's characteristic function, with cmath for eiθe^{i\theta} (lesson 1.7.3). At t=π3t = \frac{\pi}{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)\cos(tx) times the Student t density out to infinity; doubling the half-line gives e−∣t∣e^{-\lvert t \rvert}.

⌘/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\begin{gathered} M_X = M_Y \text{ near } 0, \text{ or } \psi_X = \psi_Y \\[4pt] \Rightarrow\; \text{the same distribution} \end{gathered}

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+8t2M(t) = e^{3t + 8t^2} is the MGF of N(3,16)\mathcal{N}(3, 16), because σ22=8\frac{\sigma^2}{2} = 8. M(t)=0.2+0.8etM(t) = 0.2 + 0.8e^t is a coin with p=0.8p = 0.8, and et+e2t+e3t3\frac{e^t + e^{2t} + e^{3t}}{3} 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][-\sqrt{3}, \sqrt{3}] both have mean 0 and variance 1. Their characteristic functions, e−t2/2e^{-t^2/2} and sin⁡(3t)3t\frac{\sin(\sqrt{3}t)}{\sqrt{3}t}, share 1−t221 - \frac{t^2}{2} and differ from the t4t^4 term on.

Try it

The loop prints both characteristic functions at four values of tt: close at 0.5, far apart at 3. The uniform's is real because the distribution is symmetric.

SymPy expands both. The t4t^4 terms, 340\frac{3}{40} and 18\frac{1}{8}, are E[X4]4!\frac{\mathbb{E}[X^4]}{4!} 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

  1. M(t)M(t) is E[etX]\mathbb{E}[e^{tX}], not et E[X]e^{t\,\mathbb{E}[X]}. For the die at t=0.3t = 0.3 they are 3.247172 and 2.857651.
  2. Differentiate, then set t=0t = 0. M(1)=E[eX]M(1) = \mathbb{E}[e^X] is not a moment.
  3. M′′(0)M''(0) is E[X2]\mathbb{E}[X^2], not the variance. Subtract the squared mean: 916−3.52=3512\frac{91}{6} - 3.5^2 = \frac{35}{12} for the die.
  4. An MGF may exist only near 0, or not at all. The exponential's needs t<λt < \lambda; the Student t has none. The characteristic function always exists.
  5. Move and stretch inside. MaX+b(t)=ebtMX(at)M_{aX + b}(t) = e^{bt}M_X(at), not aMX(t)+baM_X(t) + b.
  6. The normal's characteristic function sinks. It is e−t2/2e^{-t^2/2}, of size at most 1, while its MGF et2/2e^{t^2/2} grows.

Moment generating and characteristic functions

0 of 12 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
  12. 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]\mathbb{E}[X^k]; Var⁡(X)=E[X2]−μ2\operatorname{Var}(X) = \mathbb{E}[X^2] - \mu^2.
  • MX(t)=E[etX]M_X(t) = \mathbb{E}[e^{tX}], with MX(0)=1M_X(0) = 1 and MX(k)(0)=E[Xk]M_X^{(k)}(0) = \mathbb{E}[X^k]: the moments are k!k! times its series coefficients.
  • Coin: 1−p+pet1 - p + pe^t. Exponential: λλ−t\frac{\lambda}{\lambda - t}, only for t<λt < \lambda, so E[Xk]=k!λk\mathbb{E}[X^k] = \frac{k!}{\lambda^k}. Normal: eμt+σ2t2/2e^{\mu t + \sigma^2 t^2/2}, for every tt.
  • MaX+b(t)=ebtMX(at)M_{aX + b}(t) = e^{bt}M_X(at). For a normal, E[eX]=MX(1)=eμ+σ2/2\mathbb{E}[e^X] = M_X(1) = e^{\mu + \sigma^2/2}.
  • ψX(t)=E[eitX]=E[cos⁡tX]+i E[sin⁡tX]\psi_X(t) = \mathbb{E}[e^{itX}] = \mathbb{E}[\cos tX] + i\,\mathbb{E}[\sin tX]: an average of points on the unit circle, always defined, of size at most 1. Normal: eiμt−σ2t2/2e^{i\mu t - \sigma^2 t^2/2}.
  • An MGF near 0, or a characteristic function, determines the distribution; a mean and variance do not.

Free further reading

Optional. This lesson covers what you need; these add more examples and practice. All are free to read.