Chapter 5: Continuous random variables

Previous: PS 04 Expectation and Variance | PS 00 Course Map | Next: PS 06 Joint Distributions and Covariance

The big idea

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).

In this chapter you will learn to
  1. Explain why single points have probability zero, and what a density is.
  2. Compute probabilities, means and variances from a density.
  3. Use the CDF and quantiles.
  4. Work with the uniform, exponential and normal distributions.
  5. Standardize to z-scores and use the standard normal.
  6. Transform a random variable and generate samples from a distribution.
Before you start

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

Definition: probability density function

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

A density is not a probability

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 .


5.3 The CDF and quantiles

Definition: CDF

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

Uniform()

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

Exponential()

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

Normal()

Mean , variance . Written .

Reading the formula, piece by piece

  1. : how far is from the mean, measured in standard deviations.
  2. Squaring it: distance in either direction counts the same, so the curve is symmetric.
  3. : the density falls off very fast as you move away. At it is of the peak; at , ; at , .
  4. : 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

WithinProbability
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

Standardization

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 :

00.511.51.6451.9622.53
0.5000.6910.8410.9330.9500.9750.9770.9940.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

  1. 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.
  2. Linear combinations of independent normals are normal:
  3. Its log-density is a quadratic, so maximizing a Gaussian likelihood is minimizing squared error (PS 09 Estimation and Maximum Likelihood).

Its limits

Thin tails

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.


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

Inverse transform method

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

DistributionDensityMeanVariance
Uniform()
Exponential()
Normal()

Three facts to remember:

  1. Probability is area under the density; the height alone is not a probability.
  2. Standardize with ; 68-95-99.7 within 1, 2, 3 standard deviations.
  3. 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.

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.

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.

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

Next: PS 06 Joint Distributions and Covariance: two random quantities at once.