TensorTau PathToAGI Probability and Uncertainty Level 03

Level 03 · Probability and Uncertainty · Chapter 030

Random Variables and Expectation

How do we turn uncertain outcomes into numbers we can calculate with?

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 XX to be the number shown.

XX 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; XX records a numeric property of its outcome.

For a fair die:

P(X=x)=1/6,x∈{1,2,3,4,5,6}.P(X=x)=1/6,\quad x\in\{1,2,3,4,5,6\}.


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 XX, a density p(x)p(x) is not itself the probability of exactly xx. Probabilities come from areas:

P(a≤X≤b)=∫abp(x) dx.P(a\le X\le b)=\int_a^b p(x)\,dx.


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

E[X]=0(0.5)+10(0.3)+50(0.2)=13.E[X]=0(0.5)+10(0.3)+50(0.2)=13.

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 XX:

E[X]=∑xxP(X=x).\boxed{E[X]=\sum_x xP(X=x)}.

For continuous XX:

E[X]=∫xp(x) dx.\boxed{E[X]=\int x p(x)\,dx}.


4. Expectation of a Function

Often we care not about XX but about a function g(X)g(X).

E[g(X)]=∑xg(x)P(X=x).E[g(X)]=\sum_x g(x)P(X=x).

This is exactly what machine learning losses do. Given data sampled from an unknown distribution, the population risk is

R(θ)=E(X,Y)[L(fθ(X),Y)].R(\theta)=E_{(X,Y)}[L(f_\theta(X),Y)].

Training data lets us approximate that expectation with an average:

R^(θ)=1n∑i=1nL(fθ(xi),yi).\hat R(\theta)=\frac1n\sum_{i=1}^n L(f_\theta(x_i),y_i).

So the familiar mean training loss is an empirical estimate of an expectation.


5. Linearity of Expectation

A remarkable rule:

E[aX+bY+c]=aE[X]+bE[Y]+c.E[aX+bY+c]=aE[X]+bE[Y]+c.

This does not require XX and YY 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

E[L].E[L].

Reinforcement learning often maximizes

E[R].E[R].

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

1+2+3+4+5+66=3.5.\frac{1+2+3+4+5+6}{6}=3.5.

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:

E[X2]≠(E[X])2.E[X^2]\ne(E[X])^2.

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

  1. What is a random variable mathematically?
  2. Why can an expected value be impossible as a single outcome?
  3. Derive the expectation of a fair die.
  4. Why is mean training loss an empirical expectation?
  5. 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.

Six ways to work the chapter, climbing from explaining it in your own words to investigating it like a researcher. Reading is not enough — this is where the chapter lands.

Explain

Explain why a random variable is better thought of as a mapping than as “a number that randomly changes.”

Calculate

For X∈{0,2,10}X\in\{0,2,10\} with probabilities [0.5,0.3,0.2][0.5,0.3,0.2], compute E[X]E[X], E[2X+3]E[2X+3], and E[X2]E[X^2].

Predict

Roll a fair die 10 times, then 100, then 100,000 times. Before coding, predict how the sample mean’s stability changes.

Break

Find a distribution for which E[X2]≠(E[X])2E[X^2]\ne(E[X])^2. Explain why this disproves the idea that expectation can always pass through nonlinear functions.

Build

Write a function that estimates E[g(X)]E[g(X)] from samples and a Python callable g. Test with g(x)=x*x.

Investigate

Estimate expected squared error for a simple predictor using increasingly large random samples. Plot estimate versus sample size and describe convergence.

Shape Check

If losses.shape == [128], what is the shape after losses.mean()? What quantity is the mean trying to estimate?

Ready to Continue?

Continue when you can connect sample averages, population expectations and training loss in one explanation.