Build a probability quality report with Brier score, ROC AUC and weighted ECE

Quick Overview

Implement a report that scores predicted probabilities with the Brier score, ROC AUC and a sample-weighted Expected Calibration Error built on scikit-learn's calibration_curve. It tests uniform binning, aligning counts when empty bins are dropped, and the difference between ranking quality and calibration.

Build a probability quality report with Brier score, ROC AUC and weighted ECE

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Write `probability_quality_report(y_true, y_proba, n_bins)` that measures the quality of predicted probabilities. Do not fit any calibrator. Return a dict with exactly four keys: - `'brier'` (float), the value of `brier_score_loss` - `'roc_auc'` (float), the value of `roc_auc_score` - `'ece'` (float), the Expected Calibration Error - `'n_bins_used'` (int) Requirements: - Build the reliability curve with `sklearn.calibration.calibration_curve(y_true, y_proba, n_bins=n_bins, strategy='uniform')`. - `ece` is the sample-weighted sum `sum(w_b * |prob_true_b - prob_pred_b|)`, where `w_b` is bin b's share of the samples. An unweighted mean over bins is wrong. - Count the samples per bin over the same uniform edges, `np.linspace(0, 1, n_bins + 1)`, and weight only the non-empty bins, because `calibration_curve` drops empty bins from its output. - `'n_bins_used'` is the length of the returned `prob_true`. ```hint Line up the arrays The arrays from calibration_curve can be shorter than n_bins. Your counts have to describe exactly the bins those arrays describe, in the same order. ``` ```hint Values on an edge Check how each binning function assigns a probability that sits exactly on an interior edge, such as 0.5 with four bins. ``` ### Constraints & Assumptions - `y_true` is binary (0/1), and `y_proba` holds probabilities in [0, 1]. - `sklearn` and `numpy` are available. No model training is involved. - The floats should match a reference implementation to a relative tolerance of `1e-9`, and `n_bins_used` should match exactly. ### What a Strong Answer Covers - Computing the Brier score and ROC AUC with the sklearn functions and returning plain Python numbers under the four exact keys - Weighting each bin's calibration gap by its share of the samples, and why the unweighted mean over bins misleads - Counts that line up with the non-empty bins `calibration_curve` returns, including probabilities that fall exactly on a bin edge - The difference between ranking quality (AUC) and probability quality (Brier, ECE) - Edge cases such as a single class in `y_true` or every prediction landing in one bin ### Follow-up Questions - How does `strategy='quantile'` change the ECE? - Why can `roc_auc` look great while `brier` and `ece` are poor? - How would you turn ECE into a calibration fix?

Overview: Implement a report that scores predicted probabilities with the Brier score, ROC AUC and a sample-weighted Expected Calibration Error built on scikit-learn's calibration_curve. It tests uniform binning, aligning counts when empty bins are dropped, and the difference between ranking quality and calibration.

Nov 25, 2026
mediumData ScientistOtherMachine Learning
0
0

Write probability_quality_report(y_true, y_proba, n_bins) that measures the quality of predicted probabilities. Do not fit any calibrator. Return a dict with exactly four keys:

  • 'brier' (float), the value of brier_score_loss
  • 'roc_auc' (float), the value of roc_auc_score
  • 'ece' (float), the Expected Calibration Error
  • 'n_bins_used' (int)

Requirements:

  • Build the reliability curve with sklearn.calibration.calibration_curve(y_true, y_proba, n_bins=n_bins, strategy='uniform') .
  • ece is the sample-weighted sum sum(w_b * |prob_true_b - prob_pred_b|) , where w_b is bin b's share of the samples. An unweighted mean over bins is wrong.
  • Count the samples per bin over the same uniform edges, np.linspace(0, 1, n_bins + 1) , and weight only the non-empty bins, because calibration_curve drops empty bins from its output.
  • 'n_bins_used' is the length of the returned prob_true .

Constraints & Assumptions

  • y_true is binary (0/1), and y_proba holds probabilities in [0, 1].
  • sklearn and numpy are available. No model training is involved.
  • The floats should match a reference implementation to a relative tolerance of 1e-9 , and n_bins_used should match exactly.

What a Strong Answer Covers Guidance

  • Computing the Brier score and ROC AUC with the sklearn functions and returning plain Python numbers under the four exact keys
  • Weighting each bin's calibration gap by its share of the samples, and why the unweighted mean over bins misleads
  • Counts that line up with the non-empty bins calibration_curve returns, including probabilities that fall exactly on a bin edge
  • The difference between ranking quality (AUC) and probability quality (Brier, ECE)
  • Edge cases such as a single class in y_true or every prediction landing in one bin

Follow-up Questions Guidance

  • How does strategy='quantile' change the ECE?
  • Why can roc_auc look great while brier and ece are poor?
  • How would you turn ECE into a calibration fix?