Lesson 3.11 · Stage 03: The maths behind machine learning

Maths chapter 9: Random variables and distributions

A random variable is a number that comes out of a chance event, like the face of a die. A distribution lists every value it can take and how likely each one is. ML...

Published 27 November 2026

A random variable is a number that comes out of a chance event, like the face of a die. A distribution lists every value it can take and how likely each one is. ML assumes data comes from some distribution and tries to learn it.

9.1 Expected value (the mean)

The expected value E[X] is the long-run average. Multiply each value by its probability and add.

E[X]=∑xx⋅P(X=x)E[X] = \sum_{x} x \cdot P(X = x)

Worked example. A fair die: E[X] = (1 + 2 + 3 + 4 + 5 + 6) × 1/6 = 21/6 = 3.5. You never roll 3.5, but over many rolls the average settles there.

For plain data, the mean is the class 7 average: add up and divide by the count. The Greek letter μ (“mu”) is often used for it.

9.2 Variance and standard deviation

Variance measures spread: the average of the squared distances from the mean. Standard deviation σ (“sigma”) is its square root, back in the original units.

Var(X)=1n∑i=1n(xi−μ)2,σ=Var(X)\text{Var}(X) = \frac{1}{n} \sum_{i=1}^{n} (x_i - \mu)^2, \qquad \sigma = \sqrt{\text{Var}(X)}

Worked example. Marks 4, 6, 8. Mean = 6. Distances: −2, 0, 2. Squares: 4, 0, 4. Variance = 8/3 ≈ 2.67. Standard deviation ≈ 1.63.

Marks of 5, 6, 7 would have a smaller variance; marks of 0, 6, 12 a much larger one.

9.3 Covariance and correlation

Covariance says whether two variables move together. Multiply their distances from their own means, then average.

Worked example. Study hours x = 1, 2, 3 and marks y = 2, 4, 6. Means are 2 and 4. Products of distances: (−1)(−2) = 2, (0)(0) = 0, (1)(2) = 2. Covariance = 4/3 ≈ 1.33. Positive, so more hours go with more marks.

Correlation divides covariance by both standard deviations to get a number from −1 to 1. Here it is exactly 1, because the points lie on a perfect straight line. The covariance matrix holds every pair’s covariance; PCA (Chapter 5) takes its eigenvectors.

9.4 Common distributions

Distribution What it models Key formula or fact
Bernoulli one yes/no event with chance p P(1) = p, P(0) = 1 − p
Binomial number of yeses in n tries P(k) = C(n, k) pᵏ (1 − p)ⁿ⁻ᵏ; mean = np
Uniform every value in a range equally likely a fair die; a random number from 0 to 1
Normal (Gaussian) heights, errors, marks: the bell curve set by its mean μ and standard deviation σ

Binomial worked example. Toss a coin 3 times. P(exactly 2 heads) = C(3, 2) × (1/2)² × (1/2)¹ = 3 × 1/8 = 3/8.

9.5 The normal distribution

The bell curve peaks at the mean and falls away evenly on both sides. Its formula looks scary but uses only school pieces: e, π, a square and a square root.

p(x)=1σ2π e−(x−μ)22σ2p(x) = \frac{1}{\sigma\sqrt{2\pi}} \, e^{-\frac{(x - \mu)^2}{2\sigma^2}}

For smooth (continuous) quantities, probability is the area under the curve between two values, not the curve’s height.

The 68–95–99.7 rule: about 68% of values lie within 1σ of the mean, 95% within 2σ, and 99.7% within 3σ.

Worked example. Heights have mean 160 cm and σ = 10 cm. About 68% of people are between 150 and 170 cm. About 95% are between 140 and 180 cm.

Why ML needs it: linear regression assumes errors are normal. Gaussian mixture models, from MML Chapter 11, are several bell curves added together.

Try it

  1. A game pays ₹10 with probability 0.2 and ₹0 otherwise. What is the expected payout?
  2. Find the mean, variance and standard deviation of 2, 4, 6, 8.
  3. Toss a coin 4 times. What is P(exactly 2 heads)?
  4. Exam marks are normal with mean 60 and σ = 5. About what fraction of students score between 50 and 70?
  5. A Bernoulli variable has p = 0.7. What is P(0)?