advanced decoding-generation 40 min read

Calibration of Language Models

When a model says 0.9, is it right nine times in ten? For a next-token distribution over thousands of words the question splits into a strong form no corpus can test and a weak form any corpus can; every log loss separates into what the text cannot tell us, what the model's context discards, and what a recalibration could recover

Overview & Motivation

Score Topic 3’s Kneser–Ney trigram on 23,21923{,}219 tokens of new text from the generator that wrote its training corpus. For every token, write down the probability the model gave its single likeliest next word, and whether that word is the one that came. Sort the tokens into fifteen bins by that probability, and in each bin compare the average probability with the fraction of hits. The fifteen points lie almost on the diagonal: the model’s mean confidence is 0.5220.522 and its accuracy 0.5270.527. Now do the same on 830830 tokens of real MD&A text from EDGAR filings. The model’s mean confidence is 0.1440.144; its accuracy is 0.0590.059. It reads as overconfident by a factor of more than two, and most of that reading, we will see, is one token.

That comparison is a calibration check, and this page makes it precise for language models. The sampling topic treated temperature as a decoding choice that trades quality against diversity. Here the same operation is a repair: the temperature that makes the model’s probabilities honest. We develop it in order:

  1. What calibration means for a 3,427-way forecast. The strong form, that the whole distribution is right, is the same as being the true conditional, and no corpus can check it. The weak form, that the top probability is right, any corpus can check.
  2. What miscalibration costs. Every log loss splits into three exact parts, and only one of them can be recalibrated away.
  3. How to measure it honestly. The standard estimator is biased in both directions, and a calibrated twin of the model measures one of the biases.
  4. One dial. Temperature scaling is a convex fit that matches one moment; it keeps every argmax but not the order of confidences.
  5. Why τ=1\tau = 1. Maximum likelihood leaves the training-data temperature at exactly 11 when the model can scale its own logits, and below 11 when it is penalized.
  6. Calibrated sets. Calibration is what makes a nucleus a prediction set.

Then we measure both corpus engines on three test sets and return to the filings.


What calibration means for a 3,427-way forecast

We write XX for the context a model conditions on (the trigram’s last two words, or the decoder’s whole prefix), YY for the next token, and F=f(X)∈ΔVF = f(X) \in \Delta^{\mathcal V} for the model’s forecast, a point of the probability simplex over the vocabulary V\mathcal V. The truth is P=P(Y∣X)P = P(Y \mid X), the next-token distribution given everything the model sees. The recalibration of ff is

C  =  P(Y∣F)  =  E[P∣F],C \;=\; P(Y \mid F) \;=\; \mathbb{E}[P \mid F],

the distribution of the next token among all contexts that receive the same forecast. Geometrically, ff sends contexts to points of the simplex; CC averages the truth over each point’s preimage.

Definition 1 (Calibration of a next-token forecaster).

ff is canonically calibrated if C=FC = F almost surely. With conf=max⁡vFv\mathrm{conf} = \max_v F_v and a^\hat a the likeliest token (ties to the lower token id), ff is top-label calibrated if P(Y=a^∣conf)=confP(Y = \hat a \mid \mathrm{conf}) = \mathrm{conf}, and its top-label calibration error is E ∣P(Y=a^∣conf)−conf∣\mathbb{E}\,\lvert P(Y = \hat a \mid \mathrm{conf}) - \mathrm{conf} \rvert.

The formalML uncertainty-quantification topic defines calibration for a binary outcome, where there is only one probability to check. A next-token forecast has 3,4263{,}426 free coordinates, and the two notions above sit at the two ends of what one could ask of them.

Proposition 1 (Canonical, top-label, and the truth).

(a) Canonical calibration implies top-label calibration.

(b) The converse fails: with one context, truth (0.5,0.2,0.3)(0.5, 0.2, 0.3), and forecast (0.5,0.3,0.2)(0.5, 0.3, 0.2), the forecaster is top-label calibrated and not canonically calibrated.

(c) If ff gives distinct contexts distinct forecasts, then C=PC = P. So ff is canonically calibrated if and only if F=PF = P.

Proof.

(a) If C=FC = F, then P(Y=a^∣F)=Fa^=confP(Y = \hat a \mid F) = F_{\hat a} = \mathrm{conf}. The confidence is a function of FF, so by the tower property P(Y=a^∣conf)=E[conf∣conf]=confP(Y = \hat a \mid \mathrm{conf}) = \mathbb{E}[\mathrm{conf} \mid \mathrm{conf}] = \mathrm{conf}.

(b) The forecaster says 0.50.5 for its top token and is right with probability 0.50.5, but C=P≠FC = P \ne F.

(c) Each level set {F=q}\{F = q\} is a single context, so E[P∣F]=P\mathbb{E}[P \mid F] = P.

∎

Part (c) is why canonical calibration is out of reach for a language model. Most contexts the trigram has seen have a row of their own (2,9502{,}950 seen contexts sit on 2,4802{,}480 distinct rows in the test text below), so checking canonical calibration means checking that nearly every row is the true conditional over 3,4273{,}427 outcomes, from a few occurrences of each context. What a corpus can check is top-label calibration, one number per token, and that is what every corpus number on this page measures. Canonical calibration we test exactly, on small finite worlds where every expectation is a sum.


What miscalibration costs

The log loss of a forecaster is its cross-entropy, E[−log⁡FY]\mathbb{E}[-\log F_Y]. Calibration is one part of it, and the decomposition is clearest first for the Brier score E∥F−eY∥2\mathbb{E}\lVert F - e_Y \rVert^2, where eYe_Y is the corner of the simplex at the token that occurred. Three points sit in the simplex for each context: the forecast FF, its recalibration CC, and the truth PP. Conditional on FF, the outcome eYe_Y averages to CC; conditional on XX, it averages to PP. So the squared distance from FF to the outcome splits by Pythagoras, twice.

Theorem 1 (Three-term decomposition).

E[−log⁡FY]  =  E H(P)⏟irreducible  +  E KL(P ∥ C)⏟refinement gap  +  E KL(C ∥ F)⏟reliability,\mathbb{E}[-\log F_Y] \;=\; \underbrace{\mathbb{E}\,H(P)}_{\text{irreducible}} \;+\; \underbrace{\mathbb{E}\,\mathrm{KL}(P \,\|\, C)}_{\text{refinement gap}} \;+\; \underbrace{\mathbb{E}\,\mathrm{KL}(C \,\|\, F)}_{\text{reliability}},E∥F−eY∥2  =  E(1−∥P∥2)  +  E∥C−P∥2  +  E∥F−C∥2.\mathbb{E}\lVert F - e_Y \rVert^2 \;=\; \mathbb{E}\big(1 - \lVert P \rVert^2\big) \;+\; \mathbb{E}\lVert C - P \rVert^2 \;+\; \mathbb{E}\lVert F - C \rVert^2.

Every term is nonnegative. The recalibrated forecaster CC keeps the first two terms and loses the third, and the reliability term is zero exactly when ff is canonically calibrated.

Proof.

Brier. Expand ∥F−eY∥2=∥F−C∥2+∥C−eY∥2+2⟨F−C,C−eY⟩\lVert F - e_Y \rVert^2 = \lVert F - C \rVert^2 + \lVert C - e_Y \rVert^2 + 2\langle F - C, C - e_Y \rangle. Given FF, the vectors FF and CC are fixed and E[eY∣F]=C\mathbb{E}[e_Y \mid F] = C, so the cross term has conditional mean ⟨F−C,C−C⟩=0\langle F - C, C - C\rangle = 0. Repeat with CC, PP, and conditioning on XX: CC is a function of XX and E[eY∣X]=P\mathbb{E}[e_Y \mid X] = P, so E∥C−eY∥2=E∥C−P∥2+E∥P−eY∥2\mathbb{E}\lVert C - e_Y \rVert^2 = \mathbb{E}\lVert C - P \rVert^2 + \mathbb{E}\lVert P - e_Y \rVert^2. Finally E[∥P−eY∥2∣X]=∥P∥2−2∥P∥2+1\mathbb{E}[\lVert P - e_Y \rVert^2 \mid X] = \lVert P \rVert^2 - 2\lVert P \rVert^2 + 1.

Log loss. Given FF, the next token has law CC, so E[−log⁡FY∣F]=∑vCv(−log⁡Fv)=H(C)+KL(C ∥ F)\mathbb{E}[-\log F_Y \mid F] = \sum_v C_v (-\log F_v) = H(C) + \mathrm{KL}(C \,\|\, F). Next, −log⁡Cv-\log C_v is a function of FF and E[P∣F]=C\mathbb{E}[P \mid F] = C, so E H(C)=E∑vPv(−log⁡Cv)=E[H(P)+KL(P ∥ C)]\mathbb{E}\,H(C) = \mathbb{E}\sum_v P_v(-\log C_v) = \mathbb{E}\big[H(P) + \mathrm{KL}(P \,\|\, C)\big]. Nonnegativity is Gibbs’s inequality, and KL(C ∥ F)=0\mathrm{KL}(C \,\|\, F) = 0 exactly when C=FC = F.

∎

The three terms answer three questions. The irreducible term is what the text cannot tell any forecaster that sees the same context, the floor that only a longer context lowers. The refinement gap is what ff throws away by sending different contexts to the same forecast. The reliability term is what a recalibration of the forecast, a function applied to FF alone, could recover. Bröcker (2009, Eq. 10) gives the split into E H(C)\mathbb{E}\,H(C) and the reliability term for any proper score, and his §3 on sufficiency gives the second split; for binned binary forecasts under the Brier score it is Murphy’s (1973) partition, which the formalML topic proves as its Theorem 3. DeGroot and Fienberg (1983) named the two halves calibration and refinement.

Corollary 1 (No recalibration recovers more than the reliability term).

For any map gg of the forecast, E[−log⁡g(F)Y]≥E[−log⁡FY]−E KL(C ∥ F)\mathbb{E}[-\log g(F)_Y] \ge \mathbb{E}[-\log F_Y] - \mathbb{E}\,\mathrm{KL}(C \,\|\, F). In particular, no temperature lowers the log loss of a canonically calibrated forecaster.

Proof.

Let Cg=E[P∣g(F)]C_g = \mathbb{E}[P \mid g(F)]. Theorem 1 applied to g∘fg \circ f gives E[−log⁡g(F)Y]≥E H(P)+E KL(P ∥ Cg)\mathbb{E}[-\log g(F)_Y] \ge \mathbb{E}\,H(P) + \mathbb{E}\,\mathrm{KL}(P \,\|\, C_g). Since log⁡(Cv/Cg,v)\log(C_v / C_{g,v}) is a function of FF and E[P∣F]=C\mathbb{E}[P \mid F] = C,

E KL(P ∥ Cg)−E KL(P ∥ C)  =  E∑vCvlog⁡CvCg,v  =  E KL(C ∥ Cg)  ≥  0,\mathbb{E}\,\mathrm{KL}(P \,\|\, C_g) - \mathbb{E}\,\mathrm{KL}(P \,\|\, C) \;=\; \mathbb{E}\sum_v C_v \log\frac{C_v}{C_{g,v}} \;=\; \mathbb{E}\,\mathrm{KL}(C \,\|\, C_g) \;\ge\; 0,

and E H(P)+E KL(P ∥ C)\mathbb{E}\,H(P) + \mathbb{E}\,\mathrm{KL}(P \,\|\, C) is the loss minus the reliability term.

∎

So the drop in held-out log loss that any fitted map of the forecast achieves estimates a lower bound on how miscalibrated the model is. It is only an estimate, from a sample, and it needs to be an honest one, since a temperature fit and scored on the same tokens flatters itself. We cross-fit: fit τ\tau on the even sentences and score the odd ones, then swap, and average the two by their token counts. And it is only a lower bound, for the one map we tried. Temperature’s can be far from tight, as the finance case study will show. The harness checks Theorem 1 and Corollary 1 to 10−1210^{-12} on 5050 random finite worlds, against 4141 temperatures each.


Measuring calibration honestly

On a corpus we see tokens, not conditional distributions, and the standard estimator bins them.

Definition 2 (The binned calibration error).

Partition [0,1][0, 1] into BB equal-width bins and put each token in the bin of its confidence. With nbn_b tokens in bin bb, mean confidence cˉb\bar c_b, and fraction of hits aˉb\bar a_b, the plug-in ECE is ECE^B=∑b(nb/n) ∣aˉb−cˉb∣\widehat{\mathrm{ECE}}_B = \sum_b (n_b / n)\,\lvert \bar a_b - \bar c_b \rvert. Its population counterpart is ECEB=∑bP(b) ∣E[ρ(conf)−conf∣b]∣\mathrm{ECE}_B = \sum_b P(b)\,\lvert \mathbb{E}[\rho(\mathrm{conf}) - \mathrm{conf} \mid b] \rvert, where ρ(c)=P(Y=a^∣conf=c)\rho(c) = P(Y = \hat a \mid \mathrm{conf} = c).

The formalML topic notes that ECE depends on the bin count, biased toward zero with too few bins and upward with too many. Both directions can be made exact, and the second can be measured.

Theorem 2 (Two biases of the binned estimator).

(a) Binning hides. If the bins B′\mathfrak{B}' refine the bins B\mathfrak{B}, then ECEB≤ECEB′≤\mathrm{ECE}_{\mathfrak{B}} \le \mathrm{ECE}_{\mathfrak{B}'} \le the top-label calibration error. In particular ECE1≤ECE2≤ECE4≤⋯\mathrm{ECE}_1 \le \mathrm{ECE}_2 \le \mathrm{ECE}_4 \le \cdots.

(b) Sampling inflates. Suppose that, given their confidences, the tokens’ hits are independent with probabilities ρi\rho_i. Then E[ECE^B]≥∑b(nb/n) ∣ρˉb−cˉb∣\mathbb{E}\big[\widehat{\mathrm{ECE}}_B\big] \ge \sum_b (n_b/n)\,\lvert \bar\rho_b - \bar c_b \rvert, the sample’s own binned error, strictly in every bin where aˉb−cˉb\bar a_b - \bar c_b can take both signs. A perfectly calibrated model therefore shows a positive estimated error.

(c) The square can be corrected. In each bin,

E[(aˉb−cˉb)2−aˉb(1−aˉb)nb−1]  =  (ρˉb−cˉb)2−∑i∈b(ρi−ρˉb)2nb(nb−1).\mathbb{E}\Big[(\bar a_b - \bar c_b)^2 - \frac{\bar a_b(1 - \bar a_b)}{n_b - 1}\Big] \;=\; (\bar\rho_b - \bar c_b)^2 - \frac{\sum_{i \in b} (\rho_i - \bar\rho_b)^2}{n_b(n_b - 1)}.
Proof.

