Workspace/Coding labs
Loading progress

Measure probability quality and calibration bins

Intermediate50 min

Implement calibration_report(probabilities, labels, bins=5). Require equal nonempty lengths, finite numeric probabilities in [0,1] excluding booleans, integer labels 0 or 1 excluding booleans, and a positive integer bin count. Use equal-width bins [i/bins,(i+1)/bins), with probability 1 in the last bin. Return brier, ece, and bin_stats containing index, count, mean_probability, event_rate for nonempty bins only. ECE is the sample-weighted absolute difference between mean event probability and observed event rate.

Your task

  1. Complete the starter function using the contract above.
  2. Use the examples and visible tests to check normal inputs, boundaries, and rejected inputs.
  3. Run tests to record your result, then compare with the explained reference solution.

Examples

EXAMPLE 1

Inputprobabilities=[0,1], labels=[0,1], bins=2

Outputbrier=0.0, ece=0.0, two nonempty bins

Exact endpoints are valid and 1 belongs in the last bin.
EXAMPLE 2

Inputprobabilities=[0.8,0.8,0.8], labels=[1,1,0], bins=5

Outputbrier=0.24, ece about 0.133333

All rows share one probability bin.
solution.pyPython 3.12