822. Score probability forecasts with the Brier score
Why it matters Forecast and LLM-judge calibration reports use the Brier score next to accuracy.
Accuracy only checks the top class. The Brier score checks the whole probability vector: it is the squared distance between the predicted probabilities and the one-hot truth, so confident wrong answers cost the most.
Write brier(probs, labels) for probs of shape (n, C) (rows sum to 1) and a length-n list of class ids. Return the mean over rows of sum over classes (probs[i, c] - onehot[i, c]) ** 2 as a float.
def brier(probs, labels)Example 1
Input: brier([[0.8, 0.2], [0.4, 0.6]], [0, 0])
Output: 0.4
Example 2
Input: probs = [[1.0, 0.0, 0.0]], labels = [0]
Output: 0.0
Example 3
Input: probs = [[0.0, 1.0]], labels = [0]
Output: 2.0
Constraints
probshas shape(n, C)- Python 3 with NumPy. Code runs in your browser.
- Your code must pass all 5 tests to be accepted.
Hint 1
Build the one-hot matrix with fancy indexing.
Hint 2
Sum squared differences across classes, then average across rows.
Common mistakes
- Only scoring the true class probability, which ignores mass put on wrong classes.
- Averaging over classes too, which divides the multi-class score by C.
Understand the idea
For each row, compare the probability vector with the one-hot truth and add the squared gaps. A perfect confident answer scores 0; a confident wrong answer in two classes scores 2.
[0.8, 0.2] with truth class 0 gives 0.04 + 0.04 = 0.08; [0.4, 0.6] with truth 0 gives 0.36 + 0.36 = 0.72. Mean 0.4.
brier = mean_i sum_c (p_ic - y_ic)^2