SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 5.4.1 Densities, distribution functions and the uniform distributionConcept · 30 min
      2. 5.4.2 The exponential distributionConcept · 25 min
      3. 5.4.3 The normal distributionConcept · 30 min
      4. 5.4.4 The lognormal, Student t and chi-squared distributionsConcept · 30 min
      5. 5.4.5 Practice set: continuous distributionsPractice set · 25 min
      6. 5.4.6 Case study: lognormal prices and fat-tailed returnsCase study · 35 min

Level 5, Probability and statistics 5.4 Continuous distributions Lesson 1 of 6

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

Densities, distribution functions and the uniform distribution

Probability as area

A continuous random variable can take any value in an interval: a waiting time, a weight, a measurement error. Lesson 5.2.1 gave each value of a discrete variable its own probability. With a continuum of values that no longer works, so probabilities belong to intervals instead.

A density fXf_X says how probability is spread along the line. The probability that XX lands between aa and bb is the area under fXf_X between them (lesson 2.5.1). With one variable in play, write ff for short.

Rule: Density

P(a<X<b)=∫abf(x) dxf(x)≥0,∫−∞∞f(x) dx=1\begin{gathered} P(a < X < b) = \int_a^b f(x)\,dx \\[4pt] f(x) \ge 0, \quad \int_{-\infty}^{\infty} f(x)\,dx = 1 \end{gathered}

A density is never negative, and its total area is 1: XX lands somewhere. Its units are probability per unit of xx. If XX is in hours, ff is per hour.

Take f(x)=x2f(x) = \frac{x}{2} for xx from 0 to 2, and f(x)=0f(x) = 0 elsewhere. The region under it is a triangle of base 2 and height 1, so its area is 1. Values near 2 are the likeliest: there the density is tallest.

Split [0,2][0, 2] into four pieces of width 0.5. An antiderivative of x2\frac{x}{2} is x24\frac{x^2}{4} (lesson 2.5.2), so each area is a difference of x24\frac{x^2}{4} (lesson 2.5.3).

interval0 to 0.50.5 to 11 to 1.51.5 to 2
probability0.06250.18750.31250.4375
share of 100,000 draws0.06220.18730.31250.438

The four probabilities add to 1. The last row comes from one seeded simulation in the last cell of this lesson, and agrees to about two decimal places.

A density is a rate, not a probability

Over a short interval of width Δx\Delta x, the area is close to a rectangle: P(x<X<x+Δx)≈f(x) ΔxP(x < X < x + \Delta x) \approx f(x)\,\Delta x. For x=1x = 1 and Δx=0.01\Delta x = 0.01 that is 0.5×0.01=0.0050.5 \times 0.01 = 0.005, against the exact 0.005025.

So a density can exceed 1. Spread all the probability evenly over [0,0.25][0, 0.25] and the height must be 4, to make the area 1. What can never exceed 1 is an area.

Try it

f is the density x2\frac{x}{2} on [0,2][0, 2]. quad (lesson 2.5.3) finds its total area and the area from 0.5 to 1.5.

The loop prints the table's four probabilities. The last line compares the exact area from 1 to 1.01 with f(1) Δxf(1)\,\Delta x.

⌘/Ctrl + Enter runs the cell

One value has probability zero

The probability of one exact value is an integral with equal limits, ∫aaf(x) dx=0\int_a^a f(x)\,dx = 0 (lesson 2.5.3). So for a continuous variable P(X=a)=0P(X = a) = 0, and it does not matter whether an interval includes its ends: P(a≤X≤b)=P(a<X<b)P(a \le X \le b) = P(a < X < b).

That is the opposite of lesson 5.2.1, where a die gives P(X=3)=16P(X = 3) = \frac{1}{6}. A probability of 0 does not mean impossible: X=1X = 1 can happen, but no single value carries any weight.

Making a curve into a density

Any curve with g≥0g \ge 0 and a finite area becomes a density once it is divided by that area. Take g(x)=x2g(x) = x^2 on [0,3][0, 3]: its area is [x33]03=9\left[\frac{x^3}{3}\right]_0^3 = 9, so f(x)=x29f(x) = \frac{x^2}{9} is a density. Dividing by the area is called normalising, and the 9 is the normalising constant.

Try it

The first line normalises x2x^2 on [0,3][0, 3]: its area is 9, and after dividing, the area is 1.

The second line asks quad for an interval of zero width: the probability of the single value 1 is 0.

⌘/Ctrl + Enter runs the cell

The distribution function

The distribution function FX(x)=P(X≤x)F_X(x) = P(X \le x) is the area under the density from −∞-\infty up to xx. It is the area function of lesson 2.5.3, started at −∞-\infty as an improper integral (lesson 2.6.3).

Rule: Distribution function

F(x)=∫−∞xf(t) dtF′(x)=f(x)P(a<X≤b)=F(b)−F(a)\begin{gathered} F(x) = \int_{-\infty}^{x} f(t)\,dt \\[4pt] F'(x) = f(x) \\[4pt] P(a < X \le b) = F(b) - F(a) \end{gathered}

FF rises from 0 to 1 and never falls. Its slope is the density, wherever ff is continuous (lesson 2.5.3). Any interval's probability is a difference of two values of FF.

For f(x)=x2f(x) = \frac{x}{2} on [0,2][0, 2], F(x)=0F(x) = 0 below 0, F(x)=x24F(x) = \frac{x^2}{4} from 0 to 2, and F(x)=1F(x) = 1 above 2. So P(0.5<X≤1.5)=F(1.5)−F(0.5)=0.5625−0.0625=0.5P(0.5 < X \le 1.5) = F(1.5) - F(0.5) = 0.5625 - 0.0625 = 0.5.

The same probability twice. It is the shaded area under the density from 0.5 to 1.5, and the rise of the distribution function between the same points.

The distribution function of a discrete variable is a staircase (lesson 5.2.1): it jumps at each value with positive probability. A continuous variable's FF has no jumps, which is another way of saying P(X=a)=0P(X = a) = 0.

Expectation and variance as integrals

Lesson 5.3.1 defined E[X]=∑x p(x)\mathbb{E}[X] = \sum x\,p(x), each value weighted by its probability. For a density the sum becomes an integral: each xx is weighted by f(x) dxf(x)\,dx, the probability of a thin strip around it.

Rule: Expectation and variance

E[X]=∫−∞∞x f(x) dxE[g(X)]=∫−∞∞g(x) f(x) dxVar⁡(X)=E[X2]−μ2\begin{gathered} \mathbb{E}[X] = \int_{-\infty}^{\infty} x\,f(x)\,dx \\[4pt] \mathbb{E}[g(X)] = \int_{-\infty}^{\infty} g(x)\,f(x)\,dx \\[4pt] \operatorname{Var}(X) = \mathbb{E}[X^2] - \mu^2 \end{gathered}

μ=E[X]\mu = \mathbb{E}[X] is the mean, in the units of XX; the variance is in those units squared (lesson 5.3.2). The middle line is LOTUS, the law of the unconscious statistician of lesson 5.3.1, with an integral for the sum.

For f(x)=x2f(x) = \frac{x}{2} on [0,2][0, 2]:

E[X]=∫02x22 dx=[x36]02=43E[X2]=∫02x32 dx=[x48]02=2\begin{gathered} \mathbb{E}[X] = \int_0^2 \frac{x^2}{2}\,dx = \left[\frac{x^3}{6}\right]_0^2 = \frac{4}{3} \\[4pt] \mathbb{E}[X^2] = \int_0^2 \frac{x^3}{2}\,dx = \left[\frac{x^4}{8}\right]_0^2 = 2 \end{gathered}

So Var⁡(X)=2−169=29\operatorname{Var}(X) = 2 - \frac{16}{9} = \frac{2}{9}, and the standard deviation is 2/9≈0.471405\sqrt{2/9} \approx 0.471405. The mean 43\frac{4}{3} sits right of the middle of [0,2][0, 2], where most of the area is.

