Skip to content

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.

Applied ~12 min8 steps

What runs in CodeSplash

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

01

0.5 is a default, not a decision

Read

The 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 pp costs (1−p) cFP(1-p)\,c_{FP}. Not acting costs p cFNp\,c_{FN}. So act when

p>t∗=cFPcFP+cFNp > t^* = \frac{c_{FP}}{c_{FP} + c_{FN}}

If a miss costs 9 times a false alarm: t∗=1/(1+9)=0.1t^* = 1/(1+9) = 0.1. Rows with only a 10% chance are already worth acting on.

02

Where would you set it?

Predict

Predict first

A false alarm costs 5toinvestigate.Amissedfraudcosts5 to investigate. A missed fraud costs 95. With calibrated probabilities, above which probability should you investigate?

Make your guess between 0 and 1 threshold, then reveal.

Reveal

5/(5+95)=0.055 / (5 + 95) = 0.05. 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.

03

Rules the app offers

Read

The Decision thresholds table compares them on the run's out-of-fold or test predictions:

RulePicks the threshold that…
DefaultIs 0.5
Best F1Maximizes F1
Best Youden JMaximizes recall + specificity − 1
Least costMinimizes your cost per row
Your thresholdYou 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.

04

See the sweep

Explore

Each 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.

Metrics across thresholdsWhere should the decision threshold sit, and what does each choice trade off?
View data
probability thresholdvaluemetric
0.02000.2541precision
0.02001recall
0.02000.4052F1
0.02000.0084specificity
0.04000.2617precision
0.04001recall
0.04000.4148F1
0.04000.0468specificity
0.06000.2715precision
0.06001recall
0.06000.4271F1
0.06000.0936specificity
0.08000.2857precision
0.08001recall
0.08000.4444F1
0.08000.1555specificity
0.10000.3033precision
0.10001recall
0.10000.4654F1
0.10000.2241specificity
0.12000.3211precision
0.12001recall
0.12000.4862F1
0.12000.2860specificity
0.14000.3418precision
0.14001recall
0.14000.5095F1
0.14000.3495specificity
0.16000.3620precision
0.16001recall
0.16000.5316F1
0.16000.4047specificity
0.18000.3804precision
0.18001recall
0.18000.5512F1
0.18000.4498specificity
0.20000.4040precision
0.20001recall
0.20000.5755F1
0.20000.5017specificity
0.22000.4208precision
0.22001recall
0.22000.5924F1
0.22000.5351specificity
0.24000.4469precision
0.24001recall
0.24000.6177F1
0.24000.5819specificity
0.26000.4797precision
0.26000.9950recall
0.26000.6473F1
0.26000.6355specificity
0.28000.4988precision
0.28000.9901recall
0.28000.6633F1
0.28000.6639specificity
0.30000.5279precision
0.30000.9851recall
0.30000.6874F1
0.30000.7023specificity

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.

05

A budget instead of a cost

Read

Often 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 x%x\% of scores. Lift divides that by xx.

Cumulative gain and liftIf we act on the top-scored share of cases, what share of the positives do we reach?
View data
share of customers contactedshare of churners reachedmodel
00Gradient boosting
0.05000.2254Gradient boosting
0.10000.3731Gradient boosting
0.15000.5104Gradient boosting
0.20000.6218Gradient boosting
0.25000.6891Gradient boosting
0.30000.7617Gradient boosting
0.35000.8083Gradient boosting
0.40000.8575Gradient boosting
0.45000.8938Gradient boosting
0.50000.9145Gradient boosting
0.55000.9482Gradient boosting
0.60000.9637Gradient boosting
0.65000.9767Gradient boosting
0.70000.9819Gradient boosting
0.75000.9948Gradient boosting
0.80000.9974Gradient boosting
0.85000.9974Gradient boosting
0.90001Gradient boosting
0.95001Gradient boosting
11Gradient boosting
00Random forest
0.05000.1687Random forest
0.10000.3105Random forest
0.15000.4230Random forest
0.20000.5281Random forest
0.25000.6112Random forest
0.30000.6748Random forest
0.35000.7139Random forest
0.40000.7702Random forest
0.45000.7995Random forest
0.50000.8386Random forest
0.55000.8729Random forest
0.60000.9022Random forest
0.65000.9340Random forest
0.70000.9462Random forest
0.75000.9731Random forest
0.80000.9829Random forest
0.85000.9927Random forest
0.90000.9976Random forest
0.95001Random forest
11Random 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.

06

Check your understanding

Checkpoint

Check yourself

A false positive costs 1, and a false negative costs 4. What is the cost-based threshold?

Show the answer

Answer: 0.2

1/(1+4)=0.21 / (1 + 4) = 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

25/10=2.525 / 10 = 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.

07

Choose it

In the app

Open 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.

Python
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))
R
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))
08

Recap

Recap
  • With calibrated probabilities, the threshold follows from the costs: cFP/(cFP+cFN)c_{FP} / (c_{FP} + c_{FN}).
  • 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