Ran Wei/ Maths for AI and LLMs
中文
Maths for AI and LLMs · Probability and statistics

Module 2Estimation and held-out evaluation

Fit a probability from observations and measure how uncertain a held-out estimate can be.

65 minYou need Module 1 and the derivative of the logarithm

In this module you will

  • Derive a Bernoulli maximum-likelihood estimate
  • Compute a held-out mean and its standard error under independence
  • Design a split that prevents related examples from leaking across sets
1

Fit a probability

A Bernoulli variable is one with probability pp and zero with probability 1−p.1-p\mathclose{\text{.}} Suppose we observe kk ones in n>0n>0 independent trials with the same unknown p.p\mathclose{\text{.}} The likelihood of a particular ordered observation is pk(1−p)n−k.p^k(1-p)^{n-k}\mathclose{\text{.}} It is a function of pp with the data held fixed. If we only observe the count, the binomial factor (nk)\binom nk multiplies the likelihood and leaves its maximiser unchanged.

For 0<k<n,0<k<n\mathclose{\text{,}} the log-likelihood is kln⁡p+(n−k)ln⁡(1−p).k\ln p+(n-k)\ln(1-p)\mathclose{\text{.}} Setting its derivative k/p−(n−k)/(1−p)k/p-(n-k)/(1-p) to zero gives p^=k/n.\hat p=k/n\mathclose{\text{.}} The second derivative is negative on (0,1).(0,1)\mathclose{\text{.}} If all trials agree, the maximum occurs at 00 or 1.1\mathclose{\text{.}}

Example 2.1Count token completions✓ Checked

A fixed prompt is sampled independently ten times and the first token is AA seven times. The maximum-likelihood estimate of the probability of AA is 0.7.0.7\mathclose{\text{.}}

2

Measure on fresh examples

When training a model, use training data for fitting, validation data for selecting models or settings, and held-out test data for the final evaluation. For tasks with near duplicates or many items from the same source, split by source, such as document or user. Otherwise, the reported performance may reflect memorisation of related items. If the test result is used to select another model, an independently held-out set is needed for the same kind of final comparison.

Let X1,…,XnX_1,\ldots,X_n be independent scores from the same population, with variance σ2.\sigma^2\mathclose{\text{.}} The mean Xˉ\bar X has variance σ2/n.\sigma^2/n\mathclose{\text{.}} Replacing σ\sigma by the sample standard deviation s,s\mathclose{\text{,}} using denominator n−1n-1 in s2,s^2\mathclose{\text{,}} gives the estimated standard error s/n.s/\sqrt n\mathclose{\text{.}} The formula does not account for correlation between tokens in the same document. For document-level evaluation, use independently sampled documents as the units and define explicitly whether the score weights documents or tokens equally.

Compute itInspect a held-out mean

For four independent document scores, print the mean and the estimated standard error. Four observations demonstrate the arithmetic and provide limited evidence about the population.

Python
from math import sqrt
from statistics import mean, stdev

scores = [0.6, 0.8, 1.0, 0.8]
print(f"{mean(scores):.3f}")
print(f"{stdev(scores) / sqrt(len(scores)):.3f}")
Output
0.800
0.082
3

Exercises

Exercise 2.1★Estimate from a count

Find the Bernoulli maximum-likelihood estimate for two successes in eight independent trials.

Show solution✓ Checked

The estimate is 2/8=1/4.2/8=1/4\mathclose{\text{.}}

Exercise 2.2★★Precision and sample size

If the population standard deviation is 0.3,0.3\mathclose{\text{,}} find the standard deviation of the mean for 9 and 36 independent observations.

Show solution✓ Checked

Divide by n.\sqrt n\mathclose{\text{.}} The two values are 0.10.1 and 0.05.0.05\mathclose{\text{.}}

Exercise 2.3★★★Split related items

Twelve documents each supply four paragraphs. Set aside four whole documents for test and two whole documents for validation. How many paragraphs remain for training?

Show solution✓ Checked

Six documents remain for training, so there are 6⋅4=246\cdot4=24 paragraphs. Keep all paragraphs from each document in the same split.

4

Check yourself

1
What is held fixed when viewing a likelihood as a function?
2
Under independence, how does the standard error scale when the sample size quadruples?
3
Which grouping helps prevent leakage across an evaluation split?
Finished this module?