Probability & Statistics for AI
Models don't produce certainties — they produce probability distributions. Understanding probability is understanding what a model's output actually means, and statistics is how you tell whether a model's improvement is real or just noise.
A model rarely just says "yes" or "42" — it says "I'm 73% sure it's yes," or hands back a whole spread of plausible answers with different likelihoods attached. Probability is the language for describing that spread precisely, and for updating it correctly when new evidence shows up (that's Bayes' theorem — the math version of "huh, that changes things"). Statistics is the second half: once you've collected real data and computed a number from it — a p-value, an average, a model's accuracy — how do you know that number reflects something real, rather than being exactly what you'd expect from random noise alone? Both halves matter for the same reason: every model you build outputs a distribution, was trained by minimizing something derived from probability theory, and needs its claimed improvements checked against noise before anyone should trust them.
Everything below makes both halves exact.
Intuition: What a Model's Output Means, and Whether to Trust a Number
Two threads run through this whole page. The first: a model's output is a distribution, not an answer — everything from Bayes' theorem to entropy is about how to reason correctly with that distribution rather than pretending it's a single certain fact. The second: any number computed from a finite sample of data (a p-value, a confidence interval, a correlation) can mislead you in specific, well-understood ways — the second half of this page is about the tools (bootstrap, permutation tests, causal reasoning) that keep you honest about what a number from data actually licenses you to conclude.
Random Variables and Distributions
A random variable is a quantity whose value depends on chance. Its distribution describes how likely each possible value is. Pick a distribution and adjust its real parameters below:
- Bernoulli: a single yes/no outcome (e.g. "is this email spam?") — the distribution behind binary classification.
- Binomial: the number of successes in independent Bernoulli trials.
- Gaussian (Normal): the classic bell curve, defined by mean and variance . Appears everywhere — weight initialization, noise modeling, the assumed error distribution in linear regression.
- Categorical: a generalization of Bernoulli to more than two outcomes — this is exactly what an LLM's output layer produces: a probability distribution over every possible next token.
Expectation, Variance, Covariance
- Expectation : the long-run average value of a random variable — the "center of mass" of its distribution.
- Variance : how spread out the values are.
- Covariance : whether two variables move together (positive), oppositely (negative), or independently (near zero).
- Correlation: covariance normalized to , making it comparable across different variables' scales.
Why it matters: the bias-variance tradeoff (see Machine Learning) is literally a statement about the variance of a model's predictions across different training sets.
Bayes' Theorem
Lets you update a belief (, the prior) given new evidence () to get an updated belief (, the posterior). Real numbers, the classic base-rate case:
This single equation underlies Naive Bayes classifiers, Bayesian optimization (used for hyperparameter tuning), and the conceptual framing of how RAG systems should ideally combine a model's prior knowledge with retrieved evidence.
MLE vs MAP
- Maximum Likelihood Estimation (MLE): pick the model parameters that make the observed data most probable. This is what "minimizing cross-entropy loss" actually is under the hood — cross-entropy loss minimization is MLE for a categorical distribution.
- Maximum A Posteriori (MAP): like MLE, but also weighs in a prior belief about what parameters are reasonable.
Real estimates from a real (small, adjustable) coin-flip dataset:
Adding L2 regularization to a loss function is mathematically equivalent to doing MAP estimation with a Gaussian prior on the weights — the same shrinkage-toward-a-default-belief visible above, just applied to millions of weights instead of one coin bias.
Central Limit Theorem
The average of a large number of independent random variables tends toward a Gaussian distribution, regardless of the original variables' distribution. Watch it happen live, starting from a genuinely non-Gaussian source:
This is why so much of statistics gets to assume normality, and why metrics averaged over large evaluation sets behave predictably even when individual examples are noisy.
Hypothesis Testing & Significance
- Null hypothesis (): the "nothing changed" baseline assumption — e.g. "the new model's conversion rate equals the old one's."
- p-value: the probability of seeing a result at least this extreme if the null hypothesis were true. Real permutation-test p-value, computed from the real empirical null distribution rather than a formula:
A p-value is not "the probability the null hypothesis is true" — that's the single most common misinterpretation, and worth internalizing precisely: it's a statement about how surprising the data would be under , not a statement of probability about the hypothesis itself.
- Confidence intervals: a range constructed from the observed data such that, if you repeated the experiment many times, that construction procedure would contain the true value some target fraction (e.g. 95%) of the time. A 95% CI that excludes zero for a lift metric is the interval-based equivalent of a p-value below 0.05.
- Statistical power: the probability of correctly detecting a real effect when one truly exists. Power depends on sample size, effect size, and significance threshold — an underpowered A/B test can run for weeks and still correctly fail to find a real 2% lift simply because too few users saw it. Compute the required sample size before running an experiment, not after it "didn't reach significance."
- A/B testing: the practical application — before shipping a new model to 100% of users, you run it against a control group and use hypothesis testing to confirm the improvement is real, not random variation.
Bootstrap and Permutation Tests
Two resampling techniques that estimate uncertainty without assuming a particular distribution (like the Gaussian assumption behind many classical formulas) — useful exactly when a metric's theoretical sampling distribution is unknown or intractable (e.g. the median, or a custom business metric).
Bootstrap: resample the observed data with replacement many times, compute the metric of interest on each resample, and use the spread of those computed values as an empirical confidence interval. Real 1000-resample run:
If you've ever seen a metric reported with error bars from "1000 bootstrap resamples," this is exactly what produced them — and it's the same resampling-with-replacement idea bagging methods like Random Forest use internally, applied here to uncertainty estimation instead of ensembling.
Permutation test (the mechanism behind the p-value diagram above): to test whether two groups genuinely differ, randomly shuffle ("permute") the group labels many times, recomputing the difference in means each time. This builds an empirical null distribution directly from the data itself, with no distributional assumptions.
Causal Inference & Sampling Bias
Correlation is not causation is the single most important caveat in applied statistics. Watch a real spurious correlation appear from a shared confounder, and disappear under real randomization:
- Confounders: a variable that influences both the presumed cause and the presumed effect, creating a spurious correlation between them (e.g. ice cream sales and drowning deaths both rise with summer heat — heat is the confounder, ice cream doesn't cause drowning).
- Randomized controlled trials (RCTs): randomly assigning subjects to treatment/control is what breaks confounding — with random assignment, any systematic pre-existing difference between groups is (in expectation) eliminated, which is exactly why A/B testing (a form of RCT) is trusted to establish causation, while purely observational comparisons ("users who used feature X had higher retention") are not, without further assumptions.
- Sampling bias: when the data collected systematically over- or under-represents part of the population you actually care about — e.g. training a model on logged data from users who opted in to a feature necessarily excludes everyone who didn't, biasing conclusions about the feature's general effect. This is a silent, common failure mode distinct from data leakage (see ML Workflow Fundamentals): leakage contaminates training with future information, sampling bias means the training distribution was never representative of the true population to begin with.
Entropy, Cross-Entropy, and KL Divergence
- Entropy : measures the inherent uncertainty in a distribution.
- Cross-entropy : measures how well a predicted distribution matches the true distribution .
- KL divergence : measures how much information is lost when is used to approximate .
Drag a predicted distribution toward (or away from) the true and watch all three real quantities respond:
This is the loss function used to train virtually every classifier and every LLM — next-token prediction is a giant cross-entropy minimization over the vocabulary distribution. KL divergence shows up in variational autoencoders, in RLHF (constraining the fine-tuned policy from drifting too far from the base model), and in DPO's loss formulation.
Code: A Real Bootstrap Confidence Interval
The exact procedure the bootstrap diagram above runs:
Where this shows up in the rest of the curriculum
| Concept | Used in |
|---|---|
| Cross-entropy | Loss function for classifiers and every LLM |
| Bayes' theorem | Naive Bayes, Bayesian hyperparameter search |
| MLE / MAP | Why cross-entropy loss = MLE; why L2 regularization = MAP |
| KL divergence | RLHF/DPO constraints, VAEs |
| Hypothesis testing | A/B testing new models in production |
| Bootstrap / permutation tests | Distribution-free confidence intervals and significance tests |
| Causal inference | Distinguishing "correlated with" from "caused by" in production data |
Next: Algorithms & Data Structures — the CS fundamentals that show up in every ML engineering interview alongside the math.