Sub topic 6
Log loss and Brier score
This chapter assumes you know binary classification, the sigmoid function, and basic scikit-learn. You should also have read the chapter on reading a reliability diagram. A reliability diagram shows calibration, but it gives no single number you can compare across models. Proper scoring rules give you that number.
What a proper scoring rule is
A scoring rule assigns a penalty to a predicted probability once the true label is known. A rule is proper if the expected penalty is lowest when the model reports its true belief about the outcome. Reporting a probability that is too confident or too timid cannot lower the expected penalty. Log loss and the Brier score are both proper.
Accuracy is not proper. It only checks which side of 0.5 a prediction falls on, so it ignores how confident the model was.
Log loss
For a binary label y (0 or 1) and a predicted probability p of the positive class, log loss is:
-(y · log p + (1 - y) · log(1 - p))
Averaged over all samples, this is the negative log-likelihood of the labels. It is also the loss that logistic regression minimizes.
Log loss punishes confident mistakes very hard. A prediction of 0.99 for a sample that turns out negative costs about 4.6. A prediction of 0.6 for the same sample costs about 0.9. A prediction of exactly 1.0 for a wrong label costs infinity, which is why scikit-learn clips probabilities by a tiny epsilon.
Brier score
The Brier score is the mean squared error between the probability and the 0/1 label:
(p - y)²
Its worst possible penalty for one sample is 1, so it is bounded and less dominated by a few extreme errors. A model that always predicts the base rate gives a useful baseline. Lower is better for both metrics.
Comparing the two
The upper line is log loss and the lower line is the Brier score. They agree on direction but differ in how they treat extreme errors.
| Property | Log loss | Brier score |
|---|---|---|
| Formula | Negative log-likelihood | Squared error |
| Range | 0 to infinity | 0 to 1 |
| Confident mistakes | Penalized very heavily | Penalized at most 1 |
| Sensitivity to rare events | High | Lower |
| Best when | You care about tail probabilities | You want a stable, readable number |
Computing both
The code below trains a naive Bayes model, which is often overconfident. It compares log loss and Brier score against a baseline that predicts the training base rate.
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import brier_score_loss, log_loss
X, y = make_classification(
n_samples=20000, n_features=20, n_informative=5, n_redundant=10, random_state=0
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.5, random_state=0
)
p = GaussianNB().fit(X_train, y_train).predict_proba(X_test)[:, 1]
base = np.full(len(y_test), y_train.mean())
print("model Brier:", brier_score_loss(y_test, p), "log loss:", log_loss(y_test, p))
print("baseline Brier:", brier_score_loss(y_test, base), "log loss:", log_loss(y_test, base))Naive Bayes multiplies feature likelihoods as if they were independent. Here ten features are redundant, so the model counts the same evidence repeatedly and pushes probabilities toward 0 and 1. Its log loss suffers more than its Brier score because of the confident errors.
What these scores do and do not tell you
Both scores mix calibration with discrimination. A low score can come from good ranking, good calibration, or both. A model with a poor reliability curve can still beat a better-calibrated model if it separates the classes much better. Use the scores to compare models and calibrators on the same held-out data. Use a reliability diagram to see why a score is high.
Exercise
A model predicts 0.95 for a sample whose true label is 0. Compute the Brier penalty and say why log loss would be much larger.
Show answer
The Brier penalty is (0.95 - 0)² = 0.9025. Log loss is -log(1 - 0.95) = -log(0.05), about 3.0. The logarithm grows without bound as the probability assigned to the true class approaches 0, while the squared error can never exceed 1.