Compute the Brier score of logistic regression predicted probabilities
Quick Overview
Write a function that computes the Brier score of a logistic model's positive-class probabilities against 0/1 labels. Tests probability scoring, floating-point return types, and how calibration-sensitive metrics differ from ranking metrics such as AUC.
Compute the Brier score of logistic regression predicted probabilities
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Write `brier(y_true, y_proba)` that returns the Brier score of a logistic model's positive-class probabilities:
$$BS = \frac{1}{n} \sum_{i=1}^{n} (p_i - y_i)^2$$
Using `sklearn.metrics.brier_score_loss` or NumPy is fine; only the returned number is graded.
- `y_true` is a 1-D int ndarray of 0/1 labels, shape `(n,)`.
- `y_proba` is a 1-D float ndarray of values in [0, 1], shape `(n,)`.
- Return a Python `float`. Lower is better: predicting every label correctly with probability 1 scores `0.0`.
For example:
```python
y_true = np.array([1, 0, 1, 1, 0])
y_proba = np.array([0.9, 0.2, 0.6, 0.8, 0.3])
brier(y_true, y_proba) # -> 0.068 (up to floating-point rounding)
```
```hint Watch the types
The labels arrive as integers and the grader wants a plain Python float back. Make sure the arithmetic happens in floating point and check what type your final expression returns.
```
### Constraints & Assumptions
- `y_true` and `y_proba` have the same length `n`, with `n` at least 1.
- `y_proba` holds the probability of class 1 for each sample.
- The result is checked with `assert_allclose(rtol=1e-9)`.
### What a Strong Answer Covers
- Computes the mean of the squared gaps between positive-class probabilities and 0/1 labels and returns a Python float.
- Keeps the computation in floating point regardless of the input dtypes.
- Interprets the value: the best and worst possible scores, and the score of a model that always predicts the base rate.
- Explains why the Brier score reflects calibration as well as ranking, unlike AUC.
### Follow-up Questions
- Why can a model have great AUC but a poor Brier score?
- How would you decompose the Brier score into calibration and refinement?
- When would you report log loss instead of the Brier score, or the other way around?
- How would you compare Brier scores across two datasets with very different positive rates?
Overview: Write a function that computes the Brier score of a logistic model's positive-class probabilities against 0/1 labels. Tests probability scoring, floating-point return types, and how calibration-sensitive metrics differ from ranking metrics such as AUC.