(a) A bin is a function of the confidence, so E[ρ−conf∣b]=E[E[ρ−conf∣conf]∣b]\mathbb{E}[\rho - \mathrm{conf} \mid b] = \mathbb{E}\big[\mathbb{E}[\rho - \mathrm{conf} \mid \mathrm{conf}] \mid b\big], and Jensen’s inequality gives ∣E[ρ−conf∣b]∣≤E[∣ρ−conf∣∣b]\lvert \mathbb{E}[\rho - \mathrm{conf} \mid b] \rvert \le \mathbb{E}\big[\lvert \rho - \mathrm{conf} \rvert \mid b\big]. Summing over bins compares a partition with the finest one, and the same argument inside each cell of B\mathfrak{B} compares B\mathfrak{B} with B′\mathfrak{B}'.

(b) Jensen again, inside a bin: E∣aˉb−cˉb∣≥∣Eaˉb−cˉb∣=∣ρˉb−cˉb∣\mathbb{E}\lvert \bar a_b - \bar c_b \rvert \ge \lvert \mathbb{E}\bar a_b - \bar c_b \rvert = \lvert \bar\rho_b - \bar c_b \rvert, with equality only if aˉb−cˉb\bar a_b - \bar c_b has one sign almost surely.

(c) Let S=∑i∈byiS = \sum_{i \in b} y_i count the hits, so aˉb=S/nb\bar a_b = S / n_b, ES=nbρˉb\mathbb{E}S = n_b\bar\rho_b, and, with D=∑i∈b(ρi−ρˉb)2D = \sum_{i \in b}(\rho_i - \bar\rho_b)^2, Var⁡S=∑iρi(1−ρi)=nbρˉb(1−ρˉb)−D\operatorname{Var} S = \sum_i \rho_i(1 - \rho_i) = n_b\bar\rho_b(1 - \bar\rho_b) - D. Then E(aˉb−cˉb)2=(ρˉb−cˉb)2+[nbρˉb(1−ρˉb)−D]/nb2\mathbb{E}(\bar a_b - \bar c_b)^2 = (\bar\rho_b - \bar c_b)^2 + [n_b\bar\rho_b(1 - \bar\rho_b) - D]/n_b^2. The hits are 00 or 11, so s2=nbaˉb(1−aˉb)/(nb−1)s^2 = n_b\bar a_b(1 - \bar a_b)/(n_b - 1) is their sample variance, and Es2=ρˉb(1−ρˉb)+D/(nb(nb−1))\mathbb{E}s^2 = \bar\rho_b(1 - \bar\rho_b) + D/(n_b(n_b - 1)). Subtracting Es2/nb\mathbb{E}s^2 / n_b leaves the stated right side.

∎

Part (c) is the correction Bröcker (2012) and Ferro and Fricker (2012) derived for the Brier score’s reliability term; Kumar, Liang, and Ma (2019) state it for binned models (their Definition 5.2), where every token in a bin shares one ρ\rho, D=0D = 0, and the estimator is exactly unbiased, and they prove it needs far fewer samples than the plug-in. For a model whose confidences vary inside a bin, the remaining bias is the small negative heterogeneity term. Roelofs and colleagues (2022) find the binned estimator’s bias most severe for perfectly calibrated models, which is part (b).

Part (b) also suggests a measurement. Keep the model’s confidences and redraw every token’s hit as a coin with that confidence: this calibrated twin is calibrated by construction, so its estimated ECE is pure estimator noise. On the trigram’s in-distribution tokens, at 1515 bins, 100100 twins average 0.0060.006 with a standard deviation of 0.00120.0012, while the trigram itself scores 0.0180.018, about ten standard deviations above the floor. The miscalibration is real, and small. The twin’s floor rises with the bin count, from 0.0020.002 at one bin to 0.0120.012 at 6060; it grows like B\sqrt{B}, at about 0.25B/n0.25\sqrt{B/n}. The trigram’s own plug-in ECE rises too, from 0.0050.005 to 0.0240.024, but that is not part (a) at work. Part (a) is about the population ECE and nested bins, and the plug-in also carries the noise of part (b): the twin’s floor alone rises 0.0100.010 of that 0.0190.019. Bin counts of 1010 and 1515 are not nested in any case. The harness checks part (a) along B=1,2,4,…,64B = 1, 2, 4, \dots, 64 on 5050 worlds, and parts (b) and (c) by exact Poisson-binomial expectations on 200200 random bins.


One dial: temperature scaling

A recalibration map has to be fit, and the simplest has one parameter. With zz the log-probabilities of a row and β=1/τ\beta = 1/\tau, temperature scaling replaces the row by softmax⁡(βz)\operatorname{softmax}(\beta z), exactly the operation of the sampling topic, now chosen to minimize the held-out log loss

L(β)  =  1N∑tokens[log⁡∑veβzv  −  βzy].L(\beta) \;=\; \frac{1}{N} \sum_{\text{tokens}} \Big[\log \sum_v e^{\beta z_v} \;-\; \beta z_y\Big].

Proposition 2 (Temperature scaling is a convex moment match).

(a) L′(β)=1N∑(Epβz−zy)L'(\beta) = \frac{1}{N}\sum \big(\mathbb{E}_{p_\beta} z - z_y\big) and L′′(β)=1N∑Var⁡pβ(z)≥0L''(\beta) = \frac{1}{N}\sum \operatorname{Var}_{p_\beta}(z) \ge 0, where pβ=softmax⁡(βz)p_\beta = \operatorname{softmax}(\beta z). So LL is convex, and strictly convex unless every row is constant on its support.

(b) A minimizer, if one exists, is unique, and it is the β\beta at which the expected logit equals the observed one: ∑Epβz=∑zy\sum \mathbb{E}_{p_\beta} z = \sum z_y.

(c) Every row keeps its argmax, so accuracy does not change. But for τ≠1\tau \ne 1 the order of confidences across rows can: the rows (0.5,0.5,0)(0.5, 0.5, 0) and (0.6,0.2,0.2)(0.6, 0.2, 0.2) have confidences 0.5<0.60.5 \lt 0.6, and at τ=2\tau = 2 they have 0.5>0.4640.5 \gt 0.464.

Proof.

(a) The derivative of log⁡∑veβzv\log\sum_v e^{\beta z_v} in β\beta is Epβz\mathbb{E}_{p_\beta} z, and its second derivative is Var⁡pβ(z)\operatorname{Var}_{p_\beta}(z).

(b) A strictly convex function has at most one minimizer, and it is where L′=0L' = 0.

(c) softmax⁡(βz)\operatorname{softmax}(\beta z) is increasing in each coordinate of zz, so it keeps the order within a row. At τ=2\tau = 2 the first row stays (0.5,0.5,0)(0.5, 0.5, 0), and the second becomes proportional to (0.6,0.2,0.2)(\sqrt{0.6}, \sqrt{0.2}, \sqrt{0.2}), with top probability 0.6/(0.6+20.2)=0.464\sqrt{0.6}/(\sqrt{0.6} + 2\sqrt{0.2}) = 0.464.

∎

Part (a) is the formalStatistics exponential-families topic’s Theorem 7.1 for a one-parameter family whose natural parameter is β\beta, and part (b) is its Theorem 7.4: the maximum-likelihood fit matches the sufficient statistic’s mean. Guo and colleagues (2017, Supplement S2) characterize temperature scaling the same way, as the maximum-entropy distribution subject to that one moment. The harness fits β\beta by Newton’s method from β=1\beta = 1 and checks that ∣L′∣<10−10\lvert L'\rvert \lt 10^{-10} at the result on every engine and test set (by convexity, the global optimum); on the two EDGAR sets it also agrees with a bounded scalar search to 10−610^{-6}.

Part (c) separates temperature scaling from the recalibrations of the formalRAG significance-testing topic, whose Theorem 2 shows that a monotone map of a score preserves every ranking. Temperature is not a function of the confidence: two rows with the same top probability can land on different ones. On the trigram’s in-distribution rows, Kendall’s τb\tau_b between the confidences at τ=1\tau = 1 and at τ=2\tau = 2 is 0.8600.860 over distinct rows and about 0.810.81 over tokens. A selective-prediction rule that ranks contexts by confidence changes when the temperature does, though not by much at the top: a rule that keeps the fifth of the tokens with the highest confidence keeps 91.5%91.5\% of the same tokens from τ=1\tau = 1 to τ=2\tau = 2, and its accuracy goes from 1.0001.000 to 0.9970.997.

A · Reliability — how honest the top probability is, and how much of the measured error is noise
1.00
bins B
accuracy against confidence, one circle per bin (area ∝ tokens); ticks mark each gap000.20.20.40.40.60.60.80.811mean confidence in the binaccuracyplug-in ECE against the bin count, and the calibrated twin’s mean ± 2 SD00.0050.010.0150.020.0251510153060number of equal-width bins B (log scale)
the engine’s ECEcalibrated twin, meantwin ± 2 SD (100 twins)
ECE at 15 bins
0.018
calibrated twin’s ECE (noise floor)
0.006 ± 0.001
mean confidence / accuracy
0.522 / 0.527
log loss, nats per token
2.538
nucleus(0.9): coverage / kept mass
0.926 / 0.939
NLL-optimal τ* (its NLL)
1.003 (2.538)
cross-fitted gain from τ*, nats
0.000
tokens scored
23,219

Fewer bins average miscalibration away and more bins invent it (Theorem 2): the twin, calibrated by construction, shows how much of an ECE is noise at this sample size. Temperature moves tokens between bins, so the circles can jump, and only the overall accuracy readout stays fixed (Proposition 2). In distribution both engines sit near the diagonal. On the EDGAR filings the trigram’s coverage can’t pass its share of known words at any temperature, because the unknown-word token is last in every row (Proposition 3); the decoder’s comes close only at high τ.

B · Facts by regime — one temperature cannot calibrate what the model has and has not seen
1.00
mean confidence (bar) against accuracy (black tick), by regime00.250.50.751seen twice or moreseen oncenever seen−log P(the knowledge base’s answer) per regime, clipped at 4 nats; triangles mark each best τ012340.050.10.20.512510temperature τ (log scale)
seen twice or more (30 probes)seen once (33 probes)never seen (48 probes)all 111 probes
seen twice or more: confidence / accuracy
0.785 / 1.000
seen once: confidence / accuracy
0.332 / 0.697
never seen: confidence / accuracy
0.303 / 0.062
all probes: log loss at this τ
1.623
log loss, in sample: τ = 1 / one τ* / τ* per regime
1.623 / 1.613 / 1.119

Accuracy does not move with τ; confidence does. Facts seen twice or more are always right but underconfident, so their loss falls as τ falls; facts never seen are overconfident, so theirs falls as τ rises toward the uniform forecast. The pooled optimum (1.179) serves neither.


Why τ = 1 at all

In distribution, the trigram’s NLL-optimal temperature is 1.0031.003, as if the model came out of training exactly where temperature scaling would put it. That number is a balance, not a law: on the words the trigram knows, τ∗\tau^* is 0.960.96, and the <unk> tokens pull it back up. A Kneser–Ney trigram is no likelihood optimum and has no direction that scales its logits, so nothing pins its temperature. For some families, though, something does.

Theorem 3 (Where maximum likelihood leaves the temperature).

Suppose a model family has a reparametrization θ↦sβ(θ)\theta \mapsto s_\beta(\theta), differentiable at β=1\beta = 1 with s1(θ)=θs_1(\theta) = \theta, that scales its logits, zsβ(θ)=β zθz_{s_\beta(\theta)} = \beta\, z_\theta, and a penalty that scales as Ω(sβ(θ))=β2 Ω(θ)\Omega(s_\beta(\theta)) = \beta^2\,\Omega(\theta). If θ^\hat\theta is a stationary point of Ltrain(θ)+λΩ(θ)L_{\text{train}}(\theta) + \lambda\Omega(\theta), then

ddβ Ltrain(β zθ^)∣β=1  =  −2λ Ω(θ^).\frac{d}{d\beta}\, L_{\text{train}}\big(\beta\, z_{\hat\theta}\big)\Big|_{\beta = 1} \;=\; -2\lambda\,\Omega(\hat\theta).

So an unpenalized stationary point has training-data temperature exactly 11. With λΩ(θ^)>0\lambda\Omega(\hat\theta) \gt 0, the training-data optimum has τ<1\tau \lt 1: the model is underconfident on its own training data.

Proof.

Let φ(β)=Ltrain(sβ(θ^))+λΩ(sβ(θ^))=Ltrain(βzθ^)+λβ2Ω(θ^)\varphi(\beta) = L_{\text{train}}(s_\beta(\hat\theta)) + \lambda\Omega(s_\beta(\hat\theta)) = L_{\text{train}}(\beta z_{\hat\theta}) + \lambda\beta^2\Omega(\hat\theta). By the chain rule φ′(1)\varphi'(1) is the gradient of the penalized loss at θ^\hat\theta applied to ∂βsβ(θ^)\partial_\beta s_\beta(\hat\theta), which is 00 at a stationary point. The penalty contributes 2λΩ(θ^)2\lambda\Omega(\hat\theta) to φ′(1)\varphi'(1), so the training loss contributes its negative. When that is negative, the training loss decreases as β\beta passes 11, and by Proposition 2’s convexity its minimizer has β>1\beta \gt 1.

∎

This is the score equation of the formalStatistics maximum-likelihood topic (its Theorem 1) read along one direction of parameter space. A log-linear bigram, whose logits are a matrix WW indexed by the previous token, has the direction exactly: sβ(W)=βWs_\beta(W) = \beta W, and the L2 penalty Ω=12∥W∥2\Omega = \tfrac12\lVert W \rVert^2 scales as required. Fit on the shared filings over the decoder’s 512512 tokens, at ten penalties from 10−210^{-2} to 10−710^{-7}, the derivative at β=1\beta = 1 and −λ∥W∥2-\lambda\lVert W \rVert^2 agree to six decimals every time. Seven of the ten:

penalty λ\lambda10−210^{-2}10−310^{-3}10−410^{-4}3⋅10−53 \cdot 10^{-5}10−510^{-5}10−610^{-6}10−710^{-7}
derivative at β=1\beta = 1−0.277-0.277−1.092-1.092−1.456-1.456−0.960-0.960−0.581-0.581−0.155-0.155−0.029-0.029
τ∗\tau^*, training0.2340.2340.5200.5200.7440.7440.8220.8220.8710.8710.9430.9430.9840.984
τ∗\tau^*, continuation0.2320.2320.5160.5160.7440.7440.8290.8290.8890.8891.0011.0011.1071.107
τ∗\tau^*, held0.2100.2100.4720.4720.7110.7110.7840.7840.8280.8280.8850.8850.9170.917
τ∗\tau^*, EDGAR0.2850.2850.7580.7580.9380.9381.0381.0381.1301.1301.3551.3551.6471.647

