Probability Calibration › Formula

Michael BrenndoerferOctober 29, 202613 min read
Machine Learning from ScratchAll chapters
Part 12 of 15Calibration and Prediction Uncertainty
Chapter 76Probability Calibration › Formula
Reading section 4 of 10 · Formula · 13 minSection 4 of 10 · 13 min

Section 4 of 10 · 13 min read

Probability Calibration › FormulaSection view · Sections in this chapter4 / 10 ↓
Reading tools Definitions, links & notes

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Notes & comments

Calibration is easiest to understand as a promise about conditional frequency. Imagine collecting every case for which the model says 0.30 and asking what fraction of those cases turned out positive. A calibrated model keeps that promise: the fraction should be about 0.30. To state the promise formally, let the binary outcome be Y∈{0,1}Y \in \{0,1\}, and let a model produce a score pip_i for observation ii. We treat pip_i as a claimed probability that Yi=1Y_i=1. Population calibration means that the conditional positive probability equals the forecast, almost surely under the forecast distribution. Among independently sampled cases from the target population, a finite group's observed event fraction fluctuates around that probability:

Pr⁡(Y=1∣ppred=p)=pfor almost every p under the forecast distribution.\Pr(Y=1 \mid p_{\text{pred}}=p)=p \quad \text{for almost every } p \text{ under the forecast distribution.}

where:

  • YY: the binary outcome, with Y=1Y=1 for a positive case and Y=0Y=0 for a negative case
  • ppredp_{\text{pred}}: the model's predicted probability for a case
  • pp: a specific predicted-probability value at which we inspect the conditional positive rate

This condition applies across the probability scale. Equivalently, for a calibrated forecast PP,

E[Y∣P]=Palmost surely.\mathbb{E}[Y \mid P]=P \quad \text{almost surely}.

where:

  • YY: the binary outcome
  • PP: the calibrated forecast probability, treated as a random variable
  • E[Y∣P]\mathbb{E}[Y \mid P]: the conditional positive rate as a function of the forecast PP

Taking expectations over the forecast distribution gives a useful consequence:

E[Y]=E[E[Y∣P]]=E[P].\mathbb{E}[Y]=\mathbb{E}\big[\mathbb{E}[Y\mid P]\big]=\mathbb{E}[P].

where:

  • E[Y]\mathbb{E}[Y]: the overall positive rate (base rate) of the population
  • E[Y∣P]\mathbb{E}[Y\mid P]: the conditional positive rate given forecast PP
  • E[P]\mathbb{E}[P]: the average predicted probability across the population

So a calibrated model must have average predicted probability equal to the base rate. This is necessary but not sufficient. A constant predictor that always outputs the base rate is calibrated, but it has no discrimination.

Calibration, discrimination, and proper scoring rules

A model can be useful in two different ways, and calibration addresses only one of them. Calibration asks whether the probability scale matches event rates: among cases assigned 0.8, is the positive-event rate about 80 percent? Discrimination asks whether the model separates positives from negatives: do positives tend to receive higher scores than negatives? AUC measures ranking:

AUC=Pr⁡(s(X+)>s(X−))+12Pr⁡(s(X+)=s(X−)),\mathrm{AUC}=\Pr\big(s(X^{+})>s(X^{-})\big) +\frac12\Pr\big(s(X^{+})=s(X^{-})\big),

Here X+X^{+} and X−X^{-} are independent draws from the positive and negative populations, both of which must occur, and ss is the score. AUC gives half credit to a tie. It depends only on score order and ties, so any strictly increasing transformation of the scores leaves AUC unchanged. A model can have high AUC and poor calibration. For example, it might assign scores near 0.99 to positives and near 0.98 to negatives. The ranking is nearly perfect, but the probabilities are not trustworthy. Conversely, the constant population base-rate forecast is calibrated and has AUC equal to 0.5 when both classes occur.

