Probability
Conditional probability, Bayes' theorem, and why a model with 99 percent accuracy can still be wrong most of the time it raises an alarm.
The idea
Probability measures ignorance, not randomness. When a model says a document is "87 percent an invoice", it means: among all documents that look like this one, 87 percent were invoices in the training data.
What it is good for
Every language-model output is a probability distribution over the whole vocabulary. Every classification is a distribution over classes. Being able to read those numbers is how you notice when a model is guessing.
The example that explains everything
A checking system detects fraudulent invoices with 99 percent sensitivity and a 1 percent false-alarm rate. Out of 10,000 invoices, 10 are fraudulent.
| actually fraud | actually fine | total | |
|---|---|---|---|
| System raises alarm | 9.9 | 99.9 | 109.8 |
| System stays silent | 0.1 | 9,890.1 | 9,890.2 |
| Total | 10 | 9,990 | 10,000 |
Of 109.8 alarms, 9.9 are real. That is 9 percent. Nine out of ten alarms are false, even though the system is 99 percent "correct".
def posterior(prevalence, sensitivity, specificity):
tp = prevalence * sensitivity
fp = (1 - prevalence) * (1 - specificity)
return tp / (tp + fp)
print(round(posterior(0.001, 0.99, 0.99), 4)) # 0.0902
print(round(posterior(0.100, 0.99, 0.99), 4)) # 0.9167Same model quality, two orders of magnitude difference in usefulness.
The formulas
Where this sits inside the model
An autoregressive language model factorises the probability of a whole sequence using the chain rule of probability:
In practice this is computed with logarithms, because a product of thousands of
numbers below one underflows to zero in float32 immediately. The product
becomes a sum of log probabilities, and the mean negative log probability
becomes perplexity, see
Information and entropy.
Measuring calibration
Expected Calibration Error buckets predictions by confidence and compares the mean stated confidence per bucket with the actual hit rate in it. An ECE above 0.1 means the model's confidence figures are unusable as a basis for decisions and need recalibrating, for example by temperature scaling on a validation set.
Related courses and sources
Mathematics for Machine Learning
Exactly the mathematics machine learning needs and none of the rest. Linear algebra, calculus and probability in one volume, free as a PDF.
For anyone who wants exactly the mathematics machine learning needs and no more.
Probabilistic Machine Learning
Kevin Murphy's volumes, building machine learning consistently out of probability theory. Extensive and freely available.
For anyone wanting machine learning built consistently out of probability theory.