BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Evaluation metrics32 of 33 in topic

822. Score probability forecasts with the Brier score

EasyEvaluation metrics

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.

Functiondef 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

  • probs has 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.

Worked example

[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
Topics
Evaluation metrics
Similar questions
  • Compute precision, recall and F1 from predictionsEasy
  • Measure how overconfident a classifier is with expected calibration errorEasy
  • Build a confusion matrixEasy
  • Compute mean reciprocal rankEasy
All Evaluation metrics problems
Report
822 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def brier(probs, labels):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 5 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS