Maths for AI and LLMs · Probability and statistics
Module 1Conditional probability and token sequences
Compute probabilities of token sequences, then use Bayes’ rule to reverse a conditional probability.
60 minYou need Fractions and finite sums
In this module you will
Distinguish joint, marginal and conditional probabilities
Multiply conditional factors to score a sequence
Update a prior after observing a token
1
Probabilities on a finite vocabulary
A language model returns a distribution over its vocabulary at each prefix. All probabilities are nonnegative and the sum over the vocabulary is one. Treat a token as a discrete outcome. In practice a token can be a word fragment, punctuation, or a special symbol. Here we use letters and a stop token.
For events A and B with P(B)>0,P(A∣B)=P(A∩B)/P(B). The denominator restricts the sample space to outcomes with B. The marginal P(A) can be recovered by summing joint probabilities over a partition. For a binary partition, P(A)=P(A∩B)+P(A∩Bc).
2
Score a continuation
Definition 1.1The probability chain rule
P(t1,…,tT)=i=1∏TP(ti∣t1,…,ti−1).
For a given prompt, condition every factor on that prompt as well. Repeatedly expand the probability of a prefix and its next token as a product. Factors for the specified path suffice, even though each factor comes from a distribution over all possible next tokens. For an exact completed sequence, include the probability of the stop token. Stop is unnecessary when the question is whether the sequence begins with a prefix.
Example 1.1Prefix versus completed string✓ Checked
A model gives P(A)=3/5,P(B∣A)=1/4 and P(stop∣AB)=2/3. The probability of beginning with AB is 3/20. The probability of emitting exactly AB and stopping is 1/10.
3
Reverse the condition
Suppose a token was produced by one of two sources, S or T. If P(S)=1/4,P(A∣S)=3/5 and P(A∣T)=1/5, then P(A)=3/10. Bayes’ rule divides the joint probability by that marginal, giving P(S∣A)=(3/20)/(3/10)=1/2.
Try it
In the diagram, let D mean source S and a positive outcome mean token A. Set the three inputs to 25%, 60% and 20%, then read the posterior from the orange column. What happens as the prior for S gets smaller?
Compute itFollow one path
Use exact rational arithmetic for the example’s three factors. Later we will add log probabilities to score much longer paths.
Python
from fractions import Fraction
from math import prod
factors = [Fraction(3, 5), Fraction(1, 4), Fraction(2, 3)]
print(prod(factors[:2]))
print(prod(factors))
Output
3/20
1/10
4
Exercises
Exercise 1.1★Missing probability
After a prefix the vocabulary is A,B,stop. The probabilities of A and B are 0.3 and 0.5. Find the probability of stop.
Show solution✓ Checked
The probabilities sum to one, so stop has probability 0.2.
Exercise 1.2★★The other path
If P(A)=1/4 and P(A∣A)=2/5, find P(AA) as a prefix.
Show solution✓ Checked
The two factors are 1/4 and 2/5, giving 1/10.
Exercise 1.3★★★Posterior odds
Keep the two-source Bayes example above, but observe a token B with P(B∣S)=P(B∣T)=1/5. Find P(S∣B).
Show solution✓ Checked
P(B)=1/5. Its conditional probability is the same under either source, so the common factor cancels and the posterior remains 1/4.
5
Check yourself
1
What is multiplied in the sequence chain rule?
Each factor depends on the preceding tokens.
2
When can you divide by P(B) to define P(A∣B)?
The conditional probability formula requires a positive denominator.
3
What changes when you include a stop token with probability 1/2 at the end of a prefix?
The stop token contributes another conditional factor.