The training row is the theorem: the more penalty, the more underconfident. Without the penalty there is no finite optimum to be stationary at: the logits of word pairs never seen in training run off to −∞-\infty, and with them the held-out likelihood of any text that contains one. The penalty that keeps the model usable is what makes it underconfident on its training data. The other rows are text the theorem says nothing about, and the temperatures there are not pinned. On the continuation τ∗\tau^* crosses 11 near λ=10−6\lambda = 10^{-6}, on EDGAR between 10−410^{-4} and 3⋅10−53 \cdot 10^{-5}, and on held it never crosses on this grid. (The test sets, continuation, held, and EDGAR, are defined below.)

Topic 14’s decoder has no such direction. Its final layer normalization has no learned gain, its output matrix is its input embedding scaled by a fixed 1/d1/\sqrt{d}, and scaling the embedding moves the inputs as well as the outputs. Nor is it a stationary point: its 1,2001{,}200 steps of Adam end with a nonzero gradient over the training windows. Neither hypothesis of Theorem 3 holds, so nothing pins its temperature. On every second training sentence the NLL-optimal τ\tau is 0.9430.943, and it is 0.9370.937 to 0.9430.943 across Topic 14’s four training seeds.

The same dial appeared in the sampling topic for the opposite reason. There, temperature reshaped a model deliberately, to trade predictability for diversity.

Corollary 2 (Sampling temperature miscalibrates).

If ff is canonically calibrated and τ≠1\tau \ne 1, the sampler Fτ=softmax⁡(log⁡F/τ)F_\tau = \operatorname{softmax}(\log F / \tau) has reliability term E KL(F ∥ Fτ)>0\mathbb{E}\,\mathrm{KL}(F \,\|\, F_\tau) \gt 0 unless every forecast is uniform on its support.

Proof.

The map F↦FτF \mapsto F_\tau is one-to-one, so conditioning on FτF_\tau is conditioning on FF, and the recalibration of FτF_\tau is C=FC = F. Theorem 1 gives its reliability term as E KL(F ∥ Fτ)\mathbb{E}\,\mathrm{KL}(F \,\|\, F_\tau), which is zero only where Fτ=FF_\tau = F.

∎

Calibrated sets

The sampling topic’s nucleus keeps the shortest list of likeliest tokens whose mass reaches pp. It is also a prediction set: a claim that the next token is in the list. Calibration is what makes the claim honest.

Proposition 3 (Calibrated sets cover their kept mass).

If ff is canonically calibrated, then every set S(F)S(F) chosen from the forecast alone covers the next token with probability equal to its expected kept mass: P(Y∈S(F))=E F(S(F))P(Y \in S(F)) = \mathbb{E}\,F(S(F)). A nucleus of mass pp therefore covers at least pp, and a coverage that differs from the kept mass certifies miscalibration.

Proof.

P(Y∈S(F)∣F)=C(S(F))=F(S(F))P(Y \in S(F) \mid F) = C(S(F)) = F(S(F)), and the tower property averages both sides.

∎

In distribution the trigram’s nucleus of mass 0.90.9 keeps 0.9390.939 of the mass on average and covers 0.9260.926 of the tokens; the decoder’s keeps 0.9450.945 and covers 0.9510.951. Both are close, in opposite directions. The harness checks the proposition exactly on 3030 calibrated worlds, and on the trigram’s own rows with outcomes drawn from the model, where coverage matches kept mass within four standard errors. A guarantee of coverage that does not need calibration at all comes from choosing the threshold on held-out data, which is split conformal prediction.


On the corpus

Both engines are the sampling topic’s. Topic 3’s interpolated Kneser–Ney trigram has 3,4273{,}427 outcomes, 3,4253{,}425 word types plus </s> and <unk>. Topic 14’s two-block decoder has 512512 tokens, 509509 words plus <s>, </s>, and <unk>, and <s> is never scored. Here both score every token of a text, so the unknown-word token <unk> is an outcome like any other; the decoder sees only the first 3232 positions of each sentence. Three test sets:

  • continuation: the generator that wrote the training corpus, run on for 100100 more documents. The same source: a best case, in which 486486 of its 2,0432{,}043 sentences repeat a training sentence word for word.
  • held: Topic 3’s held-out split, a fresh run of the generator whose word frequencies differ from the training text’s.
  • EDGAR: the public MD&A excerpts, real filings, 3333 sentences and 830830 trigram tokens.

The table gives mean confidence and accuracy, the ECE at 1515 bins beside the calibrated twin’s floor, the NLL-optimal temperature τ∗\tau^*, and the coverage and kept mass of the nucleus of mass 0.90.9.

engine, test setconf. / acc.ECE / twinτ∗\tau^*nucleus cover / kept
trigram, continuation0.5220.522 / 0.5270.5270.0180.018 / 0.0060.0061.0031.0030.9260.926 / 0.9390.939
trigram, held0.5120.512 / 0.5000.5000.0230.023 / 0.0060.0061.0551.0550.8760.876 / 0.9380.938
decoder, continuation0.6530.653 / 0.6610.6610.0160.016 / 0.0060.0060.9570.9570.9510.951 / 0.9450.945
decoder, held0.6490.649 / 0.7070.7070.0640.064 / 0.0060.0060.8240.8240.9670.967 / 0.9440.944

