Sam Babichenko

Reading notes / Cost-sensitive classification and calibration

The Foundations of Cost-Sensitive Learning

Elkan, 2001; Saerens et al., 2002

Context. By 2001 cost-sensitive learning had many algorithms but loose foundations: published cost matrices mixed baselines, and rebalancing the training set was a common practice with no formula for how much. Elkan's short paper settles the two-class case. A year later Saerens et al. take up the companion problem: a classifier trained on a sample drawn by class (a case-control study, a deliberately balanced training set) reports posteriors at the training priors, and the deployment priors are usually unknown because the deployment labels are. Costs and base rates enter only through the threshold applied to P(y∣x)P(y\mid x), so the learner should estimate that probability well and the threshold is set at decision time, per example if the costs vary. Rebalancing the training set and moving to a new base rate are the same odds multiplication, which for the learners Elkan analyzes can be done after training, and when the new base rate is unknown it is the fixed point of "average the corrected posteriors" over the unlabeled deployment data.

Setting. C(i,j)=cijC(i,j)=c_{ij} is the cost of predicting class ii when the true class is jj, and the optimal prediction minimizes L(x,i)=∑jP(j∣x) C(i,j)L(x,i)=\sum_j P(j\mid x)\,C(i,j). Under the reasonableness conditions c10>c00c_{10}\gt c_{00} and c01>c11c_{01}\gt c_{11} (a mistake costs more than the correct label), predict class 1 if and only if P(1∣x)≥p∗P(1\mid x)\ge p^*, with p∗=c10−c00c10−c00+c01−c11,p∗=c10c10+c01  when c00=c11=0.p^*=\frac{c_{10}-c_{00}}{c_{10}-c_{00}+c_{01}-c_{11}},\qquad p^*=\frac{c_{10}}{c_{10}+c_{01}}\ \text{ when } c_{00}=c_{11}=0. For priors, classes ω1,…,ωn\omega_1,\dots,\omega_n have training priors pt(ωi)=Nt,i/Ntp_t(\omega_i)=N_{t,i}/N_t, the model's outputs satisfy gi(x)≈pt(ωi∣x)g_i(x)\approx p_t(\omega_i\mid x), and the within-class densities do not change between training and deployment, p(x∣ωi)=pt(x∣ωi)p(x\mid\omega_i)=p_t(x\mid\omega_i) (label shift), which holds when the training set was selected on the class and not on xx.

Step 1

The decision is a threshold, and the cost matrix carries one number. Predicting 1 is optimal when (1−p)c10+pc11≤(1−p)c00+pc01(1-p)c_{10}+pc_{11}\le(1-p)c_{00}+pc_{01}, which rearranges to p≥p∗p\ge p^*. Adding kjk_j to the column of true class jj adds kjP(j∣x)k_jP(j\mid x) to both actions' expected costs (our reading, stronger than the paper's scaling and shifting invariance), so only the regrets c10−c00c_{10}-c_{00} and c01−c11c_{01}-c_{11} matter, and only through their ratio. Reasonableness puts p∗p^* in (0,1)(0,1) with class 1 predicted above it; a class whose row is entrywise at least another's is never worth predicting.

Step 2

Costs need one baseline, and they can vary by example. The German credit matrix charges 1 for rejecting a good applicant, 5 for approving a bad one, and 0 on the diagonal. But denying a loan produces the same cash flow whatever the applicant would have done, so in a coherent matrix both entries of the "predict bad" row are equal; measuring benefits from the state before the decision avoids the error. In Elkan's card example, refusing a fraud is worth +$20+\$20, refusing a legitimate customer −$20-\$20, approving a fraud −x-x and a legitimate purchase +0.02x+0.02x for amount xx, and Step 1 gives refuse  ⟺  P(fraud∣x) ≥ 20+0.02x40+1.02x,\text{refuse}\iff P(\text{fraud}\mid x)\ \ge\ \frac{20+0.02x}{40+1.02x}, which is 0.5 at x=0x=0, about 15% at \$100, about 3.8% at \$1,000, and tends to 2%. A threshold that moves with each transaction cannot come from any single rebalancing of the training set, only from a probability plus a per-example decision.

