Probability Calibration › Formula
Machine Learning from ScratchAll chapters 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
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 , and let a model produce a score for observation . We treat as a claimed probability that . 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:
where:
- : the binary outcome, with for a positive case and for a negative case
- : the model's predicted probability for a case
- : 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 ,
where:
- : the binary outcome
- : the calibrated forecast probability, treated as a random variable
- : the conditional positive rate as a function of the forecast
Taking expectations over the forecast distribution gives a useful consequence:
where:
- : the overall positive rate (base rate) of the population
- : the conditional positive rate given forecast
- : 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:
Here and are independent draws from the positive and negative populations, both of which must occur, and 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 , a scoring rule is proper if the following inequality holds for every reported probability :
where:
- : the score assigned to reported probability under outcome
- : the true probability that
- : the reported probability we are evaluating
- : the expectation over outcomes drawn from a Bernoulli distribution with success probability
For a strictly proper rule, equality holds only when . The Brier score is the mean squared error of the probabilities:
where:
- : the number of cases
- : the predicted probability for case
- : the observed outcome for case , coded as 1 for positive and 0 for negative
To see why it is proper, fix a true probability and suppose we report . The expected squared error is
where:
- : the reported probability
- : the outcome, which is 1 with probability and 0 with probability
- : the true probability that
The second term does not depend on , and the first term is minimized at . Log loss is the negative log likelihood:
For a true probability and reported , the expected log loss is
where:
- : the true probability that
- : the reported probability
For and , differentiating with respect to gives
The minimum is unique because the second derivative is in the interior. For or , the minimum occurs at the matching endpoint, using the usual 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 , 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]Expand
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]Expand

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 contains cases, with mean predicted probability
and observed positive frequency
where:
- : the index of the reliability bin
- : the number of cases in bin
- : the predicted probability for case
- : the observed outcome for case
- : the mean predicted positive-event probability in bin
- : the observed positive rate in bin , rather than thresholded classification accuracy
We plot on the horizontal axis and 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:
where:
- : the number of bins
- : the number of cases in bin
- : the total number of cases
- : the observed positive rate in bin
- : the mean predicted probability in bin
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 is represented by the bin mean , the Brier score decomposes as
where:
- : the Brier score of the binned forecasts
- : the reliability term, the calibration penalty
- : the resolution term, the discrimination reward
- : the score of the empirical constant base-rate forecast
and the reliability term is
the resolution term is
where:
- : the number of cases in bin
- : the total number of cases
- : the observed positive rate in bin
- : the overall base rate, the fraction of positive cases
and the uncertainty term is
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:
where:
- : the Brier skill score relative to the base-rate forecast
- : the Brier score of the model being evaluated
- : 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 , we can work on its logit scale:
where:
- : the raw probability output by the base model for case
- : the logit of that probability, which is the input scale for sigmoid recalibration
- : the log-odds transformation
The calibrated probability is
where:
- : the calibrated probability for case
- : the logit of the raw model probability
- : the slope parameter on the logit scale
- : the intercept parameter on the logit scale
- : the logistic sigmoid function
The parameters and are fitted on the calibration set by minimizing log loss:
where:
- : the negative log likelihood on the calibration set as a function of the slope and intercept
- : the number of calibration cases
- : the calibrated probability for case , equal to
- : the observed outcome for case
The derivative of the logistic sigmoid satisfies , and the derivative of is . Applying the chain rule gives
where:
- : the negative log likelihood from above
- : the calibrated probability for case
- : the observed outcome for case
- : the logit of the raw model probability for case
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 and , the map is the identity. With and , the map moves probabilities toward one half; with and , it moves them toward the endpoints. A nonzero intercept shifts the curve. Positive preserves order, negative reverses it, and 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 .
Isotonic recalibration is nonparametric. It seeks a nondecreasing function that minimizes
where:
- : the fitted nondecreasing mapping from raw score to calibrated probability
- : the fitted calibrated probability for case
- : the raw score for case
- : the observed outcome for case
- : the number of calibration cases
- : 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 . This follows from the law of total expectation. Calibration is not preserved by arbitrary transformations: if is calibrated and we apply a nonlinear function , the transformed value 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 , the expected Brier score is . 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!