Quant interview toolkit · case-control sampling

Why a 90% alert can mean an 8.3% chance of an incident

An incident classifier trained on a deliberately balanced archive assigns a new message a 90% incident probability. In production, only one message in a hundred concerns an incident. An engineer proposes multiplying every score by 0.01/0.50. That would turn 90% into 1.8%. You can test this adjustment with a pencil and two piles of messages.

An alert’s precision depends on prevalence

Incident chance after alert (%)

Illustrative sensitivity 90% and false-positive rate 10%; Bayes’ rule converts alert likelihoods to posterior probability.

Rebuild the balanced archive

Imagine a binary message feature called a flag. It appears on 90% of incidents and 10% of ordinary messages. In a balanced archive of 100 incidents and 100 ordinary messages, the flagged pile contains 90 incidents and 10 ordinary messages. A model trained to report the incident frequency for flagged messages should say 90%.

In a hypothetical live batch with 100 incidents and 9,900 ordinary messages, keep the same flag frequencies within each class. The flagged pile now contains 90 incidents and 990 ordinary messages. Its incident frequency is 90/1080=1/1290/1080=1/12, about 8.33%. The numerator stayed at 90. The denominator grew in a way that depended on how often ordinary messages carried the flag.

Transfer the likelihood ratio

Let πs\pi_s be the sampled incident fraction and πt\pi_t the target fraction. For features xx, let psp_s be the sampled posterior probability. Under class-dependent sampling that leaves the feature distribution within each class intact, Bayes’ rule gives

ps1−ps=f(x∣Y=1)f(x∣Y=0)πs1−πs.\frac{p_s}{1-p_s} =\frac{f(x\mid Y=1)}{f(x\mid Y=0)} \frac{\pi_s}{1-\pi_s}.

The first factor transfers to the target population. Divide out the old prior odds and multiply by the new ones. With a=[πt/(1−πt)]/[πs/(1−πs)]a=[\pi_t/(1-\pi_t)]/[\pi_s/(1-\pi_s)],

pt=aps1−ps+aps.p_t=\frac{a p_s}{1-p_s+a p_s}.

The sample prior odds are one, so a 90% sampled probability supplies likelihood ratio 9. Target prior odds are 1/991/99. Posterior odds become 9/99=1/119/99=1/11, yielding probability 1/121/12. On a log-odds scale the repair is a constant additive offset. The nonlinear conversion back to probability explains why multiplying the probabilities by one constant fails.

Which parts of the system change?

Sample probabilityProbability at a 1% live incident rate
50%1.00%
90%8.33%
99%50.00%

A sampled probability of 50% gives no evidence beyond the balanced prior. In production the same evidence leaves you at the live prior. A 99% sample probability supplies likelihood ratio 99, just enough to bring live posterior odds to one. Because the correction is strictly increasing for interior priors, it preserves the review ordering. A dashboard using fixed probability thresholds can nevertheless change dramatically. What AUC measures in a review queue

from fractions import Fraction as F

def transfer(p, sample_rate, live_rate):
    factor = live_rate * (1 - sample_rate) / (sample_rate * (1 - live_rate))
    return factor * p / (1 - p + factor * p)

assert transfer(F(9, 10), F(1, 2), F(1, 100)) == F(1, 12)

Practice

SelectionThe archive kept every incident but chose ordinary messages by searching for suspicious words. Does the same correction apply?
Show answer

Ordinary messages with those words are overrepresented even conditional on being ordinary. The archive’s feature likelihood ratio can differ from the live one. Correcting the prior alone cannot reconstruct the missing distribution. A representative live sample or known feature-dependent sampling probabilities can help identify the necessary correction.

Operational thresholdWith the same priors, what sampled probability corresponds to a live probability of 10%?
Show answer

Live odds 1/91/9 require a likelihood ratio of (1/9)/(1/99)=11(1/9)/(1/99)=11. Balanced-sample odds therefore equal 11 and the sampled probability is 11/1211/12, about 91.67%. This conversion assumes that the input probabilities are calibrated for the sampled population.