If we want a single number to reward honest probabilities, we need a loss whose best possible report is the truth. That is the idea of a proper scoring rule. For a binary outcome with true probability pp, a scoring rule S(q,y)S(q,y) is proper if the following inequality holds for every reported probability qq:

EY∼Bernoulli(p)[S(q,Y)]≥EY∼Bernoulli(p)[S(p,Y)]\mathbb{E}_{Y\sim \mathrm{Bernoulli}(p)}[S(q,Y)] \ge \mathbb{E}_{Y\sim \mathrm{Bernoulli}(p)}[S(p,Y)]

where:

  • S(q,Y)S(q,Y): the score assigned to reported probability qq under outcome YY
  • pp: the true probability that Y=1Y=1
  • qq: the reported probability we are evaluating
  • EY∼Bernoulli(p)[⋅]\mathbb{E}_{Y\sim \mathrm{Bernoulli}(p)}[\cdot]: the expectation over outcomes drawn from a Bernoulli distribution with success probability pp

For a strictly proper rule, equality holds only when q=pq=p. The Brier score is the mean squared error of the probabilities:

BS=1n∑i=1n(pi−yi)2.\mathrm{BS} = \frac{1}{n}\sum_{i=1}^{n}(p_i-y_i)^2.

where:

  • nn: the number of cases
  • pip_i: the predicted probability for case ii
  • yiy_i: the observed outcome for case ii, coded as 1 for positive and 0 for negative

To see why it is proper, fix a true probability pp and suppose we report qq. The expected squared error is

E[(q−Y)2∣p]=p(1−q)2+(1−p)q2=p(1−2q+q2)+(1−p)q2=p−2pq+pq2+q2−pq2=(q−p)2+p(1−p).\begin{aligned} \mathbb{E}[(q-Y)^2\mid p] &= p(1-q)^2+(1-p)q^2 \\ &= p(1-2q+q^2)+(1-p)q^2 \\ &= p - 2pq + pq^2 + q^2 - pq^2 \\ &= (q-p)^2+p(1-p). \end{aligned}

where:

  • qq: the reported probability
  • YY: the outcome, which is 1 with probability pp and 0 with probability 1−p1-p
  • pp: the true probability that Y=1Y=1

The second term does not depend on qq, and the first term is minimized at q=pq=p. Log loss is the negative log likelihood:

LL=−1n∑i=1n[yilog⁡pi+(1−yi)log⁡(1−pi)].\mathrm{LL} = -\frac{1}{n}\sum_{i=1}^{n} \left[ y_i\log p_i+(1-y_i)\log(1-p_i) \right].

For a true probability pp and reported qq, the expected log loss is

−plog⁡q−(1−p)log⁡(1−q).-p\log q-(1-p)\log(1-q).

where:

  • pp: the true probability that Y=1Y=1
  • qq: the reported probability

For 0<p<10<p<1 and 0<q<10<q<1, differentiating with respect to qq gives

−pq+1−p1−q=0⟺−p(1−q)+(1−p)q=0⟺−p+pq+q−pq=0⟺q=p.\begin{aligned} -\frac{p}{q}+\frac{1-p}{1-q}&=0 \\ \Longleftrightarrow\quad -p(1-q)+(1-p)q&=0 \\ \Longleftrightarrow\quad -p+pq+q-pq&=0 \\ \Longleftrightarrow\quad q&=p. \end{aligned}

The minimum is unique because the second derivative is p/q2+(1−p)/(1−q)2>0p/q^2+(1-p)/(1-q)^2>0 in the interior. For p=0p=0 or p=1p=1, the minimum occurs at the matching endpoint, using the usual 0log⁡0=00\log0=0 limit. Log loss therefore has the same properness property. It penalizes confident mistakes much more strongly than the Brier score because the logarithm is unbounded near zero. Thresholded accuracy cannot identify the true probability: many reports on the same side of the threshold give the same label. At threshold one half, the corresponding zero-one loss is weakly proper under the definition above, but it is not strictly proper. AUC is a pairwise ranking measure rather than a per-observation Bernoulli loss S(q,y)S(q,y), and it does not identify the numerical probability scale.

