Thresholds, costs and lift
A probability isn't a decision until you set a threshold. Derive it from costs, compare the rules the app offers, and use lift when you have a budget.
What runs in CodeSplash
- ROC curve and AUC (DeLong) Verified in Python and R
You will be able to
- Derive a cost-based threshold from the costs of each error
- Compare the F1, Youden and least-cost rules
- Read a gain curve to plan for a limited budget
Read first
0.5 is a default, not a decision
ReadThe model estimates a probability. You choose what to do at each value. A threshold of 0.5 is right only if a false positive and a false negative cost the same and the probabilities are calibrated.
Act when the expected cost of acting is less than that of not acting. Acting on a row with probability costs . Not acting costs . So act when
If a miss costs 9 times a false alarm: . Rows with only a 10% chance are already worth acting on.
Where would you set it?
PredictPredict first
A false alarm costs 95. With calibrated probabilities, above which probability should you investigate?
Make your guess between 0 and 1 threshold, then reveal.
Reveal
. A threshold that looks absurdly low is exactly right when misses are this costly. Test it on the Evaluation tab with Cost of a false positive at 5 and Cost of a false negative at 95.
Rules the app offers
ReadThe Decision thresholds table compares them on the run's out-of-fold or test predictions:
| Rule | Picks the threshold that… |
|---|---|
| Default | Is 0.5 |
| Best F1 | Maximizes F1 |
| Best Youden J | Maximizes recall + specificity − 1 |
| Least cost | Minimizes your cost per row |
| Your threshold | You typed |
On glucose, Youden's J picks 128. Predicting a manual gearbox from car weight, with misses twice as costly, the minimum-cost rule picks 3.17 for a sensitivity of 0.923.
See the sweep
ExploreEach line is a metric as the threshold moves. The marker is the chosen threshold. Find where precision and recall cross, and notice that it isn't the same place as the peak of F1.
View data
| probability threshold | value | metric |
|---|---|---|
| 0.0200 | 0.2541 | precision |
| 0.0200 | 1 | recall |
| 0.0200 | 0.4052 | F1 |
| 0.0200 | 0.0084 | specificity |
| 0.0400 | 0.2617 | precision |
| 0.0400 | 1 | recall |
| 0.0400 | 0.4148 | F1 |
| 0.0400 | 0.0468 | specificity |
| 0.0600 | 0.2715 | precision |
| 0.0600 | 1 | recall |
| 0.0600 | 0.4271 | F1 |
| 0.0600 | 0.0936 | specificity |
| 0.0800 | 0.2857 | precision |
| 0.0800 | 1 | recall |
| 0.0800 | 0.4444 | F1 |
| 0.0800 | 0.1555 | specificity |
| 0.1000 | 0.3033 | precision |
| 0.1000 | 1 | recall |
| 0.1000 | 0.4654 | F1 |
| 0.1000 | 0.2241 | specificity |
| 0.1200 | 0.3211 | precision |
| 0.1200 | 1 | recall |
| 0.1200 | 0.4862 | F1 |
| 0.1200 | 0.2860 | specificity |
| 0.1400 | 0.3418 | precision |
| 0.1400 | 1 | recall |
| 0.1400 | 0.5095 | F1 |
| 0.1400 | 0.3495 | specificity |
| 0.1600 | 0.3620 | precision |
| 0.1600 | 1 | recall |
| 0.1600 | 0.5316 | F1 |
| 0.1600 | 0.4047 | specificity |
| 0.1800 | 0.3804 | precision |
| 0.1800 | 1 | recall |
| 0.1800 | 0.5512 | F1 |
| 0.1800 | 0.4498 | specificity |
| 0.2000 | 0.4040 | precision |
| 0.2000 | 1 | recall |
| 0.2000 | 0.5755 | F1 |
| 0.2000 | 0.5017 | specificity |
| 0.2200 | 0.4208 | precision |
| 0.2200 | 1 | recall |
| 0.2200 | 0.5924 | F1 |
| 0.2200 | 0.5351 | specificity |
| 0.2400 | 0.4469 | precision |
| 0.2400 | 1 | recall |
| 0.2400 | 0.6177 | F1 |
| 0.2400 | 0.5819 | specificity |
| 0.2600 | 0.4797 | precision |
| 0.2600 | 0.9950 | recall |
| 0.2600 | 0.6473 | F1 |
| 0.2600 | 0.6355 | specificity |
| 0.2800 | 0.4988 | precision |
| 0.2800 | 0.9901 | recall |
| 0.2800 | 0.6633 | F1 |
| 0.2800 | 0.6639 | specificity |
| 0.3000 | 0.5279 | precision |
| 0.3000 | 0.9851 | recall |
| 0.3000 | 0.6874 | F1 |
| 0.3000 | 0.7023 | specificity |
First 60 of 196 rows.
Note that a threshold chosen on these rows is optimistic for those same rows. The app says so in a note below the table. Confirm it on new data.
A budget instead of a cost
ReadOften you can't price errors, but you can only act on so many. Gain shows the share of all positives caught by acting on the top of scores. Lift divides that by .
View data
| share of customers contacted | share of churners reached | model |
|---|---|---|
| 0 | 0 | Gradient boosting |
| 0.0500 | 0.2254 | Gradient boosting |
| 0.1000 | 0.3731 | Gradient boosting |
| 0.1500 | 0.5104 | Gradient boosting |
| 0.2000 | 0.6218 | Gradient boosting |
| 0.2500 | 0.6891 | Gradient boosting |
| 0.3000 | 0.7617 | Gradient boosting |
| 0.3500 | 0.8083 | Gradient boosting |
| 0.4000 | 0.8575 | Gradient boosting |
| 0.4500 | 0.8938 | Gradient boosting |
| 0.5000 | 0.9145 | Gradient boosting |
| 0.5500 | 0.9482 | Gradient boosting |
| 0.6000 | 0.9637 | Gradient boosting |
| 0.6500 | 0.9767 | Gradient boosting |
| 0.7000 | 0.9819 | Gradient boosting |
| 0.7500 | 0.9948 | Gradient boosting |
| 0.8000 | 0.9974 | Gradient boosting |
| 0.8500 | 0.9974 | Gradient boosting |
| 0.9000 | 1 | Gradient boosting |
| 0.9500 | 1 | Gradient boosting |
| 1 | 1 | Gradient boosting |
| 0 | 0 | Random forest |
| 0.0500 | 0.1687 | Random forest |
| 0.1000 | 0.3105 | Random forest |
| 0.1500 | 0.4230 | Random forest |
| 0.2000 | 0.5281 | Random forest |
| 0.2500 | 0.6112 | Random forest |
| 0.3000 | 0.6748 | Random forest |
| 0.3500 | 0.7139 | Random forest |
| 0.4000 | 0.7702 | Random forest |
| 0.4500 | 0.7995 | Random forest |
| 0.5000 | 0.8386 | Random forest |
| 0.5500 | 0.8729 | Random forest |
| 0.6000 | 0.9022 | Random forest |
| 0.6500 | 0.9340 | Random forest |
| 0.7000 | 0.9462 | Random forest |
| 0.7500 | 0.9731 | Random forest |
| 0.8000 | 0.9829 | Random forest |
| 0.8500 | 0.9927 | Random forest |
| 0.9000 | 0.9976 | Random forest |
| 0.9500 | 1 | Random forest |
| 1 | 1 | Random forest |
If the top 20% holds 60% of positives, lift is 3: you find cases three times as fast as at random. So a marketing team with a budget for 20% of customers reads this chart, not a threshold table.
Check your understanding
CheckpointCheck yourself
A false positive costs 1, and a false negative costs 4. What is the cost-based threshold?
Show the answer
Answer: 0.2
.
Check yourself
The top 10% of scores contains 25% of all positives. What is the lift at 10%?
Show the answer
Answer: 2.5
. A random 10% would hold 10% of the positives.
Check yourself
The cost formula assumes something about the model's probabilities. What?
Show the answer
Answer: a
If not, the formula's threshold lands in the wrong place. Either recalibrate, or choose by the Least cost rule directly on held-out rows.
Check yourself
When is Youden's J a poor choice?
Show the answer
Answer: a
Youden's J weighs the two kinds of error evenly, whatever they cost in practice.
Choose it
In the appOpen a binary run, such as the walkthrough's model of {{target}}, and go to Evaluation. Type a Threshold, or set Cost of a false positive and Cost of a false negative and read the Decision thresholds table. A confusion matrix, a threshold sweep and a lift chart are all computed from the run's predictions.
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold, cross_val_predict
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
df = pd.read_csv("pima.csv")
X, y = df.drop(columns="diabetic"), df["diabetic"].to_numpy()
cv = StratifiedKFold(5, shuffle=True, random_state=1)
p = cross_val_predict(make_pipeline(StandardScaler(), LogisticRegression()), X, y, cv=cv, method="predict_proba")[:, 1]
c_fp, c_fn = 1, 4
grid = np.linspace(0.01, 0.99, 99)
cost = [(c_fp * ((p >= t) & ~y).sum() + c_fn * ((p < t) & y).sum()) / len(y) for t in grid]
print("formula", c_fp / (c_fp + c_fn), "best on data", round(grid[int(np.argmin(cost))], 2), "cost", round(min(cost), 3))df <- rbind(MASS::Pima.tr, MASS::Pima.te)
y <- df$type == "Yes"
set.seed(1)
fold <- sample(rep(1:5, length.out = nrow(df)))
p <- numeric(nrow(df))
for (k in 1:5) p[fold == k] <- predict(glm(type ~ ., binomial, df[fold != k, ]), df[fold == k, ], type = "response")
c_fp <- 1; c_fn <- 4
grid <- seq(0.01, 0.99, by = 0.01)
cost <- sapply(grid, function(t) (c_fp * sum(p >= t & !y) + c_fn * sum(p < t & y)) / length(y))
c(formula = c_fp / (c_fp + c_fn), best_on_data = grid[which.min(cost)], cost = round(min(cost), 3))Recap
Recap- With calibrated probabilities, the threshold follows from the costs: .
- F1 and Youden assume particular costs. State your own if you can.
- With a limited budget, read gain and lift. Confirm any chosen threshold on new rows.
Concepts in this lesson