Step 3

Under label shift the posterior is the old one reweighted by the prior ratio (Elkan's Theorem 2, and Saerens' correction). Equate the class densities in Bayes for the two populations, pt(ωi∣x)pt(x)/pt(ωi)=p(ωi∣x)p(x)/p(ωi)p_t(\omega_i\mid x)p_t(x)/p_t(\omega_i)=p(\omega_i\mid x)p(x)/p(\omega_i). The unknown factor pt(x)/p(x)p_t(x)/p(x) is common to all classes and fixed by normalization, sop(ωi∣x)=p(ωi)pt(ωi) pt(ωi∣x)∑jp(ωj)pt(ωj) pt(ωj∣x),for two classeslogit⁡p′=logit⁡p+logit⁡b′−logit⁡b,p(\omega_i\mid x)=\frac{\dfrac{p(\omega_i)}{p_t(\omega_i)}\,p_t(\omega_i\mid x)}{\sum_{j}\dfrac{p(\omega_j)}{p_t(\omega_j)}\,p_t(\omega_j\mid x)},\qquad\text{for two classes}\quad \operatorname{logit}p'=\operatorname{logit}p+\operatorname{logit}b'-\operatorname{logit}b,with b,b′b,b' the old and new base rates. Elkan's page of algebra is this one line, and it uses nothing about the learner. He reads it as relaxing the assumption that training and test come from one population; it relaxes it only to label shift, where the class proportions move and nothing else does. In logits the correction adds the log prior ratio to each class, which is also the logit adjustment of Menon et al. (later in this section).

Step 4

