Bird
Raised Fist0

Calibration - Why Accuracy Isn't Enough for Probability Outputs

THEORYadvancedInterview freq: high
Choose your preparation mode6 modes available
🐛
The Misleading Confidence
Issue noticed after a new feature was added last week.

A fraud detection model for a financial service, processing 500K transactions daily.

Symptom
Model accuracy is stable at 0.95, but recall for fraudulent transactions is 0.
📡
Signals
metric
Recall for fraudulent transactions
Recall dropped to 0 after new feature deployment
metric
Overall model accuracy
Accuracy remains stable at 0.95
distribution
Probability distribution of predicted fraud scores
Predicted probabilities are skewed towards 0.9 and above
feature_importance
Importance of the new feature
New feature 'transaction_time' importance is 0.25, highest among all features
log
Model prediction logs
All fraud predictions have a probability > 0.9
🔬
Hypotheses
The model is overconfident in its predictions due to poor calibration.
Probability distribution of predicted fraud scoresModel prediction logs
Verify: Check the calibration curve to see if predicted probabilities match actual outcomes.
from sklearn.calibration import calibration_curve
import matplotlib.pyplot as plt

prob_true, prob_pred = calibration_curve(y_true, y_prob, n_bins=10)
plt.plot(prob_pred, prob_true, marker='o')
plt.plot([0, 1], [0, 1], linestyle='--')
plt.show()
The new feature 'transaction_time' is causing the model to misclassify due to its high importance.
Feature importance of 'transaction_time'
Verify: Analyze the impact of 'transaction_time' by removing it and observing changes in recall.
X_train_reduced = X_train.drop(columns=['transaction_time'])
model.fit(X_train_reduced, y_train)
y_pred_reduced = model.predict(X_test.drop(columns=['transaction_time']))
recall_reduced = recall_score(y_test, y_pred_reduced)
print(recall_reduced)
🎯
Root Cause
The model's probability outputs are poorly calibrated, leading to overconfidence in predictions.

The addition of a new feature skewed the model's probability distribution, causing miscalibration.

🔧
Fix Strategy
ImmediateRollback to the previous model version without the new feature.
Short-termApply a calibration technique like Platt scaling or isotonic regression to adjust probability outputs.
Long-termIncorporate a calibration step in the model training pipeline to ensure probabilities are well-calibrated.
🐍
Python Fix
Python
from sklearn.calibration import CalibratedClassifierCV

calibrated_model = CalibratedClassifierCV(base_estimator=model, method='isotonic', cv='prefit')
calibrated_model.fit(X_val, y_val)
y_calibrated = calibrated_model.predict_proba(X_test)[:, 1]
✗
Common Wrong Diagnoses
▼ Show
The model is overfitting to the training data.
Why candidates think this: High accuracy but poor recall suggests overfitting.
Why wrong: Overfitting would typically affect both accuracy and recall, not just recall.
The new feature is irrelevant and causing noise.
Why candidates think this: High feature importance suggests it might be misleading.
Why wrong: Removing the feature doesn't improve recall, indicating the issue is with calibration.
📊
Monitoring to Add
  • Regularly monitor calibration curves for new model versions.
  • Track recall for minority classes separately in production.
  • Set alerts for significant changes in feature importance.
📋
Process Change

Implement a mandatory calibration check for all models before deployment.

🏗️
Architectural Change

Design the system to include a calibration layer that adjusts probabilities before making final predictions.

💡
Key Insight
Calibration ensures that predicted probabilities reflect true likelihoods, crucial for decision-making in critical applications.

Practice

(1/5)
1. In the context of Random Forests, what is the primary purpose of using bagging?
easy
A. To increase the model's interpretability
B. To enhance the model's feature selection
C. To improve the model's computational efficiency
D. To reduce the model's variance

Solution

  1. Step 1: Understand bagging's role

    Bagging, or Bootstrap Aggregating, is used to reduce variance by averaging predictions from multiple models trained on different subsets of the data.
  2. Step 2: Eliminate wrong options

    To increase the model's interpretability is incorrect because bagging does not inherently increase interpretability. To enhance the model's feature selection is wrong as feature selection is not the primary goal of bagging. To improve the model's computational efficiency is incorrect because bagging typically increases computational cost due to multiple models.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Bagging reduces variance by averaging. [OK]
Hint: Bagging reduces variance by averaging. [OK]
Common Mistakes:
2.
import numpy as np

y_true = np.array([3, -0.5, 2, 7])
y_pred = np.array([2.5, 0.0, 2, 8])

mae = np.mean(np.abs(y_true - y_pred))
print(mae)
What is the output of this code snippet?
easy
A. 0.625
B. 0.5
C. 1.0
D. 1.25

Solution

  1. Step 1: Calculate the absolute errors.

    Errors are [0.5, 0.5, 0, 1].
  2. Step 2: Compute the mean of absolute errors.

    Mean is (0.5 + 0.5 + 0 + 1) / 4 = 0.625.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Sum errors, divide by count. [OK]
Hint: Sum absolute errors, divide by count. [OK]
Common Mistakes:
3. In the context of high-dimensional data, which of the following is a primary reason why the curse of dimensionality affects nearest neighbor algorithms?
easy
A. Distances between points become less meaningful as dimensions increase.
B. High-dimensional data increases the risk of overfitting.
C. High-dimensional data requires more computational resources.
D. High-dimensional data leads to more complex decision boundaries.

Solution

  1. Step 1: Understand the curse of dimensionality.

    In high dimensions, the distance between points becomes less informative.
  2. Step 2: Eliminate wrong options.

    High-dimensional data increases the risk of overfitting. is about overfitting, not distance. High-dimensional data requires more computational resources. is about computation, not distance. High-dimensional data leads to more complex decision boundaries. is about decision boundaries, not distance.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    High dimensions dilute distance meaning. [OK]
Hint: High dimensions dilute distance meaning. [OK]
Common Mistakes:
4. For a regression task with a large number of outliers, which metric would be more appropriate to use?
medium
A. Mean Squared Error (MSE)
B. Huber Loss
C. Mean Absolute Percentage Error (MAPE)
D. Root Mean Squared Error (RMSE)

Solution

  1. Step 1: Consider the impact of outliers on metrics.

    Huber Loss is less sensitive to outliers than MSE and RMSE.
  2. Step 2: Eliminate wrong options.

    MSE and RMSE amplify outliers due to squaring. MAPE is not suitable for zero or near-zero values.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Huber Loss balances sensitivity to outliers. [OK]
Hint: Huber Loss balances sensitivity to outliers. [OK]
Common Mistakes:
5. You are tasked with designing a model for a real-time translation app. Which architecture would you choose and why?
hard
A. Use a hybrid RNN-CNN model to leverage both sequence processing and speed.
B. Use a standard RNN for its simplicity and ease of implementation.
C. Use a CNN-based model for its speed in processing sequences.
D. Use a transformer model for its ability to handle long-range dependencies efficiently.

Solution

  1. Step 1: Consider the requirements of real-time translation.

    Real-time translation requires handling long-range dependencies efficiently.
  2. Step 2: Evaluate each architecture.

    Use a standard RNN for its simplicity and ease of implementation. is incorrect as RNNs are slow for long sequences. Use a CNN-based model for its speed in processing sequences. is incorrect because CNNs struggle with sequence dependencies. Use a hybrid RNN-CNN model to leverage both sequence processing and speed. is incorrect as hybrid models add complexity without solving dependency issues.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Transformers excel in long-range dependencies. [OK]
Hint: Transformers excel in long-range dependencies. [OK]
Common Mistakes:
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard