Skip to content
AriadneTechnology

The Inner Ring · Chamber 8 of 9

Chance: The Notation of Probability

P, p, 𝔼, ∼ and the vertical bar: reading the probabilistic sentences at the heart of machine learning.

40 min 60 XP + 12 questions + 1 challengeNotationVideoPapersProofsCodeLab

In this chamber you will

  • Read P(A), p(x), p(y | x) and joint distributions
  • Read 𝔼, Var and x ∼ 𝒩(μ, σ²)
  • Derive Bayes' rule from the definition of conditional probability
  • Read the GAN objective and dropout's Bernoulli masks
DiscoverLearnRead beyondPapers & lecturesYour turn

Discover: one line that switches neurons off

Dropout is one of the simplest tricks in deep learning, and one of the most widely used. While a network trains, you switch off a random selection of its units at every step, so that no unit can lean too heavily on any other. Here is how the 2014 journal paper on dropout writes that idea down:

Spotted in the wild

rj(l)∼Bernoulli(p),y~(l)=r(l)∗y(l)r_j^{(l)} \sim \mathrm{Bernoulli}(p), \qquad \widetilde{\mathbf{y}}^{(l)} = \mathbf{r}^{(l)} * \mathbf{y}^{(l)}
Srivastava et al. (2014), “Dropout: A Simple Way to Prevent Neural Networks from Overfitting”, Section 4

You can already read half of it. From Chamber 1: rj(l)r_j^{(l)} is an rr with two addresses, unit jj in layer ll, and the bracketed superscript is an index, not a power. The bold y(l)\mathbf{y}^{(l)} is the vector of layer ll's outputs, and the tilde on y~(l)\widetilde{\mathbf{y}}^{(l)} marks a modified copy. The ∗* is this paper's symbol for multiplying two vectors entry by entry (Chamber 6 writes it ⊙\odot).

What's new is the language of chance: the ∼\sim and the word Bernoulli(p)\mathrm{Bernoulli}(p). Each rj(l)r_j^{(l)} is a coin toss that comes up 1 (keep the unit) or 0 (drop it), and multiplying by it zeroes out the dropped units. By the end of this chamber you'll read every symbol of this line, and of the much more intimidating objective that trains a generative adversarial network (GAN).

Quick check +20 XP

Take a guess before reading on. How would you read rj(l)∼Bernoulli(p)r_j^{(l)} \sim \mathrm{Bernoulli}(p) aloud?

DiscoverLearnRead beyondPapers & lecturesYour turn

Learn: outcomes, events and P

Every probability statement starts with an experiment whose result is uncertain: rolling a die, picking a training example, initialising a network. The set of every possible outcome is the sample space, written Ω\Omega (capital omega). For one roll of a die, Ω={1,2,3,4,5,6}\Omega = \{1, 2, 3, 4, 5, 6\}.

An event is a set of outcomes, so it's a subset A⊆ΩA \subseteq \Omega, in the language of Chamber 2. “The roll is even” is the event A={2,4,6}A = \{2, 4, 6\}. The probability P(A)P(A), read “P of A”, is a number between 0 and 1 saying how likely AA is. For a fair die, every outcome has probability 16\tfrac16, so P(A)=36=12P(A) = \tfrac36 = \tfrac12.

Three rules, known as Kolmogorov's axioms, are all the theory needs:

  • P(A)≥0P(A) \ge 0 for every event AA.
  • P(Ω)=1P(\Omega) = 1: something happens.
  • If AA and BB can't happen together (A∩B=∅A \cap B = \emptyset), then P(A∪B)=P(A)+P(B)P(A \cup B) = P(A) + P(B).

Everything else follows. For example, AA and its complement AcA^c (“not AA”) never happen together, and between them they cover all of Ω\Omega. So the third rule and then the second give P(A)+P(Ac)=P(Ω)=1P(A) + P(A^c) = P(\Omega) = 1, which means P(Ac)=1−P(A)P(A^c) = 1 - P(A). That's a complete proof, three lines long.

Set operations from Chamber 2 become sentences about chance. P(A∩B)P(A \cap B) is “P of A and B”: both happen. Papers often write it with a comma, P(A,B)P(A, B), because in probability a comma means “and”. P(A∪B)P(A \cup B) is “P of A or B”, where “or” means at least one of them. With B={4,5,6}B = \{4, 5, 6\}, “at least 4”:

P(A∩B)=P({4,6})=26,P(A∪B)=P(A)+P(B)−P(A∩B)=12+12−13=23.P(A \cap B) = P(\{4, 6\}) = \tfrac{2}{6}, \qquad P(A \cup B) = P(A) + P(B) - P(A \cap B) = \tfrac12 + \tfrac12 - \tfrac13 = \tfrac23.

The union formula is inclusion–exclusion again: without the subtraction, the outcomes 4 and 6 would be counted twice.

Events and their probabilities
  • Ω\Omega“capital omega, the sample space”
    The sample space: the set of every possible outcome of the experiment.
    Ω={1,2,3,4,5,6}\Omega = \{1, 2, 3, 4, 5, 6\}
  • A⊆ΩA \subseteq \Omega“A, an event”
    An event is a set of outcomes, so a subset of Ω\Omega. “The roll is even” is the event {2,4,6}\{2, 4, 6\}.
    A={2,4,6}A = \{2, 4, 6\}
  • P(A)P(A)“P of A”
    The probability of event AA: a number from 0 (impossible) to 1 (certain).
    P(even)=12P(\text{even}) = \tfrac{1}{2}
  • P(Ac)P(A^c)“P of A complement”
    The probability that AA does not happen. It always equals 1−P(A)1 - P(A).
    P(Ac)=1−P(A)P(A^c) = 1 - P(A)
  • P(A∩B)P(A \cap B)“P of A and B”
    The probability that both happen. Often written with a comma instead: P(A,B)P(A, B).
    P(A∩B)=P(A,B)P(A \cap B) = P(A, B)
  • P(A∪B)P(A \cup B)“P of A or B”
    The probability that at least one happens. Inclusion–exclusion from Chamber 2 gives the formula.
    P(A∪B)=P(A)+P(B)−P(A∩B)P(A \cup B) = P(A) + P(B) - P(A \cap B)
  • P(A∣B)P(A \mid B)“P of A given B”
    Conditional probability: how likely AA is once you know BB happened. The bar is read “given”.
    P(A∣B)=P(A∩B)P(B)P(A \mid B) = \frac{P(A \cap B)}{P(B)}

Learn: the vertical bar means “given”

Information changes probabilities. Suppose a friend peeks at the die and tells you only “it's at least 4”. The chance that it's even is no longer 12\tfrac12: the world has shrunk to B={4,5,6}B = \{4, 5, 6\}, and two of those three outcomes are even. That's conditional probability:

P(A∣B):=P(A∩B)P(B),provided P(B)>0.P(A \mid B) := \frac{P(A \cap B)}{P(B)}, \qquad \text{provided } P(B) > 0.

Read it “P of A given B”. The recipe is to keep only the part of AA that lies inside BB, then divide by P(B)P(B) so that the new, smaller world has total probability 1. For the die, P(A∣B)=2/63/6=23P(A \mid B) = \frac{2/6}{3/6} = \frac23.

Multiplying both sides by P(B)P(B) gives the product rule, P(A∩B)=P(A∣B) P(B)P(A \cap B) = P(A \mid B)\,P(B): the chance of both is the chance of BB, times the chance of AA once BB has happened.

Two events are independent when knowing one tells you nothing about the other: P(A∣B)=P(A)P(A \mid B) = P(A). By the product rule that's the same as P(A∩B)=P(A) P(B)P(A \cap B) = P(A)\,P(B). Independence can surprise you. On a fair die, “even” and “at most 2” are independent: P({2})=16P(\{2\}) = \tfrac16, which is exactly 12⋅13\tfrac12 \cdot \tfrac13.

Quick check +20 XP

Roll one fair die. What is P(roll=6∣roll is even)P(\text{roll} = 6 \mid \text{roll is even})? A fraction like 1/2 is fine.

Learn: Bayes' rule, derived

The product rule works in both directions, and that's all you need to derive the most famous formula in probability. Here is the derivation in full.

  1. 1

    Write the joint probability one way

    Condition on BB: P(A∩B)=P(A∣B) P(B)P(A \cap B) = P(A \mid B)\,P(B).

  2. 2

    Write it the other way

    A∩BA \cap B is the same event as B∩AB \cap A, so we can also condition on AA: P(A∩B)=P(B∣A) P(A)P(A \cap B) = P(B \mid A)\,P(A).

  3. 3

    Set the two equal and divide

    Both right-hand sides equal P(A∩B)P(A \cap B), so they equal each other. Dividing by P(B)>0P(B) > 0:

    P(A∣B)=P(B∣A) P(A)P(B).P(A \mid B) = \frac{P(B \mid A)\,P(A)}{P(B)}.
  4. 4

    Expand the bottom

    BB happens either together with AA or together with AcA^c, never both, so the third axiom and the product rule give the law of total probability:

    P(B)=P(B∣A) P(A)+P(B∣Ac) P(Ac).P(B) = P(B \mid A)\,P(A) + P(B \mid A^c)\,P(A^c).

That's Bayes' rule. It turns a probability you can measure, P(B∣A)P(B \mid A), into the one you actually want, P(A∣B)P(A \mid B). In machine learning, AA is usually a hypothesis hh and BB is data DD, and each piece has a name:

P(h∣D)⏟posterior=P(D∣h)⏞likelihood  P(h)⏞priorP(D)⏟evidence.\underbrace{P(h \mid D)}_{\text{posterior}} = \frac{\overbrace{P(D \mid h)}^{\text{likelihood}}\;\overbrace{P(h)}^{\text{prior}}}{\underbrace{P(D)}_{\text{evidence}}}.

A worked example shows why it matters. A condition affects 1% of people. A test detects it 90% of the time, but also comes back positive for 9% of healthy people. You test positive. How worried should you be?

P(ill∣+)=0.9×0.010.9×0.01+0.09×0.99=0.0090.0981≈0.092.P(\text{ill} \mid +) = \frac{0.9 \times 0.01}{0.9 \times 0.01 + 0.09 \times 0.99} = \frac{0.009}{0.0981} \approx 0.092.

About 9%, not 90%. Picture 1,000 people: 10 are ill and 9 of them test positive, but about 89 of the 990 healthy people test positive too. Only 9 of the roughly 98 positives are actually ill. The prior, 1%, matters enormously.

Learn: random variables and the little p

Most of the time we care about numbers, not events: the number rolled, a pixel's brightness, a model's loss. A random variable is a number whose value depends on chance. Convention: a capital letter XX for the random variable, and a lower-case xx for a particular value it might take. So P(X=x)P(X = x) reads “P of big X equals little x”, the probability that XX comes out as xx.

Papers abbreviate this constantly. p(x)p(x), with a lower-case pp, is the distribution of XX at the value xx:

  • For a discrete XX (finitely many values, like a die), p(x)=P(X=x)p(x) = P(X = x). This is a probability mass function (pmf), and its values add up to 1.
  • For a continuous XX (any real number, like a height), p(x)p(x) is a probability density function (pdf). A single value has probability zero, and probabilities are areas: P(a≤X≤b)=∫abp(x) dxP(a \le X \le b) = \int_a^b p(x)\,dx, the continuous sum of Chamber 7. A density can be larger than 1. Only the total area must be 1.

With two random variables, p(x,y)p(x, y) is their joint distribution. Here is a small one, for tomorrow's weather XX and whether someone carries an umbrella YY:

umbrellano umbrellatotal
sun0.050.550.60
rain0.300.100.40
total0.350.651

The marginal distribution of one variable comes from adding the other one up: p(rain)=∑yp(rain,y)=0.30+0.10=0.40p(\text{rain}) = \sum_y p(\text{rain}, y) = 0.30 + 0.10 = 0.40. The name is literal: those totals are written in the margins of the table. For continuous variables the sum becomes an integral, p(x)=∫p(x,y) dyp(x) = \int p(x, y)\,dy. Either way, people say that yy has been summed out, or marginalised out.

The conditional distribution works just like conditional probability: p(y∣x)=p(x,y)/p(x)p(y \mid x) = p(x, y) / p(x). So p(umbrella∣rain)=0.30/0.40=0.75p(\text{umbrella} \mid \text{rain}) = 0.30 / 0.40 = 0.75. And XX and YY are independent, written X⊥ ⁣ ⁣ ⁣⊥YX \perp\!\!\!\perp Y, when p(x,y)=p(x) p(y)p(x, y) = p(x)\,p(y) for every pair. Here they're not: p(rain) p(umbrella)=0.4×0.35=0.14p(\text{rain})\,p(\text{umbrella}) = 0.4 \times 0.35 = 0.14, far from 0.300.30.

Now the most important line in this chamber for machine learning. A classifier with parameters θ\theta doesn't just output a label. It outputs a whole distribution over labels, written

pθ(y∣x),p_\theta(y \mid x),

read “p theta of y given x”: the probability the model gives to label yy when it sees input xx. The subscript is Chamber 4's fθf_\theta again, saying which model's probabilities these are.

Random variables and distributions
  • XX“random variable X”
    A capital letter is a random variable: a number whose value depends on chance, like the result of a roll.
    X=the number rolledX = \text{the number rolled}
  • P(X=x)P(X = x)“P of big X equals little x”
    The probability that the random variable XX takes the particular value xx. Capital: the random quantity. Lower case: one value it could take.
    P(X=6)=16P(X = 6) = \tfrac{1}{6}
  • p(x)p(x)“p of x”
    The distribution of XX at the value xx. For discrete XX it's a pmf, p(x)=P(X=x)p(x) = P(X = x). For continuous XX it's a density: areas under it are probabilities.
    p(6)=16p(6) = \tfrac{1}{6}
  • p(x,y)p(x, y)“p of x comma y”
    The joint distribution: how likely X=xX = x and Y=yY = y are together.
    p(rain,umbrella)=0.3p(\text{rain}, \text{umbrella}) = 0.3
  • p(y∣x)p(y \mid x)“p of y given x”
    The conditional distribution of YY once X=xX = x is known: p(x,y)/p(x)p(x, y) / p(x).
    p(y∣x)=p(x,y)p(x)p(y \mid x) = \frac{p(x, y)}{p(x)}
  • pθ(y∣x)p_\theta(y \mid x)“p theta of y given x”
    A model: the probability that a network with parameters θ\theta assigns to label yy for input xx.
    pθ(cat∣image)=0.92p_\theta(\text{cat} \mid \text{image}) = 0.92
  • ∑yp(x,y)\sum_{y} p(x, y)“sum over y of p of x comma y”
    Marginalising: add up the joint over every value of yy and you're left with p(x)p(x). People say yy has been “summed out”.
    p(x)=∑yp(x,y)p(x) = \sum_{y} p(x, y)
  • ∫p(x,y) dy\int p(x, y)\, dy“integral of p of x comma y, d y”
    The same marginalisation when yy is continuous: the integral is a continuous sum (Chamber 7).
    p(x)=∫p(x,y) dyp(x) = \int p(x, y)\, dy
  • X⊥ ⁣ ⁣ ⁣⊥YX \perp\!\!\!\perp Y“X is independent of Y”
    Independence: knowing one tells you nothing about the other, so the joint factorises: p(x,y)=p(x) p(y)p(x, y) = p(x)\,p(y).
    p(x,y)=p(x) p(y)p(x, y) = p(x)\,p(y)
Quick check +20 XP

Use the weather table above: p(sun,umbrella)=0.05p(\text{sun}, \text{umbrella}) = 0.05, p(sun,no umbrella)=0.55p(\text{sun}, \text{no umbrella}) = 0.55, p(rain,umbrella)=0.3p(\text{rain}, \text{umbrella}) = 0.3, p(rain,no umbrella)=0.1p(\text{rain}, \text{no umbrella}) = 0.1. You see someone carrying an umbrella. What is p(rain∣umbrella)p(\text{rain} \mid \text{umbrella})? Give 3 decimal places or a fraction.

Learn: drawing samples

To say that xx is a sample from a distribution pp, papers write x∼px \sim p, read “x is drawn from p” or “x is distributed as p”. It's a different symbol from Chamber 1's ≈\approx, and a different job from the tilde decoration on x~\tilde{x}.

A handful of distributions have their own names:

  • Bernoulli, Bernoulli(p)\mathrm{Bernoulli}(p): a biased coin. The value is 1 with probability pp and 0 with probability 1−p1 - p. Dropout's masks are Bernoulli.
  • Normal (or Gaussian), N(μ,σ2)\mathcal{N}(\mu, \sigma^2): the bell curve centred at the mean μ\mu with spread σ\sigma. N(0,1)\mathcal{N}(0, 1) is the standard normal.
  • Uniform, U(a,b)\mathcal{U}(a, b): every value between aa and bb equally likely.

The normal distribution's density is worth reading once, symbol by symbol. It's Chamber 4's exp⁡\exp applied to a squared distance from μ\mu, scaled by σ2\sigma^2, with a constant in front that makes the total area 1:

