← All tutorials

Probability and Bayes Rule

How chance is written, chained word by word, and flipped by Bayes rule.

On this page

1 A model’s output is a list of chances 2 A random variable is a value chance picks 3 Joint, marginal and conditional 4 Bayes rule flips a conditional 5 Independence and correlation 6 Common traps 7 Naive Bayes and the common shapes 8 References

1 A model’s output is a list of chances

A language model does not output a word. It reads and writes tokens, words or pieces of words, and for the next token it outputs a probability for every choice. Here each token is a whole word, in the toy example from Softmax, Temperature and Decoding.

next word after “the cat sat on the”mat0.431floor0.194sofa0.130bed0.096roof0.064chair0.043moon0.021piano0.010Tuesday0.006of0.004ten chances that add to 1
Figure 1. One step of a language model is a probability for every word.

Each idea in this table is taught in the section listed.

ideasectiontutorials that need it
chances, running totals, averages, spread2keeping only the likeliest words (top-p sampling), analog-to-digital converter (ADC) noise, dropout, GELU, a smooth activation
joint, conditional, chain rule3hidden Markov models (HMM), Gaussian mixtures (GMM), CTC speech recognition, decoding
Bayes rule4naive Bayes, Wiener filter
independence and correlation5naive Bayes, power spectral density (PSD), scaling the attention scores of Transformers

2 A random variable is a value chance picks

Which word comes next is a random variable, a value chance picks. So is whether dropout zeroes a unit, and so is the rounding error of an ADC, the analog-to-digital converter, the chip that turns a microphone voltage into numbers. A list of values gets a chance per value, the PMF (probability mass function).

next wordchancerunning total
mat0.4310.431
floor0.1940.625
sofa0.1300.754
bed0.0960.850
roof0.0640.915
the other five0.0851

An ADC outputs only evenly spaced levels; the gap between neighbouring levels is one step Δ. It rounds each voltage to the nearest level, so its error is equally likely anywhere in a width Δ centred on 0. A value on a line like this gets a PDF (probability density function) instead. Its flat shape and the bell curve are taught in Common Probability Distributions; here only their areas and running totals matter.

Chances add to 1; for a value on a line, the area under the density is 1.
Σx P(x) = 1
Capital X names the random variable and lowercase x one value it can take. P(x) is the chance that X takes the value x; Σx adds it over every value. The ten words add to 1, so when one gets likelier the others lose the same amount.
∫ p(x) dx = 1
∫ … dx measures the area under the density p(x), a height, not a chance. The rounding error is flat at height one over Δ across a width Δ, so its area is 1. Only ranges get chances, so a narrow density can rise above 1. Figure 2 draws the word chances (top) and the flat density (middle).
The running total of chance from the left, the CDF (cumulative distribution function).
F(x) = P(X ≤ x)
x is any value of X, used as a cut point. For a list, F adds the PMF up to x; for a line, it is the area under p up to x. It climbs from 0 to 1 and never goes down. Top-p 0.9, a decoding rule, keeps the likeliest words until their total reaches 0.9: five words, up to roof. The rounding error’s running total reads 0.5 at 0, its centre. The standard bell curve is the hump-shaped density centred on 0 that noise often follows, taught in Common Probability Distributions, section 5. Its running total is written Φ (capital phi), the bottom panel of Figure 2; it reads 0.841 at 1.
next-token chances and their running total0.9matfloorsofabedroofrest1ADC rounding error, one step wideheight 1 over Δ2−0.5Δ00.5Δbell curve and its running total Φ3−2−1012
Figure 2. 1Top-p 0.9 stops at roof. 2F(0) = 0.5. 3Φ(1) = 0.841.

An activation function sits after each layer of a network. ReLU, the simplest, passes positive inputs and zeroes negative ones. GELU, used inside models such as BERT and GPT-2, is a smooth ReLU, taught with the other activations in Multilayer Perceptrons. It keeps 0.841 of an input of 1 and turns −1 into −0.159.

GELU weights each input by the bell’s running total at that input.
GELU(x) = x Φ(x)
Here x is one input value, a number fed to the activation. Φ(x) is the bottom curve of Figure 2: near 0 for very negative x and near 1 for large x. So GELU passes large inputs almost unchanged, nearly zeroes very negative ones and bends smoothly in between, where ReLU has a sharp corner at 0.

In dropout, each unit is dropped with chance 0.1, and a kept unit is divided by 0.9, one minus the drop chance (inverted dropout). Take one unit whose output, its activation, is an illustrative 1.5.

