Week 03 · Logistic regression and decision thresholds

Convert a probability into an action using the costs of mistakes.

← Week 2 · Subject contents · Week 4 →

Convert a probability into an action using the costs of mistakes.

Work through the problem

For a calibrated score p, predicting positive costs C_FP(1−p) in expectation and predicting negative costs C_FN p. Act when p ≥ C_FP/(C_FP+C_FN). With false alert cost 1 and missed event cost 4, the cutoff is 0.20. At p = 0.30 the two expected costs are 0.70 and 1.20.

Logistic regression models log odds as w·x+b, so a linear score becomes a probability through the sigmoid. The fit estimates scores from training data. Apply a cutoff justified by validation or a known cost model, then assess it on separate data.

Run the Python lab

import numpy as np
from sklearn.linear_model import LogisticRegression
x = np.arange(-4,5).reshape(-1,1)
y = [0,0,0,1,0,1,1,1,1]
model = LogisticRegression(solver="liblinear").fit(x,y)
prob = model.predict_proba([[-1],[0],[1]])[:,1]
for missed in [1,4]:
    cutoff = 1/(1+missed)
    print(cutoff, prob.round(3), (prob>=cutoff).astype(int))
assert prob[0] < prob[1] < prob[2]

Output from the lab

0.5 [0.347 0.559 0.752] [0 1 1]
0.2 [0.347 0.559 0.752] [1 1 1]

Practice

  1. What cutoff follows if both mistakes cost 2?

Check your reasoning

  1. 0.50. Scaling both costs equally does not change the decision.

← Week 2 · Subject contents · Week 4 →

Further reading · An Introduction to Statistical Learning