#86Brier Score + Reliability DecompositionMedium

Brier Score + Reliability Decomposition

Background

The Brier score is the mean squared error of probabilistic predictions: 1n(piyi)2\frac{1}{n}\sum (p_i - y_i)^2. Decompose into reliability (calibration error) and resolution (sharpness) per bin to reason about which kind of miscalibration dominates.

Problem statement

Implement brier_score(probs, labels, n_bins=10) returning (brier, reliability, resolution).

Brier  =  1n(piyi)2,Rel  =  1nmnm(pˉmyˉm)2,Res  =  1nmnm(yˉmyˉ)2\text{Brier} \;=\; \frac{1}{n}\sum (p_i - y_i)^2,\quad \text{Rel} \;=\; \frac{1}{n}\sum_m n_m\,(\bar{p}_m - \bar{y}_m)^2,\quad \text{Res} \;=\; \frac{1}{n}\sum_m n_m\,(\bar{y}_m - \bar{y})^2

Input

  • probs - array-like of floats in [0,1][0, 1], length nn.
  • labels - array-like of {0,1}\{0, 1\}, length nn.
  • n_bins - bins for the decomposition (default 1010).

Output

  • (brier, reliability, resolution) - tuple of plain Python floats.

Examples

Input: probs=[0.9, 0.1, 0.9, 0.1], labels=[1, 0, 1, 0], n_bins=2
Output: brier=0.01, reliability~0.01, resolution=0.25

Constraints

  • Equal-width bins on [0,1][0, 1] with clamp at the last bin.
  • Empty bins contribute 0 to both reliability and resolution.

Notes

  • Identity. Brier = Rel - Res + Unc (uncertainty = yˉ(1yˉ)\bar{y}(1-\bar{y})). Rel = 0 means perfectly calibrated; Res large means the bins differentiate outcomes well.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Two-bin decomposition, hand-checkable (example)
  • Full decomposition on two predictions (reference)
  • Full decomposition across five bins (sample)