outcomevaluechance
dropped00.1
kept1.5 divided by 0.9, 1.6670.9
average, 0 × 0.1 + 1.667 × 0.91.5
The expected value, also called the mean, is the long-run average: each value times its chance, all added.
E[X] = Σx x P(x)
Each value x is weighted by its chance and the weighted values are added. The table’s average is 1.5, the activation itself and the balance point of Figure 3, so nothing needs rescaling at test time. In training, an expected loss, the error score training pushes down, is estimated by averaging the loss over a batch of examples (cross-entropy, policy gradients).
dropped, chance 0.1kept, chance 0.9balance point, mean 1.5011.667
Figure 3. Each value weighted by its chance; they balance at the mean, 1.5.

Spread is the variance, the average squared distance from the mean; its square root is the standard deviation σ. Attention in Transformers scores a token’s query vector, what it looks for, against another token’s key vector, what it offers, entry by entry.

spread ofmeanvariance
ADC rounding error, illustrative step Δ = 1 mV, one millivolt0Δ² over 12, 0.0833 mV², so σ = 0.289 mV
one query entry times one key entry, independent, each mean 0, variance 101 × 1 = 1 per product; a score sums 64 products, so 64 before dividing by √64 (section 5)
The variance measures how far a value tends to land from its mean.
Var(X) = E[(X − E[X])²]
X − E[X] is how far one value lands from the mean; squaring counts both sides; E averages. The standard deviation σ is its square root. The ADC page calls the table’s 0.0833 mV² the noise power; Bias and Variance splits a model’s error this way. Figure 4 draws the squared distance and its average for the rounding error.
average 0.083 mV²squared distance from the mean−0.500.5σ−σrounding error in mV, equally likely
Figure 4. The average squared distance is the variance; its root is σ = 0.289 mV.

3 Joint, marginal and conditional

A speech or silence detector reads one audio frame, a slice of sound a few tens of milliseconds long, and asks whether it is loud. Write s for speech or silence and o for what is observed, loud or quiet. With illustrative rates, half the frames are speech. A speech frame is loud with chance one half, since speech has pauses, and a silent frame with chance one sixth, from noise bursts.

One table of joint chances gives how often two things happen together, each alone, and one given the other.
P(o) = Σs P(s, o)
P(s, o), the joint, is one cell of the table in Figure 5. Adding a column over s gives the marginal: the chance of loud is 0.333. When s is never seen, this sum is how a model scores what it does see: the hidden s is speech or silence in a hidden Markov model, the cluster that made a point in a Gaussian mixture model (GMM), and the alignment that says which audio frame spells which letter in Connectionist Temporal Classification (CTC), a speech-recognition loss. Expectation maximization (EM) fits models with hidden values like these.
P(o | s) = P(s, o)P(s)
The bar reads given: the conditional keeps one row and rescales it to add to 1. Loud given speech is 0.5, marker 1 in Figure 5. Swapping the roles gives P(s | o): speech given loud is 0.75, marker 2. So P(loud | speech) and P(speech | loud) are different numbers.
P(s, o) = P(o | s) P(s)
The product rule says chance first picks speech or silence, s, then loud or quiet given it, o. Half the frames are speech and half of those are loud, which gives the 0.25 cell.
jointspeech rowloud columnloudquietspeech0.250.25silence0.0830.4170.333sum0.50.5loudquiet0.750.2512
Figure 5. 1P(loud | speech) = 0.5; 2P(speech | loud) = 0.75.

A language model scores a whole reply the same way, one token at a time (decoding). In Figure 1 the four rare words moon, piano, Tuesday and “of” together get about 0.042. If every token had Figure 1’s chances, whatever came before, each would avoid them with chance 0.958. Models often keep the natural log of a chance, written ln. The ln of a number is the power you raise e ≈ 2.718 to in order to get that number, so ln 0.431 is −0.84.

replychance
“mat” after “the cat sat on the”0.431, ln −0.84
20 tokens with no rare word0.95820 = 0.42
100 tokens with no rare word0.958100 = 0.014
The chain rule of probability builds a joint one variable at a time, each given everything before it.
P(w1, …, wT) = P(w1) P(w2 | w1) ⋯ P(wT | w1, …, wT−1)
wt is token t of T. The dots … stand for the tokens in between and ⋯ for the factors in between. Each factor is the product rule again, and the history behind the bar grows by one token per factor. Many chances below 1 multiplied together shrink fast: the chance of a reply that avoids every rare word falls from 0.42 at 20 tokens to 0.014 at 100. The log of the product is a sum of per-token logs, so models add log chances, like −0.84 for mat, instead of multiplying tiny numbers. Figure 6 draws the fall.
0.42 at 200.014 at 1000501001chance of no rare wordreply length in tokens
Figure 6. Multiplying 0.958 once per token shrinks the chance fast.