The properness condition has a visual signature: for a fixed true probability, the expected Brier score is smallest when the reported probability equals the truth. The next figure draws that expected loss for three true probabilities and marks each minimum.

PythonIn [3]
CollapseExpand
import numpy as np

q_grid = np.linspace(0.01, 0.99, 200)
true_probs = [0.2, 0.5, 0.8]
brier_curves = [(q_grid - p) ** 2 + p * (1.0 - p) for p in true_probs]
VisualizationOut [4]
CollapseExpand
Expected Brier score curves for three true probabilities, with minima at each true value.
Expected Brier score as a function of the reported probability for three true probabilities. Each curve reaches its minimum at reported probability equal to the true probability.

Reliability diagrams and Brier score

The formulas above describe an ideal conditional frequency. In practice we estimate that frequency by grouping predictions into bins and comparing the average prediction in each bin with the observed positive rate. A reliability diagram is the graphical version of that comparison, and it is often the first diagnostic we inspect. Suppose bin bb contains nbn_b cases, with mean predicted probability

confb=1nb∑i∈bpi\mathrm{conf}_b=\frac{1}{n_b}\sum_{i\in b}p_i

and observed positive frequency

accb=1nb∑i∈byi.\mathrm{acc}_b=\frac{1}{n_b}\sum_{i\in b}y_i.

where:

  • bb: the index of the reliability bin
  • nbn_b: the number of cases in bin bb
  • pip_i: the predicted probability for case ii
  • yiy_i: the observed outcome for case ii
  • confb\mathrm{conf}_b: the mean predicted positive-event probability in bin bb
  • accb\mathrm{acc}_b: the observed positive rate in bin bb, rather than thresholded classification accuracy

We plot confb\mathrm{conf}_b on the horizontal axis and accb\mathrm{acc}_b on the vertical axis. The diagonal is perfect calibration. A point below the diagonal means the model overpredicts the positive-event rate; a point above means it underpredicts that rate. Calling either direction overconfidence across the whole binary probability scale would confuse positive-event risk with confidence in the more likely class. The number of bins and the binning strategy matter: equal-width bins can put very few observations in extreme bins, while equal-frequency bins can hide poor calibration near the extremes. A reliability diagram is a diagnostic, not a single-number score.

The expected calibration error summarizes the same binned comparison:

ECE=∑b=1Bnbn∣accb−confb∣.\mathrm{ECE} = \sum_{b=1}^{B} \frac{n_b}{n} \left| \mathrm{acc}_b-\mathrm{conf}_b \right|.

where:

  • BB: the number of bins
  • nbn_b: the number of cases in bin bb
  • nn: the total number of cases
  • accb\mathrm{acc}_b: the observed positive rate in bin bb
  • confb\mathrm{conf}_b: the mean predicted probability in bin bb

ECE depends on the binning scheme. Merging bins can cancel oppositely signed gaps and reduce ECE, even when the individual forecasts are unchanged. Tied scores can also make quantile-bin counts unequal or reduce the number of distinct bins. It should not be used as the sole calibration metric. For binned forecasts, where each case in bin bb is represented by the bin mean confb\mathrm{conf}_b, the Brier score decomposes as

BSbin=REL−RES+UNC,\mathrm{BS}_{\text{bin}} = \mathrm{REL}-\mathrm{RES}+\mathrm{UNC},

where:

  • BSbin\mathrm{BS}_{\text{bin}}: the Brier score of the binned forecasts
  • REL\mathrm{REL}: the reliability term, the calibration penalty
  • RES\mathrm{RES}: the resolution term, the discrimination reward
  • UNC\mathrm{UNC}: the score of the empirical constant base-rate forecast

and the reliability term is

REL=1n∑b=1Bnb(confb−accb)2,\mathrm{REL} = \frac{1}{n}\sum_{b=1}^{B}n_b \left(\mathrm{conf}_b-\mathrm{acc}_b\right)^2,

