Ran Wei/ Maths for AI and LLMs
中文
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 AA and BB with P(B)>0,P(B)>0\mathclose{\text{,}} P(A∣B)=P(A∩B)/P(B).P(A\mid B)=P(A\cap B)/P(B)\mathclose{\text{.}} The denominator restricts the sample space to outcomes with B.B\mathclose{\text{.}} The marginal P(A)P(A) can be recovered by summing joint probabilities over a partition. For a binary partition, P(A)=P(A∩B)+P(A∩Bc).P(A)=P(A\cap B)+P(A\cap B^\mathsf c)\mathclose{\text{.}}

2

Score a continuation

Definition 1.1The probability chain rule
P(t1,…,tT)=∏i=1TP(ti∣t1,…,ti−1).P(t_1,\ldots,t_T)=\prod_{i=1}^T P(t_i\mid t_1,\ldots,t_{i-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(A)=3/5\mathclose{\text{,}} P(B∣A)=1/4P(B\mid A)=1/4 and P(stop∣AB)=2/3.P(\mathrm{stop}\mid AB)=2/3\mathclose{\text{.}} The probability of beginning with ABAB is 3/20.3/20\mathclose{\text{.}} The probability of emitting exactly ABAB and stopping is 1/10.1/10\mathclose{\text{.}}

3

Reverse the condition

Suppose a token was produced by one of two sources, SS or T.T\mathclose{\text{.}} If P(S)=1/4,P(S)=1/4\mathclose{\text{,}} P(A∣S)=3/5P(A\mid S)=3/5 and P(A∣T)=1/5,P(A\mid T)=1/5\mathclose{\text{,}} then P(A)=3/10.P(A)=3/10\mathclose{\text{.}} Bayes’ rule divides the joint probability by that marginal, giving P(S∣A)=(3/20)/(3/10)=1/2.P(S\mid A)=(3/20)/(3/10)=1/2\mathclose{\text{.}}

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.A,B,\mathrm{stop}\mathclose{\text{.}} The probabilities of AA and BB are 0.30.3 and 0.5.0.5\mathclose{\text{.}} Find the probability of stop.

Show solution✓ Checked

The probabilities sum to one, so stop has probability 0.2.0.2\mathclose{\text{.}}

Exercise 1.2★★The other path

If P(A)=1/4P(A)=1/4 and P(A∣A)=2/5,P(A\mid A)=2/5\mathclose{\text{,}} find P(AA)P(AA) as a prefix.

Show solution✓ Checked

The two factors are 1/41/4 and 2/5,2/5\mathclose{\text{,}} giving 1/10.1/10\mathclose{\text{.}}

Exercise 1.3★★★Posterior odds

Keep the two-source Bayes example above, but observe a token BB with P(B∣S)=P(B∣T)=1/5.P(B\mid S)=P(B\mid T)=1/5\mathclose{\text{.}} Find P(S∣B).P(S\mid B)\mathclose{\text{.}}

Show solution✓ Checked

P(B)=1/5.P(B)=1/5\mathclose{\text{.}} Its conditional probability is the same under either source, so the common factor cancels and the posterior remains 1/4.1/4\mathclose{\text{.}}

5

Check yourself

1
What is multiplied in the sequence chain rule?
2
When can you divide by P(B)P(B) to define P(A∣B)?P(A\mid B)\mathclose{\text{?}}
3
What changes when you include a stop token with probability 1/21/2 at the end of a prefix?
Finished this module?