Rebalancing is a threshold move (Elkan's Theorem 1). Thinning the negatives is a base-rate change, so Step 3 applies. Multiplying the negatives by kk without changing their distribution divides the training odds by kk, so a learner's rule pS≥p0p_S\ge p_0 reads odds⁡(p)≥k odds⁡(p0)\operatorname{odds}(p)\ge k\,\operatorname{odds}(p_0), and making it act as p≥p∗p\ge p^* needsk=p∗1−p∗⋅1−p0p0,k=\frac{p^*}{1-p^*}\cdot\frac{1-p_0}{p_0},which is c10/c01c_{10}/c_{01} for an accuracy learner with a zero diagonal (Breiman et al., 1984). With p∗=0.05p^*=0.05 and an accuracy learner, keep one negative in 19 or weight negatives by 1/19. Read backward, a model trained with negatives kept at rate rr reports odds inflated by 1/r1/r, and multiplying its odds by rr recovers the population probability. Both directions need the thinning to leave P(x∣0)P(x\mid0) alone: random sampling or weights do, while choices that correlate with xx (keeping only low-score negatives, say) break the theorem.

Step 5

For learners that estimate probabilities, rebalancing changes little. This part is argued, without experiments. A Bayesian classifier models each P(x∣j)P(x\mid j) separately, so rebalancing changes only the estimated P(j)P(j), exactly a threshold move; naive Bayes ranks well but gives probabilities that are too extreme, so its threshold should be set empirically, which rebalancing to 0.5 cannot do. For tree splits with impurity α(p(1−p))β\alpha(p(1-p))^\beta, Bayes inside each branch givesI(A)α=P(1)βP(0)β∑kP(ak)1−2β P(ak∣1)βP(ak∣0)β,\frac{I(A)}{\alpha}=P(1)^\beta P(0)^\beta\sum_k P(a_k)^{1-2\beta}\,P(a_k\mid1)^\beta P(a_k\mid0)^\beta,so at β=1/2\beta=1/2 the base rates factor out and the best split ignores them (Theorem 3), while for Gini (β=1\beta=1) the mixture P(ak)P(a_k) brings them back. Prevalence matters at pruning instead, where C4.5 often reduces a tree with a rare class to a single leaf predicting the common class, and Laplace pruning (smooth a leaf with kk positives out of nn to (k+1)/(n+2)(k+1)/(n+2), prune when the parent's expected loss is below the children's total) is close to no pruning: without smoothing a parent's expected loss is never below its children's total, since the minimum of an average is at least the average of the minima. Hence the recommendation: train on the data as given, smooth the probabilities, and apply the threshold per example.

Step 6

With unknown new priors, the likelihood in the priors is concave and EM finds its maximum. By the identity of Step 3, a deployment point has p(xk)=pt(xk)∑iπigi(xk)/pt(ωi)p(x_k)=p_t(x_k)\sum_i\pi_ig_i(x_k)/p_t(\omega_i) with πi=p(ωi)\pi_i=p(\omega_i), and pt(xk)p_t(x_k) does not involve π\pi, soℓ(π)=∑k=1Nlog⁡∑i=1nπi gi(xk)pt(ωi)+const.\ell(\pi)=\sum_{k=1}^N\log\sum_{i=1}^n\pi_i\,\frac{g_i(x_k)}{p_t(\omega_i)}+\text{const}.The classifier never has to produce a density, because gi(xk)/pt(ωi)g_i(x_k)/p_t(\omega_i) is p(xk∣ωi)p(x_k\mid\omega_i) up to a factor common to all classes. A sum of logs of linear functions is concave on the simplex, so the paper's worry that EM might stop at a local maximum does not apply (our observation, the standard fact about mixture weights with fixed components). EM treats the deployment labels as missing: the E-step computes the corrected posteriors at the current priors, and the M-step setsp(s+1)(ωi)=1N∑k=1Np(s)(ωi∣xk),p^{(s+1)}(\omega_i)=\frac1N\sum_{k=1}^Np^{(s)}(\omega_i\mid x_k),starting from the training priors. A fixed point solves the score equation multiplied by πi\pi_i, and EM started inside the simplex converges to the maximizer, linearly and more slowly when the classes overlap; five iterations sufficed in the paper. The M-step shows how errors propagate: the estimated prior averages posteriors over the whole deployment set, so a systematic calibration error on the bulk of one class passes straight into it.

Step 7

The competing method matches moments of hard decisions, and a likelihood ratio test asks whether the priors moved. The confusion-matrix method solves p(δi)=∑jpt(δi∣ωj)p(ωj)p(\delta_i)=\sum_jp_t(\delta_i\mid\omega_j)p(\omega_j) for the new priors, using that a decision is a function of xx, so its class-conditional law survives label shift. It needs only an invertible confusion matrix, so it stays consistent for a miscalibrated classifier, which EM does not (Lipton et al., shift section). Here the matrix is estimated on the training set, where the classifier looks more accurate than it is (both error rates too small), which plausibly pulls its estimates toward the balanced training prior (our reading, not tested in the paper). For the test, the shared factor ∏kpt(xk)\prod_kp_t(x_k) cancels and2log⁡LLt=2∑k=1Nlog⁡∑i=1np^(ωi)pt(ωi) pt(ωi∣xk)2\log\frac{L}{L_t}=2\sum_{k=1}^N\log\sum_{i=1}^n\frac{\hat p(\omega_i)}{p_t(\omega_i)}\,p_t(\omega_i\mid x_k)is referred to χn−12\chi^2_{n-1} under unchanged priors (Wilks); the paper adjusts only when it rejects, since adjusting under no shift cost a little accuracy. The null ignores miscalibration of the outputs, so on a very large deployment set a small calibration error alone will produce a rejection.

Step 8

In the experiments, estimated priors shrink toward 50%, EM less than the confusion matrix, and adjustment recovers most of the lost accuracy. On Ringnorm with a small network trained on balanced data, a true 10% prior was estimated at 14.7% by EM and 18.1% by the confusion matrix, and accuracy rose from 90.1% unadjusted to 93.6% with EM, against 94.0% with the true priors; the test rejected in every shifted run and in none at 50%. On three medical data sets trained 50/50 and tested at 20/80, adjustment always helped. Nothing here covers priors below 10% or shifts that are not pure label shift.

Consequences and limits. The lasting content is the separation of estimation from decision, with rebalancing and prior shift reduced to odds corrections that do not improve the learner. For payment fraud, decline when P(fraud∣x)≥p∗(x)P(\text{fraud}\mid x)\ge p^*(x), with p∗p^* set per transaction by the loss on an approved fraud against the cost of declining a good customer. A model trained with legitimate traffic kept at rate rr needs its odds multiplied by rr first, and when the live fraud rate moves, EM on the day's scores estimates it long before chargebacks label the traffic, with the test as an inexpensive check that it has moved. But fraud shift is rarely pure label shift: a new attack changes p(x∣fraud)p(x\mid\text{fraud}), EM reports it as a change in prevalence, and the test says only that something moved. With fraud at a fraction of a percent, the EM estimate also averages posteriors over millions of legitimate transactions, so a calibration error on them of the same order as the prevalence corrupts it, and calibration (later in this section) becomes a prerequisite; Alexandari et al. (2020) found EM on recalibrated outputs hard to beat. The claim that rebalancing has little effect is argued for naive Bayes and trees only; for a misspecified discriminative model it is false: Fithian and Hastie (later in this section) show that subsampling changes what a misspecified logistic regression converges to, enough to reverse a coefficient's sign, and suggest using the effect on purpose to aim the fit at the decision threshold.

Terms

cijc_{ij}
cost of predicting class ii when the truth is jj; c10c_{10} false positive, c01c_{01} false negative
p∗p^*
(c10−c00)/(c10−c00+c01−c11)(c_{10}-c_{00})/(c_{10}-c_{00}+c_{01}-c_{11}); predict positive iff P(1∣x)≥p∗P(1\mid x)\ge p^*
regrets
c10−c00c_{10}-c_{00} and c01−c11c_{01}-c_{11}; the decision depends only on their ratio
common baseline
every cost measured from the state before the decision; a reject row has equal entries
Theorem 2
logit⁡p′=logit⁡p+logit⁡b′−logit⁡b\operatorname{logit}p'=\operatorname{logit}p+\operatorname{logit}b'-\operatorname{logit}b
Theorem 1
multiply negatives by odds⁡(p∗)/odds⁡(p0)\operatorname{odds}(p^*)/\operatorname{odds}(p_0) to make threshold p0p_0 act as p∗p^*
subsampling correction
negatives kept at rate rr: multiply the model's odds by rr
ℓ(π)\ell(\pi)
∑klog⁡∑iπigi(xk)/pt(ωi)\sum_k\log\sum_i\pi_i g_i(x_k)/p_t(\omega_i); concave, so the EM limit is the global MLE
confusion-matrix method
solve p(δi)=∑jpt(δi∣ωj)p(ωj)p(\delta_i)=\sum_jp_t(\delta_i\mid\omega_j)p(\omega_j); needs invertibility; calibration is not required
calibration requirement
EM's estimate inherits any systematic error in the posteriors, strongly for a rare class

Quiz

  1. A fraud model was trained on all frauds and 1 in 50 legitimate transactions, kept at random. It outputs 0.20 for a transaction. What is the population fraud probability?
  2. A team keeps all frauds and only the legitimate transactions their old model scored lowest, then applies the Theorem 2 odds correction. What goes wrong?
  3. Fraud is 0.1% of transactions. Why can EM's prior estimate be badly off even with a classifier that ranks well?
  4. A new attack type pushes fraud from 0.2% to 0.4%, with features unlike past fraud. What does the EM procedure deliver?

Sources