Probability and Statistics for AI — A Model's Output Is a Distribution
Classifiers and language models do not return answers; they return probability distributions. Distributions, expectation, conditional probability and Bayes explained from the symbols up — building to the payoff: why maximum likelihood is where loss functions come from. Cross-entropy and MSE were derived, not invented.
Key points
- A model outputs a probability distribution, not an answer; softmax makes scores non-negative and sum to one.
- Expectation is an average weighted by how likely each value is; E in a paper means "average this over the data".
- A language model chains conditional probabilities: the probability of a whole passage is the product of next-token probabilities.
- Taking maximum likelihood, adding a log and a minus sign gives cross-entropy for classification and squared error (MSE) for regression under a normal-distribution assumption.
- Temperature divides the logits by T before conversion to probabilities to reshape the distribution, while top-p cuts the tail by cumulative probability; they are different operations.
The goal: being able to read this equation
When a paper writes down what it is training for, it almost always uses this shape.
Written out in words it says: show the model some data, ask how much probability it put on the correct answer, take the log of that number, average over everything you showed it, and flip the sign. is the model's parameters, is an average, is where the data comes from, means "drawn from", is the probability the model assigns to having seen , and the vertical bar reads "given".
The notation is unfamiliar; the content is not. And once this line is readable, classification losses and language-model losses turn out to be the same equation wearing different clothes.
What a model returns is not an answer
Nothing else works until this is dislodged. A classifier does not say "cat". It returns a probability for every class it knows — cat 0.82, dog 0.15, other 0.03. The interface shows you the largest entry, which is why it looks like a single answer.
Language models do the same thing. For every next token, they build a bar chart over the entire vocabulary, tens of thousands of entries wide. After "the capital of Japan is", perhaps Tokyo 0.71, Osaka 0.04, and so on down the list.
The function that builds that chart is softmax.
is the raw score the model emits — the logit, which can perfectly well be negative — is the exponential, and the denominator sums over every entry.
The fraction, in words, does two things: make every score positive without disturbing which one is largest, then rescale the whole set so it adds up to one. Calling something a probability requires exactly two properties, non-negative and summing to one; delivers the first, the division delivers the second. There is no deeper philosophy in it.
Expectation: the formal way to write "average"
is a quantity whose value is decided by chance and is the probability of it taking value .
Put in words, the sum is an average in which every possible value is weighted by how likely that value is. If a quantity is 1 with probability 90% and 100 with probability 10%, its expectation is not the naive 50.5 but .
When appears in a paper, reading it as "average this over the data" will be right nine times out of ten. Implementations cannot afford the full dataset every step, so they substitute the mini-batch average. That is what the in equation (1) becomes in code.
Conditional probability: what a language model is doing
is the probability of given that has happened. Predicting umbrella sales cold is hard; predicting them given "it is raining" is a different exercise. Conditional probability is the notation for "new information changes the outlook".
A language model is that idea wired in series.
("pi") means "multiply all of these", is the -th token, and means every token before it.
It is one line which says: the probability of a whole passage is the product of, at each position, the probability of that token given everything written so far. A GPT-style model computes only the individual factors on the right; generation is drawing from them one at a time. This is what "it just predicts the next word" actually refers to.
Bayes' theorem: updating belief with evidence
is a hypothesis (the model's parameters) and is the data observed. Each of the four pieces has a name. is the prior — what you believed before looking. is the likelihood — how well that hypothesis accounts for what you saw. is the posterior — what you believe afterwards. is the division that makes everything sum to one.
Read in words, the fraction says: take the belief you started with, multiply it by how well that belief accounts for what you just saw, and what comes out is the belief you should hold now. The medical-testing example makes the point stick. A test that is 99% accurate returns positive; if the disease affects one person in ten thousand, the chance you actually have it is far below a coin flip, because a tiny prior is not easily overturned by a 99% likelihood. Ignore the prior and you will reason badly — that is nearly the whole practical payload of Bayes' theorem.
Inside machine learning, regularization reads naturally in this frame: L2 regularization, which pulls weights toward zero, corresponds to putting a prior that says weights ought to live near zero.
The payoff: maximum likelihood becomes the loss function
Here is the centre of the article. Loss functions were not chosen by taste. They fall out of probability.
You hold data points. Under a model with parameters , write the probability of observing that entire dataset.
In symbols it is one long product; in words it says multiply together, across all examples, the probability the model handed to the correct answer. This is the likelihood, and the plan it suggests is the obvious one — pick the that makes the data you actually observed as likely as possible. That is maximum likelihood estimation.
A product is unusable, though. Multiply 0.9 by itself a thousand times and the number underflows to nothing, and differentiating products is tedious. So take the log, which is monotonically increasing and therefore does not move the maximizing . Then flip the sign, because the optimization world is standardised on minimization.
What this says is: sum the log of the probability assigned to each correct answer, and negate. The product has become a sum, and nothing else changed.
Divide by and you have equation (1) exactly. In classification this quantity already has a name — cross-entropy. The formula introduced as "the default for classification" in Loss Functions and Optimization was never invented. It came out of maximum likelihood.
Run the same argument for regression and something more surprising appears. Assume the truth is scattered around the prediction in a normal distribution:
is the model's prediction and ("sigma") is the spread. The assumption being expressed is: the true value sits in a bell curve centred on whatever the model predicted.
Take the log and the only part that depends on is . Negate it and minimize, and what remains is squared error.
Which means choosing MSE is the same act as assuming your errors are normally distributed. MSE misbehaves on outlier-heavy data precisely because the normal distribution asserts that large deviations essentially never happen. Choosing a loss function is choosing an assumption about how your data was generated.
Bias and variance: error split three ways
is what the model predicted and is the truth, so the left-hand side is simply the average squared miss. The claim is that that one number is really three added together. Bias is the systematic gap left by a model too simple to represent the truth. Variance is how much the answer thrashes when you resample the training data. Noise is the part no model can remove.
High bias is underfitting, high variance is overfitting, and how to measure either is in Overfitting and Evaluation Design.
Sampling: what temperature and top-p actually do
Once you have a distribution you still have to pick something from it. This is where temperature enters.
is the temperature, and the only change is that the logits are divided by before being turned into probabilities. Small magnifies the gaps between scores and the distribution spikes; large compresses them and it flattens. As approaches zero you get greedy decoding, always taking the maximum; raise it and rare tokens start surfacing. That is the peakedness you were dragging around in the figure above.
Top-p (nucleus sampling) is a different operation entirely. Sort by probability, accumulate until the running total passes , cut there, and re-draw among the survivors. Top-k truncates by count instead. Both keep bizarre tail tokens from being drawn, which is a distinct job from temperature reshaping the distribution itself. Low temperature when you want facts, higher when you want options — but move temperature and top-p hard at the same time and you will have no idea which one did anything.
Three things that matter in practice
1. "Probability 0.9" does not mean "90% correct" The number is a statement of confidence, not a guarantee about accuracy. A model is calibrated when, among the cases it scored 0.9, about nine in ten really are right. If a threshold gates automated behaviour, measure that correspondence on your own data before trusting it.
2. Never take the log of a probability you just computed runs to infinity, so applying to softmax output is numerically brittle. Use the function that consumes logits directly — the log-softmax and logits-based losses your framework already ships.
3. Compare log-likelihood per token, not in total The sum grows with sequence length, so totals are meaningless across datasets of different lengths. Dividing by token count first removes a great deal of confusion.
Summary
- A model outputs a distribution, not an answer; softmax is the device that makes scores non-negative and sum to one
- Expectation is a likelihood-weighted average; in a paper means "average over the data"
- Language models are chained conditional probabilities: the probability of a passage is the product of next-token probabilities
- Bayes multiplies prior belief by explanatory power; ignoring the prior is how confident people reach wrong conclusions
- Take maximum likelihood, add a log and a minus sign, and out comes cross-entropy for classification — and MSE for regression under a normality assumption
- Temperature reshapes the distribution, top-p truncates its tail; different jobs, so change one at a time
Probability is not the only door to loss functions. There is a second, entirely separate entrance — information theory — and it arrives at the identical equation (Information Theory and AI).
Comments
Sign in to comment