the resolution term is

RES=1n∑b=1Bnb(accb−yˉ)2,\mathrm{RES} = \frac{1}{n}\sum_{b=1}^{B}n_b \left(\mathrm{acc}_b-\bar y\right)^2,

where:

  • nbn_b: the number of cases in bin bb
  • nn: the total number of cases
  • accb\mathrm{acc}_b: the observed positive rate in bin bb
  • yˉ\bar y: the overall base rate, the fraction of positive cases

and the uncertainty term is

UNC=yˉ(1−yˉ).\mathrm{UNC}=\bar y(1-\bar y).

This identity is exact for the forecasts after every score in a bin is replaced by that bin's mean. It need not equal the Brier score of the original varying scores. Reliability is a penalty: it is large when predicted and observed frequencies disagree. Resolution is large when bins have event rates far from the overall rate. It measures this bin-level refinement, rather than ROC-AUC. Uncertainty is the Brier score of the empirical constant base-rate forecast on these observations. A useful summary is the Brier skill score relative to the base-rate forecast:

BSS=1−BSBSref,\mathrm{BSS} = 1-\frac{\mathrm{BS}}{\mathrm{BS}_{\text{ref}}},

where:

  • BSS\mathrm{BSS}: the Brier skill score relative to the base-rate forecast
  • BS\mathrm{BS}: the Brier score of the model being evaluated
  • BSref\mathrm{BS}_{\text{ref}}: the Brier score of the reference base-rate forecast, required to be positive

BSS is undefined when the reference score is zero. A reference using the evaluation sample's own base rate is a descriptive benchmark; a deployable constant forecast must be estimated before final evaluation.

Sigmoid and isotonic recalibration

Once the reliability diagram shows miscalibration, the next question is how to repair the probability scale without discarding the model. The repair is a mapping from raw score to calibrated probability, learned on held-out data. Sigmoid recalibration, often called Platt scaling, fits a two-parameter monotone map from the raw score to a calibrated probability. When the raw model outputs a probability 0<pi<10<p_i<1, we can work on its logit scale:

zi=logit⁡(pi)=log⁡pi1−pi.z_i=\operatorname{logit}(p_i)=\log\frac{p_i}{1-p_i}.

where:

  • pip_i: the raw probability output by the base model for case ii
  • ziz_i: the logit of that probability, which is the input scale for sigmoid recalibration
  • logit⁡(⋅)\operatorname{logit}(\cdot): the log-odds transformation

The calibrated probability is

qi=σ(azi+b)=11+e−(azi+b).q_i=\sigma(a z_i+b) = \frac{1}{1+e^{-(a z_i+b)}}.

where:

  • qiq_i: the calibrated probability for case ii
  • ziz_i: the logit of the raw model probability
  • aa: the slope parameter on the logit scale
  • bb: the intercept parameter on the logit scale
  • σ(⋅)\sigma(\cdot): the logistic sigmoid function

The parameters aa and bb are fitted on the calibration set by minimizing log loss:

ℓ(a,b)=−∑i=1n[yilog⁡qi+(1−yi)log⁡(1−qi)].\ell(a,b) = -\sum_{i=1}^{n} \left[ y_i\log q_i+(1-y_i)\log(1-q_i) \right].

where:

  • ℓ(a,b)\ell(a,b): the negative log likelihood on the calibration set as a function of the slope and intercept
  • nn: the number of calibration cases
  • qiq_i: the calibrated probability for case ii, equal to σ(azi+b)\sigma(a z_i+b)
  • yiy_i: the observed outcome for case ii

The derivative of the logistic sigmoid satisfies σ′(η)=σ(η)(1−σ(η))\sigma'(\eta)=\sigma(\eta)(1-\sigma(\eta)), and the derivative of log⁡σ(η)\log\sigma(\eta) is 1−σ(η)1-\sigma(\eta). Applying the chain rule gives

