Back to Blog
Lesson 48 of the AI/ML Foundations: Core Concepts & First Models course
AI/MLJune 25, 20264 min read

Evaluating Model Calibration: Accuracy Beyond Just Predictions

Learn how to evaluate model calibration using calibration curves and the Brier score. Ensure your predicted probabilities are accurate representations of reality.

calibrationBrier scoremachine learningmodel evaluationdata scienceaimachine-learningpython

Previously in this course, we covered Managing Model Complexity: Pruning and Regularization Strategies to prevent overfitting. Now that your model is stable, we need to answer a critical question: when your model says there is an 80% chance of an event occurring, does it happen 80% of the time?

Many beginners treat classification models as simple "Yes/No" machines. In production, however, we often care about the confidence of that prediction. If your model is poorly calibrated, a high probability might not mean high certainty, which can lead to disastrous business decisions.

Understanding Calibration and the Brier Score

A model is calibrated if its predicted probabilities align with the actual observed frequency of the positive class. If you take all samples where the model predicted 0.7 probability, approximately 70% of those samples should be positive.

To measure this, we use two primary tools:

  1. The Brier Score: This measures the mean squared difference between the predicted probability and the actual outcome (0 or 1). A lower Brier score is better. Unlike accuracy, which only checks the final label, the Brier score penalizes models that are "confidently wrong."
  2. Calibration Curves (Reliability Diagrams): This is a visual plot. We divide the predicted probabilities into "bins" (e.g., 0-0.1, 0.1-0.2) and plot the mean predicted probability of each bin against the actual fraction of positives in that bin. A perfectly calibrated model follows a 45-degree diagonal line.

Working Example: Visualizing Calibration

Let's use scikit-learn to evaluate the calibration of a classifier.

PYTHON
import numpy as np
from sklearn.calibration import calibration_curve, CalibrationDisplay
from sklearn.metrics import brier_score_loss
import matplotlib.pyplot as plt

# Assume y_test are true labels and y_prob are model probabilities
# y_prob = model.predict_proba(X_test)[:, 1]

# 1. Calculate Brier Score
brier = brier_score_loss(y_test, y_prob)
print(f"Brier Score: {brier:.4f}")

# 2. Generate Calibration Curve
prob_true, prob_pred = calibration_curve(y_test, y_prob, n_bins=10)

# 3. Visualize
disp = CalibrationDisplay(prob_true, prob_pred, y_prob)
disp.plot()
plt.title("Calibration Curve")
plt.show()

If your curve bows below the diagonal, your model is "overconfident"—the predicted probabilities are higher than the actual frequency. If it bows above, the model is "underconfident."

Adjusting Model Thresholds

You shouldn't always use 0.5 as your decision threshold. If your model is well-calibrated, you can pick a threshold based on the cost of errors.

For instance, if you are predicting fraud, missing a fraudulent transaction might be expensive. You might choose a lower threshold (e.g., 0.3) to flag more potential fraud, accepting more false positives to ensure you catch more actual fraud. Because the model is calibrated, you know that a 0.3 probability actually corresponds to a 30% risk, allowing you to make a mathematically sound trade-off.

Hands-on Exercise

  1. Take the classifier you built in Benchmarking Algorithms: Choosing the Right Model for Your Project.
  2. Run the code above to generate a calibration curve for your current best model.
  3. Identify: Is your model overconfident or underconfident?
  4. If the model is poorly calibrated, try using sklearn.calibration.CalibratedClassifierCV to wrap your model. This uses techniques like Isotonic Regression or Platt Scaling to "fix" the probabilities.

Common Pitfalls

  • Ignoring the Base Rate: If your dataset is highly imbalanced, a model might predict very low probabilities for everything. Your Brier score might look "good" because the model is always predicting near 0, but it’s actually useless. Always compare your Brier score against a "dummy" model that predicts the average frequency of the positive class.
  • Over-calibration: Sometimes, applying calibration techniques on a small test set can lead to overfitting the calibration itself. Always ensure you are calibrating on a hold-out set that was not used for training.
  • Confusing Thresholds with Calibration: Adjusting the threshold changes your sensitivity (recall) and specificity, but it does not fix the underlying calibration. If the model is poorly calibrated, changing the threshold is just putting a bandage on a broken compass.

Recap

Calibration is about the integrity of your probability estimates. By using the Brier score for a quantitative metric and calibration curves for visual debugging, you ensure that your model’s output can be trusted for real-world decision-making. When your model is calibrated, you can move beyond simple binary predictions and start optimizing for the actual costs and benefits of your business logic.

Up next: We will explore how to use more sophisticated methods to find the optimal settings for your models with Advanced Hyperparameter Search.

Similar Posts