Build a reliability table with honest bin counts, event rates and a fixed evaluation cohort.
Probability calibration plots: compare predicted risk with observed frequency
Separate ranking from calibrated probability
A score can rank likely events well yet overstate their chance. A calibration plot asks whether cases assigned a given probability experience the event at approximately that frequency. For an equipment-alert model, a group with average predicted failure probability 0.31 should have an observed failure share near 0.31 over a sufficiently large, representative evaluation set. This is not a claim about any single device. Regression overlays often invite a best-fit line; a calibration plot instead compares observed and predicted rates against the identity diagonal. Ranking metrics, decision thresholds and the cost of false alarms answer different questions and should be displayed separately.
Construct bins without concealing support
Partition predictions with a stated rule. Equal-width bins have stable numeric boundaries but may leave some bins thin; equal-count bins balance support yet their boundaries move between cohorts. For each nonempty bin, plot mean predicted probability on x and observed event fraction on y. Include count and event count in a table or label, because a point based on 7 cases is not as stable as one based on 700. Do not join empty bins with a line. The code uses fixed equal-width bins and makes p=1 belong to the final bin. Small-sample displays explain why approximate agreement needs interval context, especially near the extremes.
Protect the evaluation split
Calibration estimated on the same records used to fit or tune a model is optimistic. Hold out an evaluation period or cohort and keep its definition stable. If scores are recalibrated, fit that transformation on a separate calibration set, then evaluate it on untouched data. Log model version, score timestamp, outcome window and censoring or delayed-label policy. A device marked as no failure before its observation window closes is not a verified negative. Provisional periods must be separated from complete labels. Also show prevalence: when base failure rate shifts between cohorts, the curve may move even if the model's ranking stays useful.
Inspect slices and drift carefully
A global curve can hide one badly miscalibrated plant or device family. Compare slices only when each has enough cases, and keep the same bin boundaries and evaluation window. Report omitted records and missing scores. Point movement between months may be sampling noise, model drift or a change in who gets scored; the plot alone cannot distinguish them. Small multiples with shared scales make comparisons less deceptive, while an export manifest keeps model and data versions attached to the image. Provide the underlying bin table so readers can check the plotted rates rather than measuring the diagonal by eye.
Worked contract
from math import isfinite
def calibration_bins(records: list[tuple[str, float, bool]], width: int = 5):
if type(width) is not int or width <= 0:
raise ValueError("bin count must be positive")
identifiers = [device_id for device_id, _, _ in records]
if len(identifiers) != len(set(identifiers)) or any(not item for item in identifiers):
raise ValueError("device IDs must be unique and nonempty")
bins = [[0, 0.0, 0] for _ in range(width)]
for _, probability, failed in records:
if (not isfinite(probability) or not 0 <= probability <= 1
or type(failed) is not bool):
raise ValueError("probabilities and event flags must be valid")
bucket = min(int(probability * width), width - 1)
bins[bucket][0] += 1
bins[bucket][1] += probability
bins[bucket][2] += failed
return [(bucket, count, score_sum / count, events / count, events)
for bucket, (count, score_sum, events) in enumerate(bins)
if count]
inspection_results = [("unit-47", 0.22, False),
("unit-59", 0.31, True),
("unit-83", 0.37, False),
("unit-97", 1.0, True)]
reliability = calibration_bins(inspection_results)
assert reliability[0] == (1, 3, 0.3, 1 / 3, 1)
assert reliability[1] == (4, 1, 1.0, 1.0, 1)Performance and operating cost
For N predictions and B fixed bins, accumulation takes O(N + B) time and O(B + N) space because unique IDs are validated. Empty bins are omitted rather than invented. The four-record check demonstrates bin assignment and arithmetic, not credible calibration evidence; real evaluation needs sufficient support, a held-out cohort and uncertainty intervals. The function does not estimate those intervals or repair delayed labels.
Common Mistakes
- Using training records as the final reliability evaluation.
- Plotting one bin point without its case count or event count.
- Calling a high-ranking score calibrated without comparing observed frequency.
- Treating unfinished outcome windows as verified negatives.
Validation drill
Hold out 470 device predictions. Build fixed bins for two months, print counts and event rates, then identify whether a shifted curve also coincides with changed prevalence or incomplete labels.
Read next
- Uncertainty on charts: show denominators and intervals beside estimates
- Small multiples: compare groups with honest scales and facet order
- Time-series charts: distinguish missing days, zeros and provisional periods
- Chart exports: carry the data, filters and render contract together
