Learn › Probability & Statistics
·FREE·8 min read

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.
ModalitytextTaskmath

The goal: being able to read this equation

When a paper writes down what it is training for, it almost always uses this shape.

L(θ)=− E(x,y)∼D[log⁡pθ(y∣x)]\mathcal{L}(\theta) = -\,\mathbb{E}_{(x,y)\sim\mathcal{D}}\big[\log p_{\theta}(y \mid x)\big]
(1)

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. θ\theta is the model's parameters, E\mathbb{E} is an average, D\mathcal{D} is where the data comes from, ∼\sim means "drawn from", pθ(y∣x)p_{\theta}(y \mid x) is the probability the model assigns to yy having seen xx, and the vertical bar ∣\mid 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.

pi=exp⁡(zi)∑jexp⁡(zj)p_i = \frac{\exp(z_i)}{\sum_{j} \exp(z_j)}
(2)

ziz_i is the raw score the model emits — the logit, which can perfectly well be negative — exp⁡\exp 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; exp⁡\exp delivers the first, the division delivers the second. There is no deeper philosophy in it.

FIG 1The same logits, reshaped. Lower the temperature and the distribution spikes onto one option; raise it and the bars flatten out. Peaked means confident, flat means undecided — and that peakedness turns out to matter enormously later

Expectation: the formal way to write "average"

E[X]=∑xx p(x)\mathbb{E}[X] = \sum_{x} x\,p(x)

XX is a quantity whose value is decided by chance and p(x)p(x) is the probability of it taking value xx.

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 1×0.9+100×0.1=10.91 \times 0.9 + 100 \times 0.1 = 10.9.

When E\mathbb{E} 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 E\mathbb{E} in equation (1) becomes in code.

Conditional probability: what a language model is doing

p(y∣x)p(y \mid x) is the probability of yy given that xx 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.

p(x1,x2,…,xT)=∏t=1Tp(xt∣x<t)p(x_1, x_2, \ldots, x_T) = \prod_{t=1}^{T} p(x_t \mid x_{<t})
(3)

∏\prod ("pi") means "multiply all of these", xtx_t is the tt-th token, and x<tx_{<t} 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

p(θ∣D)=p(D∣θ) p(θ)p(D)p(\theta \mid D) = \frac{p(D \mid \theta)\,p(\theta)}{p(D)}
(4)

θ\theta is a hypothesis (the model's parameters) and DD is the data observed. Each of the four pieces has a name. p(θ)p(\theta) is the prior — what you believed before looking. p(D∣θ)p(D \mid \theta) is the likelihood — how well that hypothesis accounts for what you saw. p(θ∣D)p(\theta \mid D) is the posterior — what you believe afterwards. p(D)p(D) 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 NN data points. Under a model with parameters θ\theta, write the probability of observing that entire dataset.

p(D∣θ)=∏i=1Npθ(yi∣xi)p(D \mid \theta) = \prod_{i=1}^{N} p_{\theta}(y_i \mid x_i)

In symbols it is one long product; in words it says multiply together, across all NN 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 θ\theta 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 θ\theta. Then flip the sign, because the optimization world is standardised on minimization.

−log⁡p(D∣θ)=−∑i=1Nlog⁡pθ(yi∣xi)-\log p(D \mid \theta) = -\sum_{i=1}^{N} \log p_{\theta}(y_i \mid x_i)
(5)

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 NN 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:

p(y∣x,θ)=12πσ2exp⁡ ⁣(−(y−fθ(x))22σ2)p(y \mid x, \theta) = \frac{1}{\sqrt{2\pi\sigma^2}}\exp\!\left(-\frac{\big(y - f_{\theta}(x)\big)^2}{2\sigma^2}\right)

fθ(x)f_{\theta}(x) is the model's prediction and σ\sigma ("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 θ\theta is −(y−fθ(x))22σ2-\dfrac{(y - f_{\theta}(x))^2}{2\sigma^2}. 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

E[(y^−y)2]=(bias)2+variance+noise\mathbb{E}\big[(\hat{y} - y)^2\big] = (\text{bias})^2 + \text{variance} + \text{noise}

y^\hat{y} is what the model predicted and yy 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.

pi=exp⁡(zi/T)∑jexp⁡(zj/T)p_i = \frac{\exp(z_i / T)}{\sum_{j}\exp(z_j / T)}
(6)

TT is the temperature, and the only change is that the logits are divided by TT before being turned into probabilities. Small TT magnifies the gaps between scores and the distribution spikes; large TT compresses them and it flattens. As TT 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 pp, 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 log⁡0\log 0 runs to infinity, so applying log⁡\log 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

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).

Published by TechAthletes · Editorial policy · How we cite · Corrections

Comments

Sign in to comment