Continuous distributions
Density and the probability of an interval as an area, why a density can exceed one, the CDF and quantiles; the uniform, exponential (memoryless) and normal distributions, and gamma and beta in brief.
From sums to areas
The topic Discrete distributions described a random variable by its pmf: list the values, give each one a probability, add them up. That works for counts. But a server's response time, a person's height or a classifier's score take values on a continuum, and there the list breaks down.
The reason already appeared in the topic Kolmogorov's axioms: for a point uniform on , every single value has probability . Adding up zeros gives nothing. So we stop asking about points and start asking about intervals.
A density is a function whose graph encloses total area . The probability that lands in an interval is the area under the density over that interval:
That is all we need from integrals in this topic: is the area under the graph between and . A random variable with a density is called continuous.
Two consequences follow at once. First, for every , because a vertical line has no area. Second, endpoints do not matter: .
An example we will reuse: the density on , and elsewhere. Its graph is a triangle with base and height , so the area is . The probability is the small triangle with base and height :
Values near are more likely than values near , even though each single value has probability zero.
A density is not a probability
The height is not a probability. Over a short interval of width the density is almost flat, so the area is close to a rectangle:
A density is probability per unit length. Nothing stops it from exceeding . A point uniform on has density there: the area must be over a length of . The narrower the range, the taller the density.
Units change density values but not probabilities. Let a height be uniform between and cm. The density is per centimetre. In metres the same range has length , and the density becomes per metre, times larger. The probability of being taller than m is either way.
So a single density value means little on its own. Densities are compared with each other ("values near are twice as likely as values near ") or turned into probabilities through areas. This matters in ML: a model that reports a positive log-density for a data point is not broken. The density there is simply above .
The CDF and quantiles
The topic Discrete distributions introduced the CDF . The definition stays the same. For a continuous variable it is the area to the left of :
It climbs from to and never goes down. For a discrete variable it was a staircase with jumps at the values. A continuous variable has no single values with positive probability, so its CDF has no jumps. Any interval probability is a difference of two CDF values:
In the other direction, the density is the slope of the CDF: wherever is continuous. (A derivative is the slope of a graph; Mathematics for ML has more.) Where the CDF is steep, probability is dense; where it is flat, there is none. For the triangle density the CDF is on , and its slope is again.
Reading the CDF backwards gives quantiles. The -quantile is the point with
so a fraction of the probability lies to its left. If is flat on a stretch and the equation has many solutions, take the leftmost one. The median is the -quantile; the quartiles are the - and -quantiles. When a service promises "p99 latency of 300 ms", it says that the -quantile of the response time is ms: only one request in a hundred is slower.
For the triangle, gives the median . Half of the probability sits between and , where the density is tall.
Uniform and exponential
Now the named families. The simplest is the uniform distribution , with a constant density on :
The probability of a sub-interval is its length divided by . is where simulation starts: np.random.rand() returns (approximately) a draw from it, and every other sampler in this course is built from such draws.
The exponential distribution is the continuous version of the geometric one. Requests reach a server at rate per second, and we wait for the first one. Split time into short slots of length . Each slot, independently, holds a request with probability . Waiting longer than means that the first slots are all empty — a geometric count of trials:
At and , slots of give , slots of give , and the limit is . So has CDF and density
The Poisson distribution gives the same answer from the other side. If the number of requests in seconds is , then means zero requests, and that has probability . At requests per second, a whole second without a request has probability .
The density is highest at and decays. Short waits are common; long ones are rare but do happen. At , half of all waits are shorter than s, yet a wait longer than s still has probability .
Memorylessness
Suppose seconds have passed without a request. How long is the rest of the wait? Conditional probability from the topic Conditional probability and Bayes' formula answers this. The event lies inside , so
The time already spent is forgotten: the exponential distribution is memoryless. At every moment the clock starts afresh. It inherits this from the geometric distribution, which forgets failed trials in the same way. Among continuous distributions, the exponential is the only memoryless one.
This is a strong modelling assumption, and it is often false:
- Arrivals from many independent sources — requests to a large service, radioactive decays — are close to memoryless.
- Things that wear out are not. A disk that has run for five years is closer to failure than a new one.
- A fixed schedule is not either. A bus every minutes has rate per minute. But if you arrive at a random moment, your wait is uniform on and never exceeds minutes. After minutes at the stop, the bus is at most minute away: this clock clearly remembers.
The same rate can come with different distributions. A rate alone does not make a waiting time exponential.
The normal distribution
The normal, or Gaussian, distribution has a bell-shaped density:
The parameter is the centre, where the bell peaks, and is its width. The topic Expectation and variance will show that is the mean and the variance. The second argument of is always : has width . The case is the standard normal distribution, and its CDF is written .
Every normal is a shifted and stretched standard one: if , then . The next topic proves it. For now it means one function serves all normals:
has no formula in elementary functions; libraries compute it numerically (scipy.stats.norm.cdf). Three numbers are worth remembering. A normal variable lies within , and widths of with probabilities , and . This is the 68–95–99.7 rule.
The tails are thin. and : far from the centre the density falls like , much faster than the exponential . Say a regression model's errors follow and we flag errors larger than in absolute value. That is on each side, and of predictions get flagged.
The normal shows up wherever many small independent effects add up: measurement noise, averages over a dataset. The topic The central limit theorem explains why. Where extremes matter — financial returns, request latencies — a normal model underestimates them.
Gamma and beta
Two more families, briefly. The topic Bayesian inference will use both.
Gamma. The exponential distribution waits for the first request; the gamma distribution waits for the -th. With rate , its density on is
The gamma function extends the factorial: for whole , and exists for every , so non-integer shapes are allowed too. With the formula becomes , so . For the density starts at and peaks at . At requests per second, the third request takes longer than second with probability : that is the chance of fewer than requests in the first second, a Poisson sum. One trap: scipy.stats.gamma takes the scale , not the rate .
Beta. A conversion rate, a coin's bias, a click probability all live in . The beta distribution is the standard model for such a number:
The constant makes the area equal to . is . A larger pushes the mass toward , a larger toward , and gives a shape symmetric around . For the peak is at
peaks at with height about — one more density above one. In the topic Bayesian inference a beta distribution will describe what we believe about an unknown conversion rate, and the data will update and .
Discrete and continuous side by side
The two kinds of random variables side by side:
| Discrete | Continuous | |
|---|---|---|
| Described by | pmf | density |
| can be positive | always | |
| sum of the pmf | area under the density | |
| CDF | staircase | climbs without jumps |
| Memoryless waiting time | geometric | exponential |
Not everything fits one column. Revenue per visitor in an A/B test is exactly for most visitors and a spread-out amount for the rest. That is a mixed distribution: an atom at plus a density over positive amounts. Its CDF jumps at and then climbs smoothly. And a named family is a model, not a fact: its fit to the data has to be checked.
Models rarely use a variable as it comes. We take the log of a latency, square an error, pass a score through a sigmoid. If has a known density, what is the density of ? We have already seen a hint. Switching from centimetres to metres is , and the density grew times while every probability stayed put. The next topic, Functions of random variables, turns this into a general rule — and uses it to turn draws from into samples from any distribution.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.