As in lesson 5.3.1, E[g(X)]\mathbb{E}[g(X)] is not g(E[X])g(\mathbb{E}[X]) in general. Here E[1X]=∫021x⋅x2 dx=1\mathbb{E}\left[\frac{1}{X}\right] = \int_0^2 \frac{1}{x}\cdot\frac{x}{2}\,dx = 1, but 1E[X]=0.75\frac{1}{\mathbb{E}[X]} = 0.75.

A mean exists only if its integral converges (lesson 2.6.3). Lesson 5.4.4 meets a bell-shaped density whose tails are too heavy for that.

Try it

Each line is an integral of the rule, by quad: the mean, E[X2]\mathbb{E}[X^2], the variance, then E[1/X]\mathbb{E}[1/X] against 1/E[X]1/\mathbb{E}[X].

round hides float noise in the last digits.

⌘/Ctrl + Enter runs the cell

The uniform distribution

The simplest density is flat. XX is uniform on [a,b][a, b], written X∼U(a,b)X \sim U(a, b), when every stretch of the same length is equally likely. The symbol ∼\sim reads "has the distribution".

Rule: Uniform distribution

f(x)=1b−a on [a,b]E[X]=a+b2Var⁡(X)=(b−a)212\begin{gathered} f(x) = \frac{1}{b - a} \text{ on } [a, b] \\[4pt] \mathbb{E}[X] = \frac{a + b}{2} \\[4pt] \operatorname{Var}(X) = \frac{(b - a)^2}{12} \end{gathered}

The height is 1 over the width, so the area is 1. A probability is the share of the length: F(x)=x−ab−aF(x) = \frac{x - a}{b - a} on [a,b][a, b]. The mean is the midpoint.

A bus every 12 minutes. You arrive at a random moment, so your wait WW is uniform on [0,12][0, 12], in minutes. Then P(W>9)=312=0.25P(W > 9) = \frac{3}{12} = 0.25, the mean wait is 6 minutes, and the variance is 14412=12\frac{144}{12} = 12 minutes squared. The standard deviation is 12≈3.4641\sqrt{12} \approx 3.4641 minutes.

A rounding error. A reading rounded to the nearest 0.1 is off by an error uniform on [−0.05,0.05][-0.05, 0.05]. The ends can be negative: the mean is 0 and the standard deviation is 0.112≈0.028868\frac{0.1}{\sqrt{12}} \approx 0.028868.

The variance follows from the rule above. E[X2]=∫abx2b−a dx=a2+ab+b23\mathbb{E}[X^2] = \int_a^b \frac{x^2}{b - a}\,dx = \frac{a^2 + ab + b^2}{3}, and subtracting (a+b2)2\left(\frac{a + b}{2}\right)^2 leaves (b−a)212\frac{(b - a)^2}{12}.

Set the width to 4 and the height drops to 0.25. Then slide the interval: its probability is always its length divided by 4, wherever it sits inside [a,b][a, b].

Explore

The curve is a density: probabilities are areas under it. Change the parameters, then set an interval and read the shaded area.

00.5100.51

The dashed line is the mean.

Probability
P(0.2 ≤ X ≤ 0.5) = 0.3
Mean
0.5
Variance
0.0833, so the standard deviation is 0.2887

Try it

scipy.stats.uniform takes loc = aa and scale = the width b−ab - a, not bb. sf(x) is 1−F(x)1 - F(x).

The last line shows the trap: scale=6 with loc=2 means U(2,8)U(2, 8), mean 5, not U(2,6)U(2, 6).

⌘/Ctrl + Enter runs the cell

Quantiles

A quantile answers the reverse question: below which value does XX fall with probability pp? The pp-quantile xpx_p solves F(xp)=pF(x_p) = p. The 0.5-quantile is the median; the 0.25- and 0.75-quantiles are the quartiles.