Models cut the history in one of three ways.

history keptfactorwhere
all of itP(wt | w1 … wt−1)a language model (decoding)
one step backP(wt | wt−1)a bigram model, which looks one word back; the hidden states of an HMM
noneP(wt)independent data points, which do not affect each other (section 5), so the joint is a product and its log a sum (likelihood, naive Bayes)

Backpropagation has a chain rule too, the calculus one for derivatives: same name, different rule.

4 Bayes rule flips a conditional

An always-on keyword spotter listens for a wake word, often with a small gated recurrent network (gated RNN). Each firing without the word is a false alarm, and on-device spotters are judged by false alarms per hour. When the spotter fires, is the word there? Write A for the cause, the word, and B for the evidence, a firing. The table’s rates are illustrative.

piecevalue
the word is in 1 clip in 100, P(A)0.01
the spotter fires on a clip with the word, P(B | A)0.9
it fires on a clip without it, P(B | not A)0.09
firings with the word, 0.9 × 0.010.009
false alarms, 0.09 × 0.990.0891
share of firings that hold the word, 0.009 out of 0.009 + 0.08910.092
Bayes rule turns the chance of the evidence given a cause into the chance of the cause given the evidence.
P(A | B) = P(B | A) P(A)P(B)
P(B | A), the likelihood, is how often the spotter fires when the word is there. P(A), the prior, is how common the word is before any firing. P(A | B), the posterior, is the product rule written both ways and solved for the cause. A rare word keeps the numerator small, so the posterior is only the table’s 0.092; Figure 7 counts it out in clips.
P(B) = P(B | A) P(A) + P(B | not A) P(not A)
The denominator adds every way to see a firing: real firings with the word, then false alarms without it, in gray as in Figure 7. With 99 clips in 100 empty, the false alarms outnumber the real firings about ten to one.

To pick only the most likely cause, compare the numerators, since the denominator is the same for every cause. Naive Bayes picks the topic of a forum post, its class, this way, by an argmax, the choice with the largest score, the rule greedy decoding uses for words.

Counting clips instead of chances, the frequency formats of Gigerenzer and Hoffrage (1995), makes the flip visible.

Figure 7. 1,000 clips; the bar splits the firings.

A rare cause needs strong evidence. Put the speech or silence detector of section 3 in a quiet room where only 1 frame in 100 is speech (illustrative). The odds of speech are its chance divided by the chance of silence, and each loud frame multiplies them by 3, since a speech frame is loud with chance one half and a silent frame with chance one sixth. Each frame is scored on its own here; an HMM lets speech or silence carry over from step to step. The odds start at 1 to 99; dividing the odds by 1 plus the odds gives the chance back.

loud framesodds of speech, times 3 each framechance of speech
00.01011%
10.0303%
20.0918%
30.2721%
40.8245%
52.4571%
one half3%18%221%345%471%5loud frames in a row1
Figure 8. Speech chance after loud frames. 1After five, speech is likelier.

The snippet computes the keyword table’s posterior. prior is the chance of the word, fire_if_word and fire_if_none are the two firing rates, and fire is the denominator.

prior = 0.01
fire_if_word = 0.90
fire_if_none = 0.09
fire = fire_if_word*prior + fire_if_none*(1-prior)
posterior = fire_if_word*prior/fire  # 0.092
  1. 1The prior, the likelihood and the false-alarm rate of Figure 7.
  2. 2Every way to fire, the Bayes card’s second line.
  3. 3The posterior, the Bayes card’s first line.

5 Independence and correlation

Dropout draws each unit’s keep mask, a 1 that keeps the unit or a 0 that zeroes it, on its own (Srivastava et al. 2014). Knowing one mask says nothing about the other, so the two masks are independent.

Two words in a post are not independent. The 20 Newsgroups dataset collects posts from Usenet topic forums, such as comp.windows.x, about the windowing system of Unix computers, and rec.sport.hockey. “Port” and “motif”, a window toolkit, both show up in comp.windows.x posts. Take 20 illustrative posts, 10 from each forum. In the table, X and Y are a pair’s two yes-or-no variables, unit 1 and unit 2 kept, or port and motif in a post, and x and y are their yes values.

two dropout masks0.9unit 1 kept0.9unit 1 droppedpurple share is unit 2 keptall 20 posts0.5has port0.21no portgreen share has motifcomp.windows.x only0.6has port0.6no port12
Figure 9. Equal rows mean independent. 1Dependent. 2Independent in one topic.
pairP(x) P(y)P(x, y)
two keep masks0.9 × 0.9 = 0.810.81
port and motif, all 20 posts0.3 × 0.3 = 0.090.15
port and motif, comp.windows.x only0.5 × 0.6 = 0.30.3
Independence turns a whole table into two short lists, because the joint becomes a product.
P(x, y) = P(x) P(y)
Knowing X no longer changes the chances of Y, so every row of the table has the same shape. The two masks pass, the matching rows of Figure 9. Port and motif across all 20 posts fail, 0.15 against 0.09, so they are dependent.
P(x, y | c) = P(x | c) P(y | c)
c is the class. When the product holds inside each class, X and Y are conditionally independent given c, as port and motif are within comp.windows.x. Naive Bayes assumes this for all words; CTC treats each frame’s prediction as independent of the others given the audio.

Correlation asks a weaker question, whether two values rise together along a straight line. Take one second of a 440 Hz tone, 440 cycles per second, sampled at 16 kHz, 16,000 numbers per second.

paircorrelationindependent
a sample and the next one0.985no; a signal against its own shifted copy is its autocorrelation
the tone and microphone noiseabout 0yes
the tone and its square0no, the tone fixes its square
next sample1mic noise2its square3
Figure 10. Correlation 10.985, 2about 0, 30, yet the tone fixes its square exactly.

Sums also need the covariance: how much two values rise and fall together along a line.

sumvariance
a query-key score, the dot product of a query and a key of length dk = 64, so 64 products with covariance 0 between them, each of variance 164, so σ = 8; divided by √64 = 8, back to 1
illustrative pair, variances 1 and 1, covariance 0.53, not 2
Covariance measures how two values move together; correlation is covariance without units; covariance decides how variances add.
Cov(X, Y) = E[(X − E[X])(Y − E[Y])]
Each bracket is a distance from its own mean; the product is positive when both land on the same side. Var(X) is Cov(X, X).
ρ = Cov(X, Y)σX σY
Dividing by both standard deviations gives the correlation ρ (rho), from −1 to 1. It is the cosine between the samples of X and the samples of Y, each list with its mean subtracted. Independent implies ρ = 0; ρ = 0 does not imply independent (Goodfellow et al. 2016, sec. 3.8). Figure 10 shows all three cases.
Var(X + Y) = Var(X) + Var(Y) + 2 Cov(X, Y)
When X and Y are uncorrelated, the covariance term is 0, so the variances simply add: the 64 products give 64. That is why attention divides its scores by √dk, the square root of the key length (Vaswani et al. 2017).

Three methods on the site act on correlation.

methodwhat it does
principal component analysis (PCA) whiteningrotates and rescales data so every direction has variance 1 and no correlation; it misses the tone-and-square link
independent component analysis (ICA)finds sources that are fully independent, not only uncorrelated
mel-frequency cepstral coefficients (MFCC)a discrete cosine transform (DCT) mostly removes the correlation between neighbouring mel bands, the overlapping frequency bands of a spectrum

The snippet checks the three correlations on one second of samples.

import numpy as np
n = np.arange(16000)
tone = np.sin(2*np.pi*440*n/16000)
rng = np.random.default_rng(1)
noise = rng.standard_normal(16000)
def rho(a, b):
    return np.corrcoef(a, b)[0, 1]
r1 = rho(tone[:-1], tone[1:])  # 0.985
r2 = rho(tone, noise)          # about 0
r3 = rho(tone, tone**2)        # 0
  1. 1One second of the tone at 16 kHz and a second of noise.
  2. 2The correlation ρ, the covariance card’s second line, read from np.corrcoef’s 2 × 2 table.
  3. 3The three rows of the correlation table.

The bell curve and the tilted two-variable cloud are taught in Common Probability Distributions.

6 Common traps

7 Naive Bayes and the common shapes

Put a post’s words in the evidence slot, its topic in the cause slot and add conditional independence, and you have Naive Bayes; the Maximum Likelihood and EM page fits the chances. The few shapes those chances take are in Common Probability Distributions.

Bayes rule piecein naive Bayes
prior P(A)the share of posts in each topic
likelihood P(B | A)each word’s chance given the topic, multiplied over the post’s words, by conditional independence
posterior P(A | B)each topic’s chance after reading the words; the argmax is the predicted topic

8 References