∂ℓ∂a=∑i=1n(qi−yi)zi,∂ℓ∂b=∑i=1n(qi−yi).\frac{\partial \ell}{\partial a} = \sum_{i=1}^{n}(q_i-y_i)z_i, \qquad \frac{\partial \ell}{\partial b} = \sum_{i=1}^{n}(q_i-y_i).

where:

  • ℓ\ell: the negative log likelihood from above
  • qiq_i: the calibrated probability for case ii
  • yiy_i: the observed outcome for case ii
  • ziz_i: the logit of the raw model probability for case ii

Setting these derivatives to zero gives the first-order conditions. The general first-order system has no direct closed-form solution, so we use a numerical optimizer such as BFGS. With separated calibration labels, an unregularized finite optimum need not exist. When a=1a=1 and b=0b=0, the map is the identity. With b=0b=0 and 0<a<10<a<1, the map moves probabilities toward one half; with b=0b=0 and a>1a>1, it moves them toward the endpoints. A nonzero intercept shifts the curve. Positive aa preserves order, negative aa reverses it, and a=0a=0 gives a constant. Our unconstrained fit must therefore be checked before invoking an order-preservation claim. If the raw score is a margin rather than a probability, as with a support vector machine, the same idea applies directly as q=σ(as+b)q=\sigma(a s+b).

Isotonic recalibration is nonparametric. It seeks a nondecreasing function gg that minimizes

∑i=1n(yi−g(pi))2subject topi≤pj⟹g(pi)≤g(pj).\sum_{i=1}^{n}\left(y_i-g(p_i)\right)^2 \quad\text{subject to}\quad p_i\le p_j \Longrightarrow g(p_i)\le g(p_j).

where:

  • g(⋅)g(\cdot): the fitted nondecreasing mapping from raw score to calibrated probability
  • g(pi)g(p_i): the fitted calibrated probability for case ii
  • pip_i: the raw score for case ii
  • yiy_i: the observed outcome for case ii
  • nn: the number of calibration cases
  • pi≤pj⟹g(pi)≤g(pj)p_i\le p_j \Longrightarrow g(p_i)\le g(p_j): the monotonicity constraint ensuring a higher raw score does not produce a lower calibrated probability

The monotonicity constraint encodes the reasonable requirement that a higher raw score should not produce a lower calibrated probability. The pool adjacent violators algorithm solves this problem. We first aggregate equal scores so they receive the same fitted value, sort the distinct scores, and merge adjacent weighted blocks whenever their means violate monotonicity. The fitted value for each block is its weighted mean outcome. PAVA gives block-constant fitted values at the observed scores. Our predictor linearly interpolates between those score thresholds; any monotone extension matching the fitted points has the same training objective. Isotonic regression is flexible and can correct complex miscalibration, but it requires more calibration data and can overfit. Outside the range of observed scores it must extrapolate, typically by holding the boundary value constant.

Both methods are post hoc. They use the calibration set to learn a mapping and then apply that mapping to new scores. They leave the base model fixed and use only its score. The mapping can alter ties or score orientation, but supplies no new feature information.

Mathematical properties

A calibrated model satisfies E[P]=Pr⁡(Y=1)\mathbb{E}[P]=\Pr(Y=1). This follows from the law of total expectation. Calibration is not preserved by arbitrary transformations: if PP is calibrated and we apply a nonlinear function hh, the transformed value h(P)h(P) need not be calibrated. Discrimination is different: a strictly increasing transformation preserves both ranking and existing ties, hence AUC. Finite-precision saturation or clipping can introduce extra ties.

Proper scoring rules assess the numerical probability forecast. A model can improve its Brier score by becoming better calibrated, by becoming better at separating classes, or both. For the constant population forecast π=Pr⁡(Y=1)\pi=\Pr(Y=1), the expected Brier score is π(1−π)\pi(1-\pi). A finite-sample score need not equal this expectation. A model with strong discrimination but poor calibration can have a worse Brier score than a calibrated model with weaker discrimination. This is why we report calibration diagnostics and proper scores together rather than relying on a single number.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy