A maintenance team inspects machines in order of predicted failure risk and plans its spare parts from the predicted probabilities themselves. Two models can flag exactly the same machines and still disagree about how many failures to expect, so a good ranking alone does not tell the team which numbers to trust. Probability calibration asks whether, among the cases a model gives probability , the event happens about percent of the time. This lesson separates calibration from discrimination, scores probability forecasts with the Brier score and log loss, and fits a calibration map on data that the model never trained on.
Key terms
Probability calibration
Definition Let a classifier give each case with features a predicted probability in for the positive class . The predictions are calibrated in a population of cases if, among the cases that receive the prediction , the proportion with equals ,
for every value that the model produces. When the predictions vary continuously, the condition is stated as with probability . Calibration is a property of the predictions together with a population, so the same model can be calibrated in one population and miscalibrated in another with a different event rate. On a finite sample each observed rate only estimates the conditional probability, and an honest check uses labeled cases that played no part in fitting the model or its calibration.
In plain words A calibrated prediction of means that about percent of the cases given have the event. It does not say that any single case is partly positive, since each outcome is or . A model is overconfident when its probabilities sit closer to and than the observed rates, and underconfident when they sit closer to the middle. Class imbalance on its own does not spoil calibration, and a model for a rare event can be well calibrated. Training methods that change the effective class proportions, such as resampling or class weights, tend to move the probabilities away from the event rate of the population where the model is used.
Example Eight constructed cases have outcomes . Model A predicts for the first four cases and for the last four. Model B predicts and for the same two groups. At the threshold , with a prediction equal to counted as positive, both models label six of the eight cases correctly, so both have accuracy . The observed event rates are in the first group and in the second. Model A matches both rates, while model B is overconfident in both groups. Four cases per group is a teaching example and far too few to judge a real model.
Discrimination
Definition Discrimination is the ability of a score to separate the classes by giving positive cases higher scores than negative cases. It depends only on the order of the scores. A common measure, defined when both classes occur, is the ROC AUC
where is the score of a randomly chosen positive case and the score of an independently chosen negative case. On a data set these probabilities are proportions over all pairs of one positive and one negative case. Applying the same strictly increasing function to every score keeps the order, and therefore the AUC, while it generally changes calibration.
In plain words Discrimination asks which cases are riskier than which. Calibration asks whether the risk numbers themselves are right. A model that gives every case the overall event rate can be calibrated in a stable population and still has no discrimination, since all its scores tie and its AUC is . A model can also rank the cases in exactly the right order while overstating every probability.
Example Models A and B order the eight cases in the same way, so they have the same AUC. There are pairs of a positive and a negative case. Nine pairs have the positive case in the high group and the negative case in the low group, six pairs fall within one group and count half, and one pair has the negative case scored higher. Both models therefore have
Squaring the probabilities of model A gives and . The order and the AUC stay the same, but the group event rates are still and , so the squared predictions are no longer calibrated.
Reliability diagram
Definition A reliability diagram groups cases with similar predicted probabilities and plots, for each group, the average predicted probability on the horizontal axis against the observed fraction of positive cases on the vertical axis. The groups are usually bins of the predictions, either of equal width, such as , and so on, or with equal numbers of cases. Perfect calibration on the plotted groups puts every point on the diagonal from to . A point below the diagonal means the model overstated the event rate in that group, and a point above it means the model understated it. The number of cases in each group belongs with the plot, because a rate from a small group is noisy.
In plain words The diagram puts what the model claimed next to what happened. Points on the diagonal mean the stated probabilities agree with the observed frequencies. Points that lie above the diagonal on the left and below it on the right form a line flatter than the diagonal, the usual sign of overconfidence. The choice of bins can hide a deviation or create an apparent one, so the binning rule should be reported too.
Example Grouping the eight cases by their exact predictions gives model A the points and , both on the diagonal. Model B gives , above the diagonal, and , below it. Each point rests on only four cases. Even for a correct forecast of , a group of four independent cases contains no event with probability , so groups this small often land far from the diagonal by chance.
Brier score and log loss
Definition For cases with outcomes in and predicted probabilities of the outcome , the Brier score is
a number between and . The log loss, also called the mean binary cross-entropy, uses natural logarithms,
so each case contributes minus the logarithm of the probability given to the outcome that occurred. A probability of for an outcome that occurs makes the log loss infinite. Lower is better for both scores. Both are strictly proper, which means that for a case whose true event probability is , the expected score of a forecast is smallest exactly at . When the cases are grouped by their distinct forecast values , with cases and observed event rate in group and overall event rate , the Brier score splits exactly as
The first sum, the reliability term, is zero exactly when every group is calibrated. The second sum, the resolution, grows when the groups have event rates far from the overall rate, and the last term depends only on the outcomes.
In plain words Both scores judge the whole probability forecast at once. They reward probabilities that agree with the observed rates, and they also reward probabilities that separate high-risk cases from low-risk ones. Log loss punishes a confident mistake far more harshly than the Brier score does, without limit as the probability of the observed outcome falls toward . Libraries often clip probabilities away from and to avoid infinite values, which is a numerical safeguard and not a sign that certainty is harmless. A lower score alone therefore does not show better calibration.
Example Model A has squared error on each of its six correct cases and on each of its two mistakes, so its Brier score is . Model B has and , giving . The log losses are
and . In the decomposition both models have resolution and outcome term , while the reliability term is for A and for B. A third model C that predicts for every case is calibrated on these eight cases, yet its Brier score is worse than that of the miscalibrated model B, because C has no resolution. Its log loss is better than that of B, so the two scores can even rank the same two models in opposite orders.
Platt scaling and isotonic regression
Definition Both methods learn a calibration map that sends a base model’s score to a probability , using labeled pairs from cases the base model was not trained on. The score can be a probability, a log-odds value or any other number that increases with risk. Platt scaling, in its basic form, uses
and chooses the two numbers and that minimize the log loss of the against the , which is a logistic regression with the score as its only feature. Platt’s original version also replaces the targets and by values slightly inside the interval, and some libraries follow it. With the map is strictly increasing and keeps the order of the scores. Isotonic regression instead chooses among all nondecreasing functions to minimize
Its fitted values at the calibration scores are unique. The pool adjacent violators algorithm finds them by sorting the distinct scores, starting from the observed event rate at each one, and repeatedly merging neighboring blocks whose rates decrease into one block with their pooled rate. Between and beyond the calibration scores an implementation needs a rule, for example linear interpolation inside the range and the end values outside it.
In plain words Platt scaling bends the scores along one S-shaped curve with a slope and a shift, so it needs little data but can only correct distortions of that shape. Isotonic regression only insists that a higher score never receives a lower probability, so it can follow any monotone distortion, at the price of noisier estimates and more overfitting on a small calibration set. Its steps can give several scores the same probability, and these new ties can change the AUC slightly. An increasing map never puts a lower-scored case above a higher-scored one, so calibration cannot fix a poor ranking.
Example A separate calibration set scored by model B has four cases at with one event and four cases at with three events. The observed rates and already increase, so isotonic regression returns them unchanged. Basic Platt scaling on the log-odds score , which equals or here, fits and
which maps the two groups to exactly and as well, since two free numbers can match two increasing rates. A slope below on the log-odds scale pulls overconfident probabilities toward . Applied to the eight evaluation cases, either map turns the predictions of model B into those of model A, so the calibrated model has Brier score and log loss about . The data were constructed to make this agreement exact. On real data a calibration map is an estimate, and it can make predictions on new cases worse.
Calibration set
Definition A calibration set is a labeled set of cases used only to fit a calibration map, after the base model is trained and before the final evaluation. None of its cases may have been used to train the base model, and it is kept apart from the test set that measures the finished system. It should come from the population where the probabilities will be used, with that population’s class proportions, and with grouped or time-ordered data the split respects the groups or the time order. An alternative to a separate set is to calibrate on cross-validated predictions for the training data, where each training case is scored by a model fitted on folds that exclude it, so no case’s label trained the model that scored it. Choosing between calibration methods is also part of fitting and uses training or validation data, never the test set.
In plain words A model is usually more accurate and more confident on its own training cases than on new ones, because it has adapted to their labels. A calibrator fitted to those predictions learns to trust the model too much. Resampling and class weights belong to training only, the calibration and test sets keep the class mix of the deployed population, and a decision threshold is chosen afterward from the costs of errors without touching the test set.
Example A failure model is trained after the rare failures were oversampled until the two classes were equal in number. Suppose its probabilities are correct for that balanced mix, oversampling leaves the feature distribution within each class unchanged, and the failure rate in service is . A probability on the balanced scale then converts to the service scale by multiplying its odds by , so a prediction of becomes and a prediction of becomes about . A calibration set drawn from service data, with its natural failure rate of percent, lets Platt scaling or isotonic regression learn such a correction instead of assuming it. Raising the decision threshold would change which machines are flagged and leave every one of these probabilities wrong.
Check yourself
- A model predicts probability for each of ten cases in a separate evaluation group, and four of the ten have the event. Find the reliability point and the Brier score of the group. Does moving the decision threshold from to repair the calibration?
Show the answer
The reliability point is , below the diagonal, so the model overstated the event rate in this group. Each of the four events contributes and each of the six non-events contributes , so
Moving the threshold to changes all ten labels from positive to negative but leaves every probability at , so the reliability point and the Brier score do not change. A threshold acts on decisions, and calibration is a property of the probabilities. Ten cases are also weak evidence. If the true event probability were , four or fewer events among ten independent cases would occur with probability about , so this group justifies a closer look rather than a verdict. Fitting a calibrator to these ten cases and then reporting them as fresh evidence would be leakage.
- A calibration set has five cases at each of the scores , , and , with , , and events. Find the isotonic regression fit at the four scores. Why should these cases be kept out of the base model’s training data?
Show the answer
The observed rates are , , and . The rate falls from to between the second and third scores, so those two blocks are pooled into one with rate . The values , , and are nondecreasing, so the fit is
Its sum of squared errors is , slightly above the of the raw rates, which break the ordering. The scores and now share a probability. If these cases had trained the base model, its scores on them would reflect labels it had already fitted, typically looking more extreme and more accurate than on new cases. The calibrator would then learn a map that trusts the model too much and would be overconfident on new data.
- A team has records of machines, percent of which failed. It oversamples the failures in the whole data set until half of the records are failures, splits the result at random into training, calibration and test sets, trains a classifier, fits isotonic regression on the calibration set, picks the threshold with the best F1 score on the test set, and reports the test Brier score. Find the problems and describe a sound order of steps.
Show the answer
Oversampling before splitting can put copies of the same failed machine into several sets, so the test set contains exact copies of training failures and the test results are optimistic. The calibration and test sets also have percent failures instead of the percent seen in service, so the isotonic map is calibrated to the wrong population and its probabilities will be much too high in service, while the reported Brier score describes the balanced mix. Choosing the threshold on the test set uses the test labels for a decision, so the test F1 score is optimistic as well.
A sound order splits the original records first, by machine if a machine has several records and by time if the records are ordered in time. Oversampling, if it is used at all, is applied only to the training part, inside each training fold when cross-validation is used. The calibration and test sets keep the natural percent failure rate. The calibrator and the threshold are fitted on the calibration or validation data, the threshold from the costs of the two kinds of error, and the test set is used once to report the Brier score or log loss with a reliability diagram, alongside measures of discrimination such as ROC AUC. The imbalance itself was never the problem. The leaks came from the order of the steps.