The "loss" a neural network minimizes is an expected value. The "uncertainty" a Bayesian model reports is a variance. Every metric you compare in an A/B test — and every reward in reinforcement learning — is the expectation of a random variable. If you can compute an average and notice "how spread out is this," you've already got the intuition.
Learning Objectives
After this lesson, you will be able to:
Understand random variables as rules that turn random outcomes into numbers, and tell apart the two types: countable (like dice) and continuous (like temperature)
Calculate the expected value (long-run average) of a random process and understand why it matters even when no single outcome equals the average
Measure how spread out a random process is using variance and standard deviation, and see how AI models use these same ideas in their error-measuring formulas
Apply linearity of expectation to compute E[aX + bY] without needing joint distributions, and explain why Var(X + Y) is not the same formula when X and Y are dependent
Random does not mean unpredictable. A single coin flip is random, but flip a coin 10,000 times and you can predict almost exactly how many heads you will get. This lesson teaches you how to turn randomness into reliable predictions -- which is exactly what every AI model does.
A random variable X is a function that maps outcomes of a random experiment to real numbers. We write it with a capital letter (X) and use lowercase (x) for specific values it can take.
There are two flavors:
Discrete random variables take countable values: die rolls (1-6), number of spam emails per day, pixel values (0-255)
Continuous random variables take any value in a range: temperature, height, neural network weights, loss values
For a discrete random variable, we describe its behavior with a probability mass function (PMF): P(X = x) tells you the probability of each specific value.
For a fair die:
x
1
2
3
4
5
6
P(X = x)
1/6
1/6
1/6
1/6
1/6
1/6
Rules: every probability is between 0 and 1, and they all sum to 1.
For a loaded die that favors 6:
x
1
2
3
4
5
6
P(X = x)
1/10
1/10
1/10
1/10
1/10
1/2
The PMF completely describes the discrete random variable. If you know the PMF, you know everything.
The cleanest way to internalize PMFs is to touch one. Slide n and p on the binomial below and watch the bars rearrange; switch to Poisson and verify the mean equals λ.
Loading visualization...
Try this: Set the binomial to n=10, p=0.5 — symmetric and centered at 5. Now drag p to 0.1 — the bars skew hard right because hits are rare. Switch to Poisson(λ=3) and confirm visually that the mean and variance are both ≈ 3 (a Poisson signature: mean equals variance).
Continuous random variables can take infinitely many values, so we cannot assign a probability to each individual value (it would be zero). Instead we use a probability density function (PDF) f(x).
The PDF does not give probabilities directly. It gives density -- the probability of falling in a range [a, b] is the area under the curve:
Try it! Open the Python REPL (bottom-right of the screen: click Quick Actions, then Python) and type these lines yourself.
The CDF F(x) = P(X <= x) answers: "What is the probability that X is at most x?"
For discrete variables, the CDF is a staircase function that jumps at each possible value. For continuous variables, it is a smooth S-shaped curve that goes from 0 to 1.
The CDF is incredibly useful because:
P(X > x) = 1 - F(x)
P(a < X <= b) = F(b) - F(a)
The median is the value where F(x) = 0.5
In ML, CDFs appear everywhere: computing p-values, calibrating classifier probabilities, and generating samples via inverse CDF sampling.
A fair six-sided die has E[X] = 3.5. Can you ever actually roll 3.5?
The expected value E[X] is the weighted average of all possible outcomes, where each outcome is weighted by its probability. It is the value you would get on average if you repeated the experiment forever.
You can never roll 3.5 -- it is not even on the die! But if you roll 1,000 times and average the results, you will get very close to 3.5. The expected value is about the long run, not any single trial.
For the loaded die: E[X] = 1(0.1) + 2(0.1) + 3(0.1) + 4(0.1) + 5(0.1) + 6(0.5) = 0.1 + 0.2 + 0.3 + 0.4 + 0.5 + 3.0 = 4.5. The loaded die's expected value is higher because 6 is much more likely.
This is the single most useful property in probability. It says:
E[aX+bY]=aE[X]+bE[Y]
This works even when X and Y are dependent. It is one of the rare properties in probability that does not require independence. This is why it is so powerful.
Var(aX + b) = a^2 Var(X) -- adding a constant does not change spread, but scaling by a multiplies variance by a^2
Var(X + Y) = Var(X) + Var(Y) + 2 Cov(X, Y) -- the general identity
Var(X + Y) = Var(X) + Var(Y) iff X and Y are uncorrelated (Cov = 0)
Where covariance is Cov(X, Y) = E[(X − μ_X)(Y − μ_Y)] = E[XY] − E[X]E[Y]. It measures how X and Y move together: positive means "when X is above its mean, Y tends to be too." Zero means uncorrelated. Standardizing covariance gives you correlation (Pearson's r), which is bounded in [-1, 1].
Quick check
A random variable X has Var(X) = 4. What is Var(3X − 7)?
#Compute E[X], Var[X], and watch the Law of Large Numbers
Theory says E[X] = 3.5 for a fair die. The Law of Large Numbers says that as you roll more dice, the running average converges to 3.5 in probability. The cell below proves it experimentally — and it lets you flip to a loaded die and see exactly how the long-run mean shifts.
Try it: Adjust parameters and watch how expectation and variance changeInteractive
Loading visualization...
Try this: Start with a normal distribution. Increase the standard deviation and watch the bell curve flatten -- that is variance in action. Then switch to a uniform distribution and notice how it has a different relationship between range and variance. Finally, try a binomial distribution with different n and p values and verify that E[X] = np.
#Connection to ML: Loss Functions ARE Expectations
This is the punchline of the entire lesson. When you train a machine learning model, you minimize a loss function. But what is the loss function, really?
Random variables map outcomes to numbers -- they transform real-world randomness into mathematical objects that ML can work with; discrete variables have PMFs, continuous variables have PDFs
E[X] is the long-run average -- you may never observe E[X] in a single trial (like rolling 3.5 on a die), but over many trials the average converges to it
Linearity of expectation always holds -- E[aX + bY] = aE[X] + bE[Y] regardless of independence, making it the most powerful tool in probability
Variance measures spread -- Var(X) = E[X^2] - (E[X])^2 quantifies how far outcomes deviate from the mean; unlike expectation, variance is NOT linear unless variables are independent
ML loss functions are expectations -- training minimizes E[L(y, f(x))], the bias-variance decomposition is about E[(y - f(x))^2], and batch normalization computes E[X] and Var(X) over mini-batches
A fair die has E[X] = 3.5 and Var(X) = 2.917. If you define Y = 2X + 1, what is E[Y]?
Next up: Statistical Inference -- hypothesis testing, p-values, and confidence intervals. You will learn to make rigorous claims about populations from samples, and understand the errors that lurk in every statistical decision.