All courses

Calibration of model probabilities

Define calibration (events assigned 70% happen 70% of the time) and explain why minimizing cross-entropy rewards calibrated next-token probabilities. Contrast this with the confidence expressed in generated text, and note that post-training often degrades calibration.

Builds on: The next-token cross-entropy objective, From base model to assistant

▶ Watch this lesson Start the whole course

What the animation says

Two kinds of confidence

A language model shows confidence in two different ways. First, the probability it puts on each possible next token. Second, words in its reply, like “I’m ninety percent sure.” A confidence is calibrated if, across all the times it says seventy percent, the thing happens about seventy percent of the time. Training only teaches the model to predict text. So which of these two ends up honest?

What the loss charges

The token probabilities. Here’s how the loss makes them honest. Say one context appears ten times in the corpus: seven times followed by yes, three times by no. Each time, cross-entropy charges minus the log of the probability given to the token that actually came. Small probability, big charge. Predict yes at seventy percent: each yes costs zero point three six nats, each no costs one point two. Now an overconfident ninety percent against a hedging fifty-fifty. Which costs more on average?

Minimum at the truth

Ninety percent costs more. Each rare no now costs two point three nats. Fifty-fifty overpays on every yes instead. Seventy percent is cheapest: zero point six one. In general, the average loss equals the data’s own uncertainty, which is fixed, plus a gap that is zero only at the true frequencies. For ninety percent, that gap is zero point one five. Matching every context gives calibrated probabilities. Saying “I’m ninety percent sure” gets none of this pressure. Those words are just tokens imitating people.

Post-training sharpens

Post-training changes the goal. It scores whole replies by reward, not by how often continuations occur. Confident, single-answer replies tend to win, so probability piles onto one answer. This toy simulation draws a true chance for each event, then lets each event happen with that chance. Two predictors: one reports the true chance; the other is a changed copy. Tap the line that makes the second predictor overconfident.

Said 90%, happened 75%

That line stretches every chance away from one half. A true seventy-five percent becomes a claimed ninety. Gather every case where a predictor says about ninety percent. For the calibrated one, they happen about ninety percent of the time. For the sharpened one, only about seventy-five percent. Its average loss also rises above the calibrated zero point five nats. So: base-model token probabilities tend to be calibrated, because cross-entropy is lowest at the real frequencies. Stated confidence is imitated text. And reward-driven post-training often makes even the token probabilities overconfident.

When a model says 70%, is it right 70%?

An LLM expresses confidence in two different ways. One is the probability it puts on each possible next token. The other is words in its reply, like *"I'm 90% sure."* A confidence is **calibrated** if, across all the times it says 70%, the thing happens about 70% of the time. Which of the two can you trust? Why would training make either one honest?

The loss pays for honest frequencies

In a large corpus, similar contexts recur with different continuations. Cross-entropy charges $-\log q$ for the token that actually came. If you overstate one token, its extra probability is taken from the others, so you pay more every time one of them appears. If you understate it, you pay more every time it appears. These two costs balance exactly when your probabilities equal the real frequencies. Phrases like *"I'm 90% sure"* are different: they are just more tokens, trained to sound like what people write.

Cross-entropy is minimized at the truth

**Calibration:** $\Pr(\text{event} \mid \text{model said } q) = q$ for every $q$. Take one context. Let $P$ be the real next-token frequencies after it, and $q$ the model's prediction. The expected loss is $$\mathbb{E}_{x\sim P}[-\log q(x)] = H(P) + D_{KL}(P\,\|\,q).$$ $H(P)$ is fixed, and $D_{KL}\ge 0$ equals $0$ only when $q=P$. So exaggerating or hedging both raise the loss. Matching $P$ in every context gives calibrated probabilities.

A 70/30 continuation, three guesses

After some context, the corpus continues with “yes” 70% of the time and “no” 30%. The expected loss is $-0.7\ln q - 0.3\ln(1-q)$: - $q=0.7$: $0.7(0.357)+0.3(1.204)=\mathbf{0.611}$ nats - $q=0.9$: $0.7(0.105)+0.3(2.303)=0.765$ - $q=0.5$: $\ln 2 = 0.693$ Both overconfidence and hedging cost more. The gap $0.765-0.611=0.154$ is exactly $D_{KL}$.

Post-training sharpens, calibration breaks

Post-training scores whole responses by reward, not by how often continuations occur. Confident, single-answer replies tend to win, so probability gets piled onto one answer. This toy simulation imitates that with `sharp`, which pushes true chances toward 0 or 1. The calibrated predictor's “90%” cases come true about 90% of the time, at 0.50 nats. The sharpened one's “90%” cases come true only about 75% of the time, and its cross-entropy rises above 0.50 nats.
import numpy as np
rng = np.random.default_rng(0)
truth = rng.uniform(0, 1, 200_000)              # real chance of each event
happened = rng.uniform(0, 1, truth.size) < truth
sharp = np.clip(0.5 + 1.6 * (truth - 0.5), 0.01, 0.99)  # pushed toward 0/1
for name, q in [("calibrated", truth), ("sharpened", sharp)]:
    near90 = (q > 0.85) & (q < 0.95)
    hit = happened[near90].mean()
    ce = -np.mean(np.where(happened, np.log(q), np.log(1 - q)))
    print(name, round(hit, 2), round(ce, 3))

What to remember

Cross-entropy is lowest when predicted probabilities equal real frequencies, so a base model's token probabilities tend to be calibrated. Confidence stated in words is only imitated text. Post-training that optimizes reward often makes even the token probabilities overconfident.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.