Chapter 5: Continuous random variables
Previous: PS 04 Expectation and Variance | PS 00 Course Map | Next: PS 06 Joint Distributions and Covariance
A measurement such as a length, a time or a sensor reading can take any value in a range, so single values have probability zero and probability must live in intervals. A density curve describes how probability is spread out, and probability is area under the curve. Three continuous distributions cover most needs: uniform, exponential and, above all, the normal (Gaussian).
- Explain why single points have probability zero, and what a density is.
- Compute probabilities, means and variances from a density.
- Use the CDF and quantiles.
- Work with the uniform, exponential and normal distributions.
- Standardize to z-scores and use the standard normal.
- Transform a random variable and generate samples from a distribution.
You need expectation and variance (PS 04 Expectation and Variance) and basic integration: , and the integral as area under a curve.
5.1 Why points have probability zero
A measured length could be 3.2 cm, 3.21 cm, 3.2137 cm, and so on without end. Between any two values there are infinitely many others.
If each of these infinitely many values had some positive probability, the total would be infinite, breaking the rule that probabilities sum to 1. So for a continuous random variable:
This is not a paradox. Think of a dart thrown at a board: the chance of hitting one exact mathematical point is zero, yet the dart lands somewhere. What has positive probability is landing in a region.
So the question is never “what is ?” but “what is ?“
5.2 Probability density functions
A continuous random variable has a density if, for all ,
A density satisfies and .
Probability is area under the density curve. The total area is 1.
What the height means
can be greater than 1. The uniform density on equals 2 on that interval: total area .
The height is probability per unit length. For a tiny interval of width ,
Like a population density (people per square kilometre), it only becomes a count when multiplied by a size.
Endpoints do not matter
Since single points have probability zero,
This is different from discrete variables, where including an endpoint can matter a lot.
Expectation and variance
Replace sums with integrals:
Everything from chapter 4 carries over: linearity, the variance rules, the square-root law.
Worked example 5.1: a triangular density
on , zero elsewhere.
Find . Total area must be 1: , so .
A probability. . Only a quarter, because the density is low near 0.
Mean. .
Variance. . So .
A density is on . Find and .
Answer. . .
5.3 The CDF and quantiles
Unlike the density, is a probability, always between 0 and 1. It rises from 0 to 1 and never decreases. Interval probabilities are differences:
The quantile function runs the CDF backwards: the -quantile is the value with .
- The median is the 0.5 quantile: half the probability below, half above.
- The 0.95 quantile is the value exceeded only 5% of the time.
Worked example 5.2. For example 5.1, on . Median: , so . That is slightly above the mean of . The density has a long thin tail toward 0, which pulls the mean down more than the median. (A tail on one side always drags the mean toward it.)
5.4 The uniform distribution
Every value in is equally dense:
The probability of a sub-interval is its length divided by .
Worked example 5.3. A bus arrives uniformly at random within a 10-minute window. . Mean wait 5 minutes, standard deviation minutes.
Why it matters: computer random number generators produce Uniform(0,1) numbers, and every other distribution is built from them (section 5.8).
Quantization noise. Rounding a measurement to the nearest unit adds an error roughly uniform on , with standard deviation units. That is the noise floor of any digital sensor.
5.5 The exponential distribution
The waiting time until an event that occurs at constant rate :
It is the continuous cousin of the geometric distribution, and it is memoryless in the same way:
A component with exponential lifetime does not “wear out”: if it has survived 1,000 hours, it is as good as new. (Real components often do wear out, so this is a modelling assumption to check.)
Link with Poisson. If events arrive as a Poisson process with rate per hour (chapter 3), the waiting times between them are Exponential().
Worked example 5.4. Faults occur at 2 per hour.
- .
- Mean time between faults: hours.
- Median time between faults: gives hours. Smaller than the mean, because the distribution has a long right tail.
5.6 The normal (Gaussian) distribution
The formula
Mean , variance . Written .
Reading the formula, piece by piece
- : how far is from the mean, measured in standard deviations.
- Squaring it: distance in either direction counts the same, so the curve is symmetric.
- : the density falls off very fast as you move away. At it is of the peak; at , ; at , .
- : the constant that makes the total area 1. A wider curve (larger ) must be lower.
The result is the familiar bell curve: a single peak at , symmetric, with width set by .
The 68-95-99.7 rule
| Within | Probability |
|---|---|
| about 68% | |
| about 95% | |
| about 99.7% |
The exact multiplier for 95% is 1.96. That number reappears constantly in statistics (PS 10 Confidence Intervals and Hypothesis Tests).
Standardizing: z-scores
If , then
the standard normal. Its CDF is written .
So one table (or one function) answers questions about every normal distribution. The z-score says how many standard deviations is from the mean.
Useful values of :
| 0 | 0.5 | 1 | 1.5 | 1.645 | 1.96 | 2 | 2.5 | 3 | |
|---|---|---|---|---|---|---|---|---|---|
| 0.500 | 0.691 | 0.841 | 0.933 | 0.950 | 0.975 | 0.977 | 0.994 | 0.9987 |
By symmetry, .
Worked example 5.5
Adult heights are cm.
. . .
. from to : .
The height exceeded by only 5%. Need , so , and height cm.
Why the normal is everywhere
- Sums of many small independent effects are approximately normal (the central limit theorem, PS 08 Law of Large Numbers and Central Limit Theorem). Measurement noise is typically such a sum.
- Linear combinations of independent normals are normal:
- Its log-density is a quadratic, so maximizing a Gaussian likelihood is minimizing squared error (PS 09 Estimation and Maximum Likelihood).
Its limits
The normal assigns tiny probabilities to extreme values: about 1 in 3.5 million beyond on one side. Real data (financial returns, sensor glitches, human errors) often has outliers far more often. A model that assumes normal noise will be badly misled by them. Always look at a histogram before assuming normality.
. Find and .
Answer. : . from to : .
5.7 Transforming a random variable
If for an increasing, smooth function , how is distributed?
Method 1: through the CDF (always works).
Differentiate to get the density.
Method 2: the change-of-variables formula.
Why the factor ? Probability in a tiny interval must be preserved: . If stretches the axis, the same probability is spread over a longer interval, so the density must drop.
Worked example 5.6. Uniform(0,1) and . Then , , so
Squaring pushes values toward 0 (since ), so the density piles up near 0.
In several dimensions the factor becomes the absolute determinant of the Jacobian (LA 07 Determinants). This is exactly how normalizing flows compute densities.
5.8 Generating samples
Inverse transform sampling
If Uniform(0,1) and is a CDF, then has CDF .
Why: .
Worked example 5.7: exponential samples. . Solve : . So x = -np.log(1 - rng.random()) / lam produces exponential samples from uniform ones.
Normal samples are generated by other tricks (Box-Muller, or library routines), since has no simple formula. To get from standard normal : .
Summary
| Distribution | Density | Mean | Variance |
|---|---|---|---|
| Uniform() | |||
| Exponential() | |||
| Normal() |
Three facts to remember:
- Probability is area under the density; the height alone is not a probability.
- Standardize with ; 68-95-99.7 within 1, 2, 3 standard deviations.
- Transforming a variable rescales its density by .
Exercises
Part A: warm-up
A1. Uniform(2,10). Find , the mean and the variance.
A2. A density is on . Find , and .
A3. . Find and .
A4. A component’s lifetime is exponential with mean 1000 hours. Find and the median lifetime.
A1. . Mean . Variance .
A2. , so . . .
A3. : . from to : .
A4. . . Median hours.
Part B: core
B1. Exam scores are . What score is needed to be in the top 10%? (.)
B2. Two independent measurements of a length are and (variances in mm²). Their average has what variance? Is a weighted average better? Find the that minimizes the variance.
B3. A robot’s position error in one axis is mm. The task tolerates errors up to 5 mm. What fraction of attempts are within tolerance? How small must be for 99.7% to be within tolerance?
B4. Uniform(0,1). Find the density of . Which distribution is it?
B5. Show that the median of Exponential() is , and explain why it is less than the mean.
B1. .
B2. Equal average: . Weighted: . Derivative gives , variance . Better: give more weight to the more precise measurement (weights proportional to ). This is the idea behind sensor fusion.
B3. : . For 99.7% we need , so mm.
B4. , , . Density for : Exponential(1). (This is inverse transform sampling in disguise.)
B5. gives , less than . A few very long waits pull the mean up, but not the median.
Part C: explain and prove
C1. Prove that for Uniform(), .
C2. Prove the memoryless property of the exponential distribution.
C3. Explain why a density value of 3 at some point does not contradict the axioms.
C4. Show that if then , using the change-of-variables formula.
C1. For Uniform(0,1): , , variance . Uniform() is , so by scaling the variance is .
C2. Shown in section 5.5.
C3. Axioms constrain probabilities, which are areas. A height of 3 over an interval narrower than contributes area less than 1.
C4. For : , , . Substituting into the normal density gives , the density.
Part D: practise with code
D1. Draw 100,000 samples from and check the 68-95-99.7 fractions.
D2. Generate exponential samples by inverse transform from uniform samples, and compare their histogram with the density .
D3. Use scipy.stats.norm.cdf and norm.ppf to reproduce the table of values and the answer to B1.
D4. Simulate for Uniform(0,1) and compare the histogram with .
Where this is used
- Sensor noise is usually modelled as Gaussian (04 Perception and State).
- Diffusion models start from Gaussian noise and learn to reverse it (08 Diffusion and Flow Matching).
- Gaussian policies output a mean and a standard deviation for each action dimension (Appendix - PPO SAC and Offline Learning).
- Domain randomization samples simulation parameters from uniform ranges.
Next: PS 06 Joint Distributions and Covariance: two random quantities at once.