06 / 22 · Random Variables
Continuous distributions
Measurements that can take any value in an interval: densities and why a density is not a probability, the distribution function and quantiles (a p99 latency is a quantile), and the three distributions every later note uses: uniform, exponential as the continuous geometric and the only memoryless waiting time, and the normal with its 68–95–99.7 rule and thin tails.
On this page 11
A waiting time, a height, the error of a measurement or the position of a random point can take any value in an interval. Each single value then has probability zero, as the axioms note showed for a point in , so a list of probabilities of values, the PMF of the previous note, describes nothing. The probability sits on intervals instead, and its concentration at each point is described by a density. This note introduces densities, the distribution function and quantiles, and three distributions: the uniform, from which random numbers are made, the exponential, the waiting time with no memory, and the normal, which the central limit theorem will put behind every average.
Notation. is the density of , its distribution function (CDF), its -quantile. reads ” is normal with mean and variance ”; and are the density and CDF of the standard normal .
Densities#
A random variable is continuous (strictly, absolutely continuous) if there is a function with such that for every interval
Probabilities are areas under the density. Whether the endpoints are included does not matter, since single points have probability 0. Sums over values become integrals; otherwise everything said about PMFs carries over.
A density is not a probability. For a short interval, : the density is probability per unit length, and it can exceed 1. A point uniform on has density 2 on that interval. A density also depends on the units: a height measured in metres instead of centimetres has a density 100 times larger, while all probabilities stay the same.
The distribution function and quantiles#
The CDF rises from 0 to 1. At every point where is continuous, the derivative of the CDF is the density, , so every probability is both an area under and a rise of :
One probability as an area and as a rise
The CDF answers “how likely is to be at most ”; its inverse answers “below which value does fall with probability ”. The -quantile is the smallest with ,
Where is continuous and strictly increasing, this is the unique with , written . The general definition handles the two exceptions: a flat stretch of , a range of values that never takes, would give many solutions of , and a jump, a value with positive probability, would give none. The median is the -quantile, the quartiles the - and -quantiles. Service-level targets are quantiles: a “p99 latency of 300 ms” says that the -quantile of the response time is 300 ms. Inverting is also how random numbers with any distribution are made from uniform ones, the subject of the next note.
Uniform#
has the constant density on and 0 outside: the probability of an interval inside is its length divided by . A computer’s random number generator produces (approximations to) independent numbers, and every other distribution in a simulation is made from them.
Exponential#
A machine fails, a request arrives, an atom decays. Suppose such events happen at a constant rate per unit time and without memory: in every short interval of length an event happens with probability about , whatever happened before (a homogeneous Poisson process). Then the waiting time until the first one has the exponential distribution, :
It is the continuous version of the geometric distribution. Divide time into steps of length and run a trial at every step with success probability . The number of steps until the first success is geometric, so , which tends to as :
The exponential distribution is the continuous geometric
It is also the other face of the Poisson distribution. If the number of events in an interval of length is , the first event comes after exactly when the interval contains none: . For requests arriving at 2 per second, a whole second without a request has probability . A constant average rate alone is not enough. Buses that come exactly every 10 minutes also arrive at a rate of per minute, but a passenger who turns up at a random moment waits a uniform time on , with mean 5 minutes, not an exponential one with mean 10.
Like the geometric, the exponential is memoryless: . A component whose lifetime is exponential is as good as new at every moment; it does not age. The converse also holds: a survival function with for all must be exponential, so the exponential is the only memoryless continuous distribution. Real components do wear out, and their lifetimes are then modelled by distributions whose failure rate grows with age.
The exponential is skewed. With per second the mean wait is s, the median s, and the -quantile s, more than four times the mean. A system sized for the average wait misses the tail.
Normal#
The normal (Gaussian) distribution has the density
a bell centred at with width set by . Every normal is a shifted and rescaled standard one: if , then , which the next note proves. So one table, of , serves all normals; has no formula in elementary functions and is computed numerically.
The 68–95–99.7 rule gives the probability of falling within 1, 2 and 3 standard deviations of the mean: , and . A machine cuts rods of length mm, and rods outside are rejected. Both limits are standard deviations from the mean, so the reject rate is .
The normal’s tails are thin: the density falls like . , and , about one in 3.5 million. Measurement errors and averages of many small effects are close to normal, for reasons the central limit theorem explains. Returns on financial assets, file sizes or word frequencies are not, and modelling them as normal underestimates extreme events by orders of magnitude.
What changed#
| Discrete | Continuous | |
|---|---|---|
| Described by | PMF | density , probability per unit length |
| can be positive | always 0 | |
| sum of | integral of | |
| CDF | a staircase | continuous, with |
| Waiting time | geometric, memoryless | exponential, the only memoryless one |
Limitations#
- Not every variable is discrete or continuous. Daily rainfall is exactly 0 with positive probability and continuous otherwise; such mixed distributions need a mass at 0 and a density elsewhere. (There are also continuous CDFs with no density at all, which do not appear in practice.)
- A named distribution is a model. The exponential assumes a constant rate, the normal assumes thin tails; checking the fit, as with the horse kicks of the previous note, comes before using either.
- Densities depend on the units and the parametrization. Probabilities do not; comparing density values across scales, or reading a density value as a probability, is a common error.
What you should remember#
- A continuous variable has ; single values have probability 0; a density is probability per unit length and can exceed 1.
- , ; the -quantile solves (median, quartiles, p99).
- Uniform : constant density ; the raw material of simulation.
- Exponential : , mean , memoryless; the waiting time of a Poisson stream of events.
- Normal : is standard normal; within 1, 2, 3 standard deviations with probability , , ; thin tails.
Code#
The numbers of the note with scipy.stats, and memorylessness by simulation:
import numpy as np
from scipy import stats
Z = stats.norm() # the standard normal
print([round(float(Z.cdf(k) - Z.cdf(-k)), 4) for k in (1, 2, 3)]) # [0.6827, 0.9545, 0.9973]
print(round(2 * Z.sf(2.5), 4), round(Z.sf(3), 5), f'{Z.sf(5):.1e}') # 0.0124 0.00135 2.9e-07
print(stats.uniform(0, 0.5).pdf(0.2)) # 2.0: a density, not a probability
T = stats.expon(scale=1 / 2) # waiting time at a rate of 2 per second
print(round(T.sf(1), 4), T.mean(), round(T.median(), 3), round(T.ppf(0.99), 3)) # 0.1353 0.5 0.347 2.303
# memorylessness by simulation: P(T > 1.5 | T > 0.5) = P(T > 1)
t = np.random.default_rng(0).exponential(1 / 2, 1_000_000)
print(round((t > 1).mean(), 4), round((t[t > 0.5] > 1.5).mean(), 4)) # 0.1353 0.136
# a trial every d seconds with success probability d: P(wait > 1) tends to e^-1
print([round((1 - d) ** round(1 / d), 4) for d in (0.5, 0.1, 0.01)], round(np.exp(-1), 4)) # [0.25, 0.3487, 0.366] 0.3679
Next#
The normal distribution was reduced to the standard one by , and simulation turns uniform numbers into any distribution. Both are cases of one question: if has a known distribution, what is the distribution of ? The next note answers it with the change-of-variables formula, which is also the one-dimensional case of the normalizing flows of the generative models course.
References#
- J. K. Blitzstein, J. Hwang. Introduction to Probability, 2nd ed. CRC Press, 2019. Chapter 5 (continuous random variables, uniform, normal, exponential, Poisson processes).
- W. Feller. An Introduction to Probability Theory and Its Applications, Vol. 2, 2nd ed. Wiley, 1971. Chapter I (the exponential and the uniform densities).
- A. N. Shiryaev. Probability-1, 3rd ed. Springer, 2016. Chapter 2, §3 (distribution functions and densities on the real line).