Adaptive Calibration Error
Problem statement
A classifier gives each of n examples a probability for each of K classes. probabilities[i][c] is the predicted probability that example i is class c, and labels[i] is the example's true class.
Return the model's Adaptive Calibration Error (ACE) with numBins bins, computed like this:
- For each class
c, sort the examples byprobabilities[i][c]from low to high. Examples with equal probability keep index order, smaller index first. - Cut this sorted list into
numBinsconsecutive bins ofn / numBinsexamples each. - In each bin, the accuracy is the fraction of its examples whose label is
c, and the confidence is the average of theirprobabilities[i][c]. The bin's gap is|accuracy - confidence|. - ACE is the average gap over all
K * numBinsclass and bin pairs.
A well-calibrated model has an ACE near 0. Because every bin holds the same number of examples, each gap is estimated from the same amount of data.
Examples
Example 1
probabilities = [[0.9,0.1],[0.6,0.4],[0.3,0.7],[0.2,0.8]]labels = [0,0,1,0]numBins = 2return = 0.25Class 0 sorted by probability is 0.2, 0.3 | 0.6, 0.9. The low bin has confidence 0.25 and accuracy 0.5; the high bin has confidence 0.75 and accuracy 1. Both gaps are 0.25. Class 1 sorted is 0.1, 0.4 | 0.7, 0.8, with gaps |0 - 0.25| = 0.25 and |0.5 - 0.75| = 0.25. The answer is (4 * 0.25) / (2 * 2) = 0.25.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 2 more worked examples, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.