Where We Are
Probability described uncertainty over events. Bayes updated beliefs. But models optimize numeric quantities: losses, rewards, prices, token counts. We need a bridge from uncertain outcomes to numbers.
1. A Random Variable Is a Function
Roll a die. The uncertain outcome is one of six faces. Define to be the number shown.
is a random variable: a function assigning a number to each outcome.
The variable is not “random” because the symbol wiggles. The underlying experiment is uncertain; records a numeric property of its outcome.
For a fair die:
2. Discrete and Continuous
A discrete random variable takes countable values: class label, number of clicks, token ID.
A continuous random variable can take values across an interval: temperature, time, weight.
For continuous , a density is not itself the probability of exactly . Probabilities come from areas:
3. Expectation: The Long-Run Center of Mass
Suppose a game pays:
- ₹0 with probability 0.5,
- ₹10 with probability 0.3,
- ₹50 with probability 0.2.
The expected payout is
You may never receive ₹13 in a single play. Expectation is not a promised outcome. It is the probability-weighted center of possible values.
For discrete :
For continuous :
4. Expectation of a Function
Often we care not about but about a function .
This is exactly what machine learning losses do. Given data sampled from an unknown distribution, the population risk is
Training data lets us approximate that expectation with an average:
So the familiar mean training loss is an empirical estimate of an expectation.
5. Linearity of Expectation
A remarkable rule:
This does not require and to be independent.
That fact makes many derivations in ML possible because sums can move through expectation.
6. Expected Loss and Expected Reward
Supervised learning often minimizes
Reinforcement learning often maximizes
Generative models maximize expected log-likelihood. Stochastic gradient methods use noisy samples whose gradients aim to estimate an expected gradient.
Expectation is therefore one of the quiet mathematical ideas underneath almost every learning algorithm.
7. Monte Carlo: Estimate Expectation by Sampling
If we cannot compute an expectation analytically, sample repeatedly and average.
import random
samples = [random.randint(1, 6) for _ in range(100_000)]
print(sum(samples) / len(samples))
# close to 3.5
The theoretical expectation of a fair die is
More samples usually make the empirical average settle closer to the expectation.
8. Shape Check
A model produces per-example losses for a batch:
loss_per_example: [64]
Taking .mean() produces a scalar:
batch_loss: []
That scalar is a sample estimate of expected loss.
9. Break It
“Expected value must be a possible value”
False. A die has expectation 3.5 but never rolls 3.5.
“Average of a small sample equals expectation”
False. It is an estimate and can be noisy.
“Expectation of a nonlinear function equals function of expectation”
Usually false:
This difference will lead directly to variance.
10. Real-World Connection
- minibatch loss estimates population loss,
- dropout training averages over random masks,
- data augmentation introduces random transformations,
- RL optimizes expected return,
- diffusion models use expectations over noise and data,
- evaluation metrics are sample estimates of population quantities.
Mastery Check
- What is a random variable mathematically?
- Why can an expected value be impossible as a single outcome?
- Derive the expectation of a fair die.
- Why is mean training loss an empirical expectation?
- Does linearity of expectation require independence?
Bridge to Chapter 031
Expectation tells us the center. But two variables can move together around their centers.
How do we measure whether two quantities rise and fall together?
That creates covariance and correlation.