Rule: Quantile

F(xp)=p,0<p<1F(x_p) = p, \quad 0 < p < 1

xpx_p is in the units of XX. For U(a,b)U(a, b), xp=a+p(b−a)x_p = a + p(b - a). SciPy calls the quantile function ppf, the percent point function.

For the bus, the 0.9-quantile is 0+0.9×12=10.80 + 0.9 \times 12 = 10.8: nine waits in ten are shorter than 10.8 minutes.

For f(x)=x2f(x) = \frac{x}{2}, solve xp24=p\frac{x_p^2}{4} = p to get xp=2px_p = 2\sqrt{p}. The median is 2≈1.414214\sqrt{2} \approx 1.414214 and the quartiles are 1 and 3≈1.732051\sqrt{3} \approx 1.732051. The median is above the mean, 43\frac{4}{3}: the thin side of this density is on the left, and it pulls the mean down.

Simulating with a quantile

The quantile function also makes random draws. Draw UU uniform on [0,1][0, 1]; then xUx_U has the distribution FF. Each pp is equally likely, and xpx_p sends it to the matching place under ff.

This is the inverse transform method; lesson 5.6.1 shows why it works. For our density, 2U2\sqrt{U} is a draw of XX.

Try it

The cell draws 100,000 values of UU with a fixed seed and turns each into 2U2\sqrt{U}. These are the draws of the table at the top of the lesson.

The share below 1 should be near F(1)=0.25F(1) = 0.25, the mean near 43\frac{4}{3} and the variance near 29≈0.2222\frac{2}{9} \approx 0.2222. They are from one seeded run, so they agree to about three decimal places.

⌘/Ctrl + Enter runs the cell

Lesson 5.4.2 meets a density over the whole half-line x≥0x \ge 0, the exponential. Case study 5.4.6 uses quantiles to compare return data with a model.

Common mistakes

  1. A density is not a probability. It can exceed 1; probabilities are areas under it.
  2. One value has probability 0 for a continuous variable, so ≤\le and << give the same answer. For a discrete one they do not.
  3. Subtract values of FF. P(a<X≤b)=F(b)−F(a)P(a < X \le b) = F(b) - F(a), not F(b−a)F(b - a).
  4. Check that the area is 1. If it is not, divide by the normalising constant.
  5. SciPy's uniform takes a width. uniform(loc=a, scale=b - a).
  6. E[X2]\mathbb{E}[X^2] is not (E[X])2(\mathbb{E}[X])^2. Their difference is the variance.
  7. The median and the mean differ unless the density is symmetric.

Densities, distribution functions and the uniform distribution

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

  • A density f≥0f \ge 0 has total area 1, and P(a<X<b)=∫abf(x) dxP(a < X < b) = \int_a^b f(x)\,dx. It is probability per unit of xx, so it can exceed 1.
  • For a continuous variable P(X=a)=0P(X = a) = 0, so the ends of an interval do not matter.
  • F(x)=P(X≤x)=∫−∞xf(t) dtF(x) = P(X \le x) = \int_{-\infty}^x f(t)\,dt rises from 0 to 1, F′=fF' = f, and P(a<X≤b)=F(b)−F(a)P(a < X \le b) = F(b) - F(a).
  • E[g(X)]=∫g(x)f(x) dx\mathbb{E}[g(X)] = \int g(x)f(x)\,dx, and Var⁡(X)=E[X2]−μ2\operatorname{Var}(X) = \mathbb{E}[X^2] - \mu^2.
  • U(a,b)U(a, b) has height 1b−a\frac{1}{b - a}, mean a+b2\frac{a + b}{2} and variance (b−a)212\frac{(b - a)^2}{12}. In SciPy, uniform(loc=a, scale=b - a).
  • The pp-quantile solves F(xp)=pF(x_p) = p (ppf); the median is x0.5x_{0.5}. If U∼U(0,1)U \sim U(0, 1), then xUx_U is a draw of XX.

Free further reading

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