p(x)=12πσ2exp⁡ ⁣(−(x−μ)22σ2).p(x) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\!\left(-\frac{(x - \mu)^2}{2\sigma^2}\right).

When a whole dataset is drawn, papers add “i.i.d.”: x1,…,xn∼i.i.d.px_1, \ldots, x_n \overset{\text{i.i.d.}}{\sim} p means independent and identically distributed. Every sample comes from the same pp, and none influences another. It's the standing assumption behind the dataset D={(xi,yi)}i=1n\mathcal{D} = \{(x_i, y_i)\}_{i=1}^{n} of Chamber 2.

You'll meet ∼\sim everywhere: random initial weights such as Wij∼N(0,0.01)W_{ij} \sim \mathcal{N}(0, 0.01), noise ε∼N(0,1)\varepsilon \sim \mathcal{N}(0, 1) added to data, a minibatch sampled from the training set, and dropout's coins. In Chamber 9 you'll see a variational autoencoder draw ε∼N(0,1)\varepsilon \sim \mathcal{N}(0, 1) and set z=μ+σεz = \mu + \sigma\varepsilon, which gives z∼N(μ,σ2)z \sim \mathcal{N}(\mu, \sigma^2).

Sampling and named distributions
  • ∼\sim“is drawn from”
    Between a variable and a distribution, ∼\sim says the variable is a sample from it (“is distributed as”). Not to be confused with ≈\approx.
    x∼px \sim p
  • ∼i.i.d.\overset{\text{i.i.d.}}{\sim}“independent and identically distributed as”
    Every sample comes from the same distribution, and none depends on any other. The standing assumption about datasets.
    x1,…,xn∼i.i.d.px_1, \ldots, x_n \overset{\text{i.i.d.}}{\sim} p
  • Bernoulli(p)\mathrm{Bernoulli}(p)“Bernoulli p”
    A biased coin: the value 1 with probability pp and 0 otherwise. Dropout's masks are Bernoulli.
    r∼Bernoulli(0.8)r \sim \mathrm{Bernoulli}(0.8)
  • N(μ,σ2)\mathcal{N}(\mu, \sigma^2)“normal with mean mu and variance sigma squared”
    The bell curve centred at μ\mu. The second slot is the variance σ2\sigma^2, not the standard deviation.
    ε∼N(0,1)\varepsilon \sim \mathcal{N}(0, 1)
  • U(a,b)\mathcal{U}(a, b)“uniform from a to b”
    Every value between aa and bb equally likely. A common way to initialise weights.
    u∼U(0,1)u \sim \mathcal{U}(0, 1)

Learn: expectation, the long-run average

The expectation (or expected value) of XX is its average, with each value weighted by its probability:

E[X]=∑xx p(x)(discrete),E[X]=∫x p(x) dx(continuous).\mathbb{E}[X] = \sum_x x\, p(x) \quad \text{(discrete)}, \qquad \mathbb{E}[X] = \int x\, p(x)\, dx \quad \text{(continuous)}.

Read E[X]\mathbb{E}[X] as “the expectation of X”; the E\mathbb{E} is blackboard bold (Chamber 1). For a fair die, E[X]=1+2+3+4+5+66=3.5\mathbb{E}[X] = \frac{1 + 2 + 3 + 4 + 5 + 6}{6} = 3.5: a value the die never actually shows, but the balance point of its distribution.

Often we want the average of some function of a random variable. The subscript says what's random and where it comes from:

Ex∼p[f(x)]=∑xf(x) p(x),\mathbb{E}_{x \sim p}[f(x)] = \sum_x f(x)\, p(x),

read “the expectation of f of x, with x drawn from p”. You'll meet this shape in the GAN objective at the end of this chamber. And here's the idea that makes it usable: if x1,…,xn∼i.i.d.px_1, \ldots, x_n \overset{\text{i.i.d.}}{\sim} p, then

Ex∼p[f(x)]≈1n∑i=1nf(xi),\mathbb{E}_{x \sim p}[f(x)] \approx \frac{1}{n} \sum_{i=1}^{n} f(x_i),

and the approximation gets better as nn grows. That's the law of large numbers. It's why a training loss, 1n∑iℓ(θ;xi,yi)\frac{1}{n}\sum_{i} \ell(\theta; x_i, y_i), is a sensible stand-in for the expected loss on data you haven't seen, and why a minibatch gradient is a sensible stand-in for the full gradient.

A proof: expectation is linear

Here is a complete proof of the property you'll use most, for a discrete random variable.

Claim. For any constants aa and bb, E[aX+b]=a E[X]+b\mathbb{E}[aX + b] = a\,\mathbb{E}[X] + b.

Proof. The random variable aX+baX + b takes the value ax+bax + b whenever X=xX = x, so by the definition of expectation applied to f(x)=ax+bf(x) = ax + b,

E[aX+b]=∑x(ax+b) p(x)=a∑xx p(x)+b∑xp(x)=a E[X]+b⋅1.\mathbb{E}[aX + b] = \sum_x (ax + b)\,p(x) = a \sum_x x\, p(x) + b \sum_x p(x) = a\,\mathbb{E}[X] + b \cdot 1.