In distribution, both engines are close to calibrated, and a cross-fitted temperature recovers almost nothing: under 0.00010.0001 nats for the trigram, 0.0020.002 for the decoder. The trigram’s reliability diagram has a shape, though. Bins with confidence between 1/31/3 and 0.60.6 are overconfident and every bin above 2/32/3 is underconfident. Kneser–Ney’s discounts, which take mass from what was seen and give it to what was not, explain half of that. Refit the trigram with scaled discounts and score the same tokens in every column (gaps are accuracy minus confidence):

discount, ×\times Ney’s0.10.10.250.250.50.511capped at 0.990.99
τ∗\tau^*1.5421.5421.3391.3391.1761.1761.0031.0030.9790.979
gap, 1/31/3 to 0.60.6−0.070-0.070−0.062-0.062−0.048-0.048−0.026-0.026−0.023-0.023
gap, above 2/32/3−0.006-0.006−0.004-0.004+0.001+0.001+0.012+0.012+0.016+0.016

The gap above 2/32/3 flips sign with the discount, so that underconfidence is the discounts’ doing. The overconfidence between 1/31/3 and 0.60.6 is there at every discount and shrinks as the discount grows, so it is not their signature. The top bin holds 7,6527{,}652 of the tokens at accuracy 0.9990.999: the generator’s fixed phrases, like “cost → of” and “from its head → office”, which turn up anywhere in a sentence. They are not sentence openers. Of that bin’s tokens, 55%55\% fall at positions 00 to 55, against 53%53\% of all tokens, and only 24%24\% of a sentence’s first three tokens land in it, against 33%33\% overall.

Under the held-out split’s mild shift the two engines move in opposite directions: the trigram becomes slightly overconfident, and the decoder reads as underconfident. Nearly all of the decoder’s gap sits on rows whose top label is <unk> (3,6493{,}649 tokens, confidence 0.4560.456, accuracy 0.7850.785), as <unk> rises from 10.3%10.3\% of its tokens to 16.0%16.0\%. Rows with a word on top read 0.6860.686 and 0.6930.693. Shift does not have a sign.



Where this sits

Everything on this page is the calibration of one next-token distribution at a time. The calibration of whole generations is a different property: Braverman and colleagues (2020) show that the entropy rate of a model’s samples drifts upward with length, the long-horizon face of Corollary 2, and correct it with a one-parameter fit of their own. Desai and Durrett (2020) find pre-trained transformers calibrated in domain and less so out of domain, the pattern of the table above, and Ovadia and colleagues (2019) show that calibration measured on in-distribution data does not survive dataset shift. For large models the question moves to the confidence of a chosen answer: Kadavath and colleagues (2022) measure it on multiple-choice questions and on the model’s own judgments of its answers, and the GPT-4 report (OpenAI 2023, Figure 8) finds the pre-trained model well calibrated and the post-trained one much less so. Neither finding is a case of Theorem 3, whose hypotheses ask for a stationary point of a penalized likelihood and a direction that scales the logits. What to do with a calibrated confidence, answering or abstaining, is the formalRAG selective-generation topic.


Honest caveats


Implementation

The companion notebook (notebookPath) imports language_model_calibration.py, which owns every number on this page. That module imports the sampling topic’s module for temperature, the nucleus, and the fact model, and through it the beam-search topic’s trigram and trained decoder. It implements finite worlds with exact recalibration, the plug-in and debiased estimators and the calibrated twin, a Newton temperature fit on deduplicated rows, the log-linear bigram fit by L-BFGS, and per-temperature statistics for both engines on all three test sets. A harness of 4141 assertion-based tests runs in about two and a half minutes on a laptop CPU. The tests cover:

  • Proposition 1, Theorem 1, and Corollary 1 exactly on random finite worlds, and Proposition 1’s counterexample.
  • Theorem 2(a) on nested bins, (b) and (c) by exact Poisson-binomial expectations, and the calibrated twin’s floor on the corpus.
  • Proposition 2’s derivatives against finite differences, the Newton fit’s stationarity on every set and against a scalar search on EDGAR, the invariance of accuracy, and the reordering of confidences by rows and by tokens.
  • Theorem 3’s identity on the log-linear bigram at ten penalties, where held-out temperatures land on that grid, and the decoder’s temperature across four seeds with its nonzero gradient.
  • Proposition 3 exactly on calibrated worlds and on the trigram’s rows, Corollary 2, and the fast nucleus against the sampling topic’s own.
  • The in-distribution, held-out, and EDGAR findings, the Kneser–Ney reliability shape and the discount refits, the known-word cut, <unk>’s rank and the coverage ceiling, the map onto <unk>, and the fact regimes, including that no row mixes them.

The laboratory reads its data from a JSON file that one of those tests requires to equal the module’s viz_payload() exactly, so the figure cannot drift from the code.

References & Further Reading