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 says how probability is spread along the line. The probability that lands between and is the area under between them (lesson 2.5.1). With one variable in play, write for short.
Rule: Density
A density is never negative, and its total area is 1: lands somewhere. Its units are probability per unit of . If is in hours, is per hour.
Take for from 0 to 2, and 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 into four pieces of width 0.5. An antiderivative of is (lesson 2.5.2), so each area is a difference of (lesson 2.5.3).
| interval | 0 to 0.5 | 0.5 to 1 | 1 to 1.5 | 1.5 to 2 |
|---|---|---|---|---|
| probability | 0.0625 | 0.1875 | 0.3125 | 0.4375 |
| share of 100,000 draws | 0.0622 | 0.1873 | 0.3125 | 0.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 , the area is close to a rectangle: . For and that is , against the exact 0.005025.
So a density can exceed 1. Spread all the probability evenly over 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 on . 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 .
One value has probability zero
The probability of one exact value is an integral with equal limits, (lesson 2.5.3). So for a continuous variable , and it does not matter whether an interval includes its ends: .
That is the opposite of lesson 5.2.1, where a die gives . A probability of 0 does not mean impossible: can happen, but no single value carries any weight.
Making a curve into a density
Any curve with and a finite area becomes a density once it is divided by that area. Take on : its area is , so is a density. Dividing by the area is called normalising, and the 9 is the normalising constant.
Try it
The first line normalises on : 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.
The distribution function
The distribution function is the area under the density from up to . It is the area function of lesson 2.5.3, started at as an improper integral (lesson 2.6.3).
Rule: Distribution function
rises from 0 to 1 and never falls. Its slope is the density, wherever is continuous (lesson 2.5.3). Any interval's probability is a difference of two values of .
For on , below 0, from 0 to 2, and above 2. So .
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 has no jumps, which is another way of saying .
Expectation and variance as integrals
Lesson 5.3.1 defined , each value weighted by its probability. For a density the sum becomes an integral: each is weighted by , the probability of a thin strip around it.
Rule: Expectation and variance
is the mean, in the units of ; 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 on :
So , and the standard deviation is . The mean sits right of the middle of , where most of the area is.
As in lesson 5.3.1, is not in general. Here , but .
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, , the variance, then against .
round hides float noise in the last digits.
The uniform distribution
The simplest density is flat. is uniform on , written , when every stretch of the same length is equally likely. The symbol reads "has the distribution".
Rule: Uniform distribution
The height is 1 over the width, so the area is 1. A probability is the share of the length: on . The mean is the midpoint.
A bus every 12 minutes. You arrive at a random moment, so your wait is uniform on , in minutes. Then , the mean wait is 6 minutes, and the variance is minutes squared. The standard deviation is minutes.
A rounding error. A reading rounded to the nearest 0.1 is off by an error uniform on . The ends can be negative: the mean is 0 and the standard deviation is .
The variance follows from the rule above. , and subtracting leaves .
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 .
Explore
The curve is a density: probabilities are areas under it. Change the parameters, then set an interval and read the shaded area.
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 = and scale = the width , not . sf(x) is .
The last line shows the trap: scale=6 with loc=2 means , mean 5, not .
Quantiles
A quantile answers the reverse question: below which value does fall with probability ? The -quantile solves . The 0.5-quantile is the median; the 0.25- and 0.75-quantiles are the quartiles.
Rule: Quantile
is in the units of . For , . SciPy calls the quantile function ppf, the percent point function.
For the bus, the 0.9-quantile is : nine waits in ten are shorter than 10.8 minutes.
For , solve to get . The median is and the quartiles are 1 and . The median is above the mean, : 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 uniform on ; then has the distribution . Each is equally likely, and sends it to the matching place under .
This is the inverse transform method; lesson 5.6.1 shows why it works. For our density, is a draw of .
Try it
The cell draws 100,000 values of with a fixed seed and turns each into . These are the draws of the table at the top of the lesson.
The share below 1 should be near , the mean near and the variance near . They are from one seeded run, so they agree to about three decimal places.
Lesson 5.4.2 meets a density over the whole half-line , the exponential. Case study 5.4.6 uses quantiles to compare return data with a model.
Common mistakes
- A density is not a probability. It can exceed 1; probabilities are areas under it.
- One value has probability 0 for a continuous variable, so and give the same answer. For a discrete one they do not.
- Subtract values of . , not .
- Check that the area is 1. If it is not, divide by the normalising constant.
- SciPy's uniform takes a width.
uniform(loc=a, scale=b - a). - is not . Their difference is the variance.
- The median and the mean differ unless the density is symmetric.
Densities, distribution functions and the uniform distribution
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,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for 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 has total area 1, and . It is probability per unit of , so it can exceed 1.
- For a continuous variable , so the ends of an interval do not matter.
- rises from 0 to 1, , and .
- , and .
- has height , mean and variance . In SciPy,
uniform(loc=a, scale=b - a). - The -quantile solves (
ppf); the median is . If , then is a draw of .