The middle step splits one sum into two and pulls the constants out (Chamber 5's linearity of sums). The last step uses the definition of E[X]\mathbb{E}[X] for the first sum, and the fact that probabilities add up to 1 for the second. ■\blacksquare

The same argument shows E[X+Y]=E[X]+E[Y]\mathbb{E}[X + Y] = \mathbb{E}[X] + \mathbb{E}[Y] for any two random variables, independent or not.

Two more summaries measure spread. The variance is the expected squared distance from the mean, and its square root is the standard deviation σ\sigma:

Var(X)=E[(X−E[X])2],σ=Var(X).\mathrm{Var}(X) = \mathbb{E}\big[(X - \mathbb{E}[X])^2\big], \qquad \sigma = \sqrt{\mathrm{Var}(X)}.

For the die, Var(X)=3512≈2.92\mathrm{Var}(X) = \frac{35}{12} \approx 2.92 and σ≈1.71\sigma \approx 1.71. The covariance Cov(X,Y)=E[(X−E[X])(Y−E[Y])]\mathrm{Cov}(X, Y) = \mathbb{E}\big[(X - \mathbb{E}[X])(Y - \mathbb{E}[Y])\big] asks whether two variables move together. Positive means they tend to be high together, negative means one is high when the other is low. Independent variables have covariance zero.

Expectation, variance and covariance
  • E[X]\mathbb{E}[X]“the expectation of X”
    The expected value: the average of XX, weighting each value by its probability. The long-run mean of many samples.
    E[X]=∑xx p(x)\mathbb{E}[X] = \sum_x x\, p(x)
  • Ex∼p[f(x)]\mathbb{E}_{x \sim p}[f(x)]“expectation of f of x, x drawn from p”
    The average of f(x)f(x) when xx is drawn from pp. The subscript names the random variable and its distribution.
    Ex∼p[f(x)]=∑xf(x) p(x)\mathbb{E}_{x \sim p}[f(x)] = \sum_x f(x)\, p(x)
  • Var(X)\mathrm{Var}(X)“the variance of X”
    How spread out XX is: the expected squared distance from its mean. Its square root is the standard deviation σ\sigma.
    Var(X)=E[(X−E[X])2]\mathrm{Var}(X) = \mathbb{E}[(X - \mathbb{E}[X])^2]
  • Cov(X,Y)\mathrm{Cov}(X, Y)“the covariance of X and Y”
    Whether two random variables move together (positive), oppositely (negative), or neither (zero).
    Cov(X,Y)=E[(X−E[X])(Y−E[Y])]\mathrm{Cov}(X, Y) = \mathbb{E}[(X - \mathbb{E}[X])(Y - \mathbb{E}[Y])]
Quick check +20 XP

Flip two fair coins and let XX be the number of heads, so XX is 0, 1 or 2 with probabilities 0.25, 0.5 and 0.25. What is E[X2]\mathbb{E}[X^2]?

Now watch the law of large numbers happen. Each sample is unpredictable, but their running average xˉn=1n∑i=1nxi\bar{x}_n = \frac{1}{n}\sum_{i=1}^{n} x_i (Chamber 1's bar) is not.

Interactive lab

Sampling and the law of large numbers

Pick a distribution and draw samples x₁, x₂, … from it. The top chart compares your histogram (teal) with the true p(x) (dashed gold). The bottom chart tracks the running mean xₙ = (1/n) Σᵢ xᵢ as n grows. To clear the challenge, reach n ≥ 500 with xₙ within 0.05 of 𝔼[X].
X ∼ Uniform{1, …, 6}𝔼[X] = (1 + 2 + … + 6)/6 = 3.5xₙ = …
00.10.2123456fraction of samplesp(x)
1103.353.53.65running meanvs n (log scale)𝔼[X] = 3.5

Draw samples. The challenge: reach n ≥ 500 with the running mean within 0.05 of 𝔼[X].

Each face 1 to 6 with probability 1/6.

Draw samples

samples

0

mean

…

𝔼[X]

3.5

distance

…

Challenge: Law of large numbersDraw enough samples that the running mean settles within 0.05 of the expectation.+30 XP

Things to try:

  1. With the fair die, draw one sample at a time for a while. The running mean jumps around. Then add a thousand and watch it flatten onto the dashed line at 3.5.
  2. Compare Bernoulli with p=0.5p = 0.5 against the die. Which settles within 0.05 of its expectation faster? The typical distance after nn samples is about σ/n\sigma / \sqrt{n}, so the smaller σ\sigma wins.
  3. Pick the normal distribution and set σ=2\sigma = 2. Check that the histogram's width matches the second argument of N(μ,σ2)\mathcal{N}(\mu, \sigma^2) as the lab writes it: σ2=4\sigma^2 = 4, not 2.

Learn: likelihood and KL divergence

Two more pieces of notation turn probability into training objectives.

Given a dataset D={(xi,yi)}i=1n\mathcal{D} = \{(x_i, y_i)\}_{i=1}^{n} of i.i.d. examples, independence lets the probability of the whole dataset factorise into a product. Read as a function of θ\theta, it's the likelihood:

p(D∣θ)=∏i=1npθ(yi∣xi).p(\mathcal{D} \mid \theta) = \prod_{i=1}^{n} p_\theta(y_i \mid x_i).

Multiplying thousands of numbers below 1 underflows to 0 on a computer, so papers take the log, which turns the product into a sum (Chamber 5):

log⁡p(D∣θ)=∑i=1nlog⁡pθ(yi∣xi).\log p(\mathcal{D} \mid \theta) = \sum_{i=1}^{n} \log p_\theta(y_i \mid x_i).

Maximum likelihood picks θ^=arg max⁡θ∑ilog⁡pθ(yi∣xi)\hat{\theta} = \argmax_\theta \sum_i \log p_\theta(y_i \mid x_i), the argmax of Chamber 7 wearing the hat of Chamber 1. Flip the sign and average, and you have the most common loss in deep learning, the cross-entropy or negative log-likelihood: L(θ)=−1n∑ilog⁡pθ(yi∣xi)\mathcal{L}(\theta) = -\frac{1}{n}\sum_i \log p_\theta(y_i \mid x_i).

Finally, papers often need to say how different two distributions are. The Kullback–Leibler (KL) divergence is

DKL(p ∥ q)=Ex∼p ⁣[log⁡p(x)q(x)]=∑xp(x)log⁡p(x)q(x),D_{\mathrm{KL}}(p \,\Vert\, q) = \mathbb{E}_{x \sim p}\!\left[\log \frac{p(x)}{q(x)}\right] = \sum_x p(x) \log \frac{p(x)}{q(x)},

read “the KL divergence of p from q”. You don't need to compute with it yet, only to read it. Three facts are enough: it's never negative, it's zero exactly when p=qp = q, and it is not symmetric. For a fair coin p=(12,12)p = (\tfrac12, \tfrac12) and a biased one q=(34,14)q = (\tfrac34, \tfrac14), DKL(p ∥ q)≈0.144D_{\mathrm{KL}}(p \,\Vert\, q) \approx 0.144 but DKL(q ∥ p)≈0.131D_{\mathrm{KL}}(q \,\Vert\, p) \approx 0.131. The double bar is only a separator between the two arguments. It has nothing to do with the norm ∥x∥\Vert\mathbf{x}\Vert of Chamber 6.

Likelihood and divergence
  • p(D∣θ)p(\mathcal{D} \mid \theta)“the likelihood, p of D given theta”
    The likelihood: how probable the whole dataset is under parameters θ\theta. For i.i.d. data it's a product.
    p(D∣θ)=∏i=1npθ(yi∣xi)p(\mathcal{D} \mid \theta) = \prod_{i=1}^{n} p_\theta(y_i \mid x_i)
  • log⁡p(D∣θ)\log p(\mathcal{D} \mid \theta)“the log-likelihood”
    The log turns the product into a sum (Chamber 5). Maximising it is maximum likelihood; minimising its negative is the usual cross-entropy loss.
    ∑i=1nlog⁡pθ(yi∣xi)\sum_{i=1}^{n} \log p_\theta(y_i \mid x_i)
  • DKL(p ∥ q)D_{\mathrm{KL}}(p \,\|\, q)“the K L divergence of p from q”
    The Kullback–Leibler divergence: how different qq is from pp. Never negative, zero only when p=qp = q, and not symmetric. The ∥\| is a separator, not a norm.
    DKL(p ∥ q)=Ex∼p[log⁡p(x)q(x)]D_{\mathrm{KL}}(p \,\|\, q) = \mathbb{E}_{x \sim p}\left[\log \frac{p(x)}{q(x)}\right]
DiscoverLearnRead beyondPapers & lecturesYour turn

Read beyond the course

Probability rewards seeing it from several angles: pictures, careful definitions, and the way machine-learning books use it. These are all free.

Interactive · free online · ~30 min

Seeing Theory

Daniel Kunin (Brown University) · Chapters 1–3: Basic Probability, Compound Probability, Probability Distributions

A beautiful visual introduction where you flip coins, roll dice and watch expectations and distributions take shape. Its first chapters cover this chamber's ideas with animations, including the law of large numbers you just watched.

Lecture notes · free online · ~40 min

Review of Probability Theory

Arian Maleki & Tom Do (Stanford CS229) · Sections 1–3

The probability primer handed to Stanford's machine-learning students. Twelve compact pages with Ω, events, pmfs, pdfs, expectation, Bayes' rule and independence in exactly the notation of this chamber. A good test of how much you can now read.

Book · free online · ~60 min

Deep Learning, Chapter 3: Probability and Information Theory

Ian Goodfellow, Yoshua Bengio & Aaron Courville · Sections 3.1–3.9, then 3.11 (Bayes' rule) and 3.13 (information theory, for KL)

The probability every deep-learning paper assumes, in the notation those papers copy. Section 3.13 introduces the KL divergence properly, if you want more than the reading-level treatment here.

Book · free online · ~60 min

Mathematics for Machine Learning, Chapter 6: Probability and Distributions

Marc Peter Deisenroth, A. Aldo Faisal & Cheng Soon Ong · Sections 6.1–6.5

A slower, more mathematical treatment with worked examples: sum and product rules, Bayes' theorem, summary statistics, and the Gaussian in depth. Read Section 6.3 alongside this chamber's derivation of Bayes' rule.

DiscoverLearnRead beyondPapers & lecturesYour turn

Papers and lectures

You met the dropout paper at the top of this chamber. It's long, but the part you need is short. Open it at Section 4, Model Description: one page, with Figure 3 drawing a standard layer next to a dropout layer. Read the equations aloud with this chamber's tables, then read the paragraph beneath them. If you want more, Section 10 compares Bernoulli masks with Gaussian ones and states their mean and variance, which is exactly what you'll prove below.

The GAN paper is a harder read, but its heart is also one page. Read Section 3, Adversarial nets, which ends in Equation 1. Then look at Proposition 1 at the start of Section 4.1, which states the best possible discriminator in one fraction. Skip the convergence proof and the experiments for now.

Dropout: A Simple Way to Prevent Neural Networks from OverfittingNitish Srivastava, Geoffrey Hinton, Alex Krizhevsky, Ilya Sutskever, Ruslan Salakhutdinov · Journal of Machine Learning Research 15, 2014

The full account of dropout: what it does, why it works as a cheap way of averaging many thinned networks, and how to tune it. Section 4 defines every symbol before it uses it, which makes it a good first paper to read closely.

Generative Adversarial NetsIan J. Goodfellow, Jean Pouget-Abadie, Mehdi Mirza, Bing Xu, David Warde-Farley, Sherjil Ozair, Aaron Courville, Yoshua Bengio · NIPS, 2014

The paper that pitted a generator against a discriminator and started a decade of image synthesis. Its objective is written almost entirely in this chamber's notation.

Decode the paper · Section 4 (Model Description), the first two lines of the dropout equations

Dropout: A Simple Way to Prevent Neural Networks from Overfitting

Nitish Srivastava, Geoffrey Hinton, Alex Krizhevsky, Ilya Sutskever, Ruslan Salakhutdinov · JMLR 15, 2014

+30 XP
rj(l)∼Bernoulli(p),y~(l)=r(l)∗y(l)r_j^{(l)} \sim \mathrm{Bernoulli}(p), \qquad \widetilde{\mathbf{y}}^{(l)} = \mathbf{r}^{(l)} * \mathbf{y}^{(l)}

During training, dropout switches off a random subset of units at every step, so the network can't rely on any single unit. These two lines are how the paper says it.

rj(l)r_j^{(l)}
∼\sim
Bernoulli(p)\mathrm{Bernoulli}(p)
pp
r(l)\mathbf{r}^{(l)}
∗*
y~(l)\widetilde{\mathbf{y}}^{(l)}

Options

Decode the paper · Eq. (1), Section 3

Generative Adversarial Nets

Ian J. Goodfellow, Jean Pouget-Abadie, Mehdi Mirza, Bing Xu, David Warde-Farley, Sherjil Ozair, Aaron Courville, Yoshua Bengio · NIPS, 2014

+30 XP
min⁡Gmax⁡DV(D,G)=Ex∼pdata(x)[log⁡D(x)]+Ez∼pz(z)[log⁡(1−D(G(z)))]\min_G \max_D V(D, G) = \mathbb{E}_{\boldsymbol{x} \sim p_{\text{data}}(\boldsymbol{x})}[\log D(\boldsymbol{x})] + \mathbb{E}_{\boldsymbol{z} \sim p_{\boldsymbol{z}}(\boldsymbol{z})}[\log (1 - D(G(\boldsymbol{z})))]

Two networks play a game. The generator GG turns random noise into fake examples; the discriminator DD tries to tell real from fake. This one line is the whole game. Match each piece to its meaning.

min⁡Gmax⁡D\min_G \max_D
D(x)D(\boldsymbol{x})
G(z)G(\boldsymbol{z})
pdatap_{\text{data}}
pzp_{\boldsymbol{z}}
Ex∼pdata(x)\mathbb{E}_{\boldsymbol{x} \sim p_{\text{data}}(\boldsymbol{x})}
log⁡(1−D(G(z)))\log (1 - D(G(\boldsymbol{z})))

Options

Watch

Bayes theorem, the geometry of changing beliefs3Blue1Brown · 15 min

Grant Sanderson derives Bayes' rule from a picture of all the possibilities, drawn as areas. Watch for the idea that evidence restricts the space of possibilities, and that the answer is a proportion of what's left: the same recipe as the definition of P(A∣B)P(A \mid B) above.

DiscoverLearnRead beyondPapers & lecturesYour turn

Your turn

Translate between symbols, words and code. Then prove two facts about expectations and dropout masks, and finish with three problems, the last taken straight from the GAN paper.

Match · Symbols ↔ Read aloud as

Say it aloud

+25 XP

Match each piece of probability notation to the way a researcher would read it.

P(A∣B)P(A \mid B)
P(A,B)P(A, B)
x∼N(0,1)x \sim \mathcal{N}(0, 1)
E[X]\mathbb{E}[X]
pθ(y∣x)p_\theta(y \mid x)
Var(X)\mathrm{Var}(X)
DKL(p ∥ q)D_{\mathrm{KL}}(p \,\|\, q)

Options

Match · Maths ↔ NumPy

From symbols to NumPy

+25 XP

Match each formula to the NumPy that computes or samples it. Assume rng = np.random.default_rng(), that xs is an array of samples, and that a and b are boolean arrays marking which samples fall in events AA and BB. One trap: rng.normal takes the standard deviation σ\sigma, not the variance.

x∼N(μ,σ2)x \sim \mathcal{N}(\mu, \sigma^2)
x1,…,xn∼i.i.d.N(0,1)x_1, \ldots, x_n \overset{\text{i.i.d.}}{\sim} \mathcal{N}(0, 1)
r∼Bernoulli(p)r \sim \mathrm{Bernoulli}(p)
u∼U(0,1)u \sim \mathcal{U}(0, 1)
Ex∼p[f(x)]≈1n∑i=1nf(xi)\mathbb{E}_{x \sim p}[f(x)] \approx \frac{1}{n}\sum_{i=1}^{n} f(x_i)
P(A∣B)≈#(A∩B)#BP(A \mid B) \approx \frac{\#(A \cap B)}{\#B}

Options

Proofs

The puzzle proves a shortcut for variance. You'll need that shortcut in the proof you write yourself, which explains why dropout's rescaled masks leave every unit's output unchanged on average.

Proof puzzle

The variance shortcut

+25 XP

Claim

For any random variable XX with a finite mean, Var(X)=E[X2]−(E[X])2.\mathrm{Var}(X) = \mathbb{E}[X^2] - \big(\mathbb{E}[X]\big)^2.

Tap lines in the order they should appear. Not every line belongs. Tap a line in your proof to send it back.

Your proof

  1. Pick the first line below.

Available lines

Prove it yourself

The mean and variance of a dropout mask

+40 XP

Claim

Let r∼Bernoulli(p)r \sim \mathrm{Bernoulli}(p) with 0<p≤10 < p \le 1. Prove that E[r]=p\mathbb{E}[r] = p and Var(r)=p(1−p)\mathrm{Var}(r) = p(1 - p). Then deduce that the rescaled mask r/pr/p, used by “inverted dropout”, has E[r/p]=1\mathbb{E}[r/p] = 1 and Var(r/p)=1−pp\mathrm{Var}(r/p) = \frac{1 - p}{p}.

Preview

Your typeset proof appears here.

Code it up

Problem 22·Warm-up

The expected maximum

+20 XP

Roll two fair six-sided dice. Let XX and YY be the two results, with X,Y∼i.i.d.U{1,…,6}X, Y \overset{\text{i.i.d.}}{\sim} \mathcal{U}\{1, \ldots, 6\}: each value from 1 to 6 has probability 16\tfrac16, independently.

What is E[max⁡(X,Y)]\mathbb{E}[\max(X, Y)]? Give your answer as a fraction in lowest terms.

An exact integer (or a fraction like 7/12)

Problem 23·Standard

Which coin is it?

+35 XP

A bag holds three coins: a fair coin, a biased coin that lands heads with probability 34\tfrac34, and a two-headed coin. You draw one at random, so each hypothesis h∈{fair,biased,two-headed}h \in \{\text{fair}, \text{biased}, \text{two-headed}\} has prior P(h)=13P(h) = \tfrac13. You flip it nn times and every flip is heads. Call that evidence DnD_n.

Flips are independent given the coin, so P(Dn∣h)=qh nP(D_n \mid h) = q_h^{\,n}, where qhq_h is that coin's probability of heads. Bayes' rule gives the posterior

P(h∣Dn)=P(Dn∣h) P(h)∑h′P(Dn∣h′) P(h′).P(h \mid D_n) = \frac{P(D_n \mid h)\,P(h)}{\sum_{h'} P(D_n \mid h')\,P(h')}.

What is the smallest nn for which P(two-headed∣Dn)>0.99P(\text{two-headed} \mid D_n) > 0.99?

An exact integer (or a fraction like 7/12)

Problem 24·Challenge

The value of the game

+50 XP

Apply the paper. Goodfellow et al.'s GAN value function (Eq. 1) is

V(D,G)=Ex∼pdata[log⁡D(x)]+Ex∼pg[log⁡(1−D(x))],V(D, G) = \mathbb{E}_{x \sim p_{\text{data}}}[\log D(x)] + \mathbb{E}_{x \sim p_g}[\log(1 - D(x))],

written here, as the paper does in its proofs, with the generator's samples drawn straight from its distribution pgp_g. Their Proposition 1 shows that for a fixed generator the best discriminator is

D∗(x)=pdata(x)pdata(x)+pg(x).D^*(x) = \frac{p_{\text{data}}(x)}{p_{\text{data}}(x) + p_g(x)}.

Take a tiny world with x∈{1,2,3,4,5,6}x \in \{1, 2, 3, 4, 5, 6\}. The real data are a fair die, pdata(x)=16p_{\text{data}}(x) = \tfrac16, and the generator is loaded towards high numbers, pg(x)=x21p_g(x) = \tfrac{x}{21}. Using natural logarithms, compute V(D∗,G)V(D^*, G) and give it to 4 decimal places.

A number, rounded to 4 decimal places

Key takeaways

  • Events are sets and PP measures them. P(A,B)P(A, B) means “A and B”, and the bar in P(A∣B)P(A \mid B) means “given”, never “divided by”. Order matters: P(A∣B)≠P(B∣A)P(A \mid B) \ne P(B \mid A).
  • Bayes' rule comes from writing P(A∩B)P(A \cap B) two ways: posterior equals likelihood times prior, over evidence.
  • Capital XX is a random variable and lower-case xx a value. p(x)p(x) is its distribution, p(x,y)p(x, y) a joint, p(y∣x)p(y \mid x) a conditional, and pθ(y∣x)p_\theta(y \mid x) a model. Sum or integrate a variable out to get a marginal.
  • x∼px \sim p means “x is drawn from p”. N(μ,σ2)\mathcal{N}(\mu, \sigma^2) takes a variance, Bernoulli(p)\mathrm{Bernoulli}(p) is a biased coin, and i.i.d. means independent draws from one distribution.
  • Ex∼p[f(x)]\mathbb{E}_{x \sim p}[f(x)] is a probability-weighted average, estimated by averaging samples. It's linear, and variance is E[X2]−(E[X])2\mathbb{E}[X^2] - (\mathbb{E}[X])^2.
  • Likelihoods multiply, log-likelihoods add, and DKL(p ∥ q)≥0D_{\mathrm{KL}}(p \,\Vert\, q) \ge 0 measures how far qq is from pp, in one direction only.

Checkpoint

Prove it to the labyrinth

Answer every question to clear this chamber. First-try answers earn the most XP.

0/8
Question 1 of 8 +20 XP

A paper trains a classifier by maximising log⁡pθ(y∣x)\log p_\theta(y \mid x). What is pθ(y∣x)p_\theta(y \mid x)?

Question 2 of 8 +20 XP

Weights are initialised as w∼N(0,0.04)w \sim \mathcal{N}(0, 0.04). What is the standard deviation of each weight?

Question 3 of 8 +20 XP

A continuous random variable has density p(x)=2.5p(x) = 2.5 at x=0.1x = 0.1. What can you conclude?

Question 4 of 8 +20 XP

Let X∼Bernoulli(0.3)X \sim \mathrm{Bernoulli}(0.3). What is Var(X)\mathrm{Var}(X)?

Question 5 of 8 +20 XP

20% of emails are spam. The word “winner” appears in 50% of spam and in 5% of other emails. An email contains “winner”. What is P(spam∣winner)P(\text{spam} \mid \text{winner})? Give 3 decimal places or a fraction.

Question 6 of 8 +20 XP

A paper assumes its data satisfy x1,…,xn∼i.i.d.px_1, \ldots, x_n \overset{\text{i.i.d.}}{\sim} p. What does that assume?

Question 7 of 8 +20 XP

Why do papers maximise the log-likelihood ∑ilog⁡pθ(yi∣xi)\sum_i \log p_\theta(y_i \mid x_i) instead of the likelihood ∏ipθ(yi∣xi)\prod_i p_\theta(y_i \mid x_i)?

Question 8 of 8 +20 XP

Which statement about DKL(p ∥ q)D_{\mathrm{KL}}(p \,\|\, q) is true?

End of the chamber

Clear this chamber

+60 XPProbabilityConditional Probability and Bayes' RuleExpectationSampling Notation