ML0197 Loss Choice for 1:9 Imbalance

Given a cancer-screening dataset split 100 positive vs 900 negative, what loss will you use and why? How exactly does it help, and what metric do you report?

Three-panel scenario card. Panel one shows a ten by ten grid of squares, each square standing for 10 patients, with 10 red squares for 100 cancers and 90 white squares for 900 healthy scans. Panel two shows a first attempt trained with plain binary cross-entropy at threshold 0.5 reaching 91 percent accuracy and 0.93 ROC-AUC while catching only 38 of 100 cancers, with a note that an all-negative model already scores 90 percent accuracy. Panel three shows two open decision boxes: which training loss among plain BCE, weighted BCE, or focal loss, and which metric to report among accuracy, ROC-AUC, PR-AUC, or sensitivity at a threshold.

The Problem: A cancer-screening model is trained on 1,000 scans, 100 with cancer and 900 healthy. Plain BCE reaches 91% accuracy and 0.93 ROC-AUC yet catches only 38 of 100 cancers at threshold 0.5, and the team must choose a loss and a reported metric that an all-negative model’s 90% accuracy cannot fool.

Answer

I would train with class-weighted binary cross-entropy, setting the positive weight to w = 900/100 = 9 so the 100 cancers carry as much total gradient as the 900 healthy scans. At the optimum, weighting adds \log 9 to every logit, which equals moving the threshold from 0.5 to 0.1, and during training it keeps the negatives from dominating every update. Focal loss is the tool for much harsher imbalance flooded with easy negatives, so it is not my first choice at 1:9. For reporting I lead with PR-AUC (average precision) against its no-skill baseline of 0.10, plus sensitivity, specificity and PPV at a threshold tuned on validation to the clinical sensitivity target. Accuracy is useless because an all-negative model already scores 90%, and ROC-AUC flatters the model because it ignores how many false alarms each detected cancer costs.

(1) Cost Asymmetry Is The Real Reason: Weight 9 at threshold 0.5 is equivalent to declaring a missed cancer as costly as 9 false alarms. Imbalance alone does not justify reweighting; the asymmetric cost of a false negative does.
(2) Gradient Balance: At initialization, where every prediction is 0.5, plain BCE receives a total gradient of 450 from healthy scans and 50 from cancers, so early updates mostly learn to say “healthy”. With w = 9 the two totals are 450 and 450.
(3) Weighting Is Mostly A Threshold Shift: For a well-specified model with enough data, weighting only adds \log w to each logit, so the ranking, and therefore ROC-AUC and PR-AUC, barely changes. It still helps a small or heavily regularized model spend capacity near the cancers, but it leaves the scores miscalibrated by a factor of 9 in odds.
(4) Focal Loss Targets Easy-Negative Floods: The factor (1-p_t)^{\gamma} with \gamma = 2 cuts a confident healthy scan at p_t = 0.95 by 400 times. It was built for dense detection at roughly 1:1000, and at 1:9 it mainly adds a second hyperparameter and distorts probabilities.
(5) PR-AUC Plus An Operating Point: A random ranker scores ROC-AUC 0.5 at any prevalence but PR-AUC equal to the prevalence, 0.10 here, so PR-AUC shows how far above chance the model really is. The decision itself is reported as sensitivity, specificity and PPV at the validated threshold, with confidence intervals.

Mathematical Formulation:
p = \sigma(z)
\ell_w = -w\,y\log p - (1-y)\log(1-p)
w = N_{-}/N_{+} = 900/100 = 9
\partial \ell_w / \partial z = (1-y)\,p - w\,y\,(1-p)
\mathrm{logit}\,p_w^{*}(x) = \mathrm{logit}\,q(x) + \log w
p_w^{*}(x) \geq 0.5 \iff q(x) \geq \frac{1}{1+w} = 0.1
\mathrm{FL}(p_t) = -\alpha_t\,(1-p_t)^{\gamma}\log p_t
\mathrm{AP} = \sum_{n}(R_n - R_{n-1})\,P_n
\mathrm{PPV} = \frac{\pi\,\mathrm{TPR}}{\pi\,\mathrm{TPR} + (1-\pi)\,\mathrm{FPR}}

Where:

  • p is the predicted cancer probability, \sigma the sigmoid, and z the model’s logit for one scan.
  • \ell_w is the per-example weighted BCE, y \in \{0, 1\} the label with 1 meaning cancer, and w the positive-class weight (the pos_weight argument of PyTorch’s BCEWithLogitsLoss).
  • N_{-} and N_{+} are the negative and positive counts, so w = 9 gives both classes equal total loss mass.
  • \partial \ell_w / \partial z is the gradient on the logit: a cancer’s push w(1-p) is scaled by 9, a healthy scan’s push p is unchanged.
  • q(x) = P(y = 1 \mid x) is the true posterior, p_w^{*} the minimizer of the expected weighted loss, and \mathrm{logit}\,u = \log(u/(1-u)). The shift by \log w assumes a model flexible enough to reach the optimum.
  • \mathrm{FL} is focal loss, p_t the probability assigned to the true class, \gamma \geq 0 the focusing parameter (\gamma = 0 recovers weighted BCE), and \alpha_t the class weight, which Lin et al. set to 0.25 on the rare class because \gamma = 2 already rebalances.
  • \mathrm{AP} is average precision, the step-wise PR-AUC, with P_n and R_n the precision and recall at the n-th threshold over sorted scores; its no-skill value equals the prevalence.
  • \mathrm{PPV} is precision at a fixed threshold, \pi the prevalence, \mathrm{TPR} the sensitivity, and \mathrm{FPR} = 1 - \mathrm{specificity}.
Two-panel chart. Left: gradient magnitude on the logit against predicted cancer probability; a cancer under weighted BCE falls linearly from 9 to 0, a cancer under plain BCE falls from 1 to 0, and a healthy scan rises from 0 to 1; a note says that at p equal to 0.5 the summed gradient is 50 from cancers versus 450 from healthy scans under plain BCE and 450 versus 450 under weighted BCE. Right: fraction of loss kept by focal loss against the probability of the true class for gamma 0, 1, 2 and 5; marked points show that with gamma 2 an easy healthy scan at 0.95 keeps 0.25 percent of its loss while a hard cancer at 0.30 keeps 49 percent.

Figure 1: (a) Gradient on the logit per example. With w = 9 a cancer predicted at 0.5 pushes with 4.5 instead of 0.5, so at initialization the 100 cancers and 900 healthy scans each contribute a total of 450. (b) Focal loss keeps only (1-p_t)^{\gamma} of each loss: at \gamma = 2 a confident healthy scan keeps 0.25% while a hard cancer at p_t = 0.30 keeps 49%.

Worked Example:
Take a scan whose true cancer posterior is q = 0.2. A plain-BCE model outputs 0.2 and misses it at threshold 0.5. The weighted model outputs 9 \times 0.2 / (9 \times 0.2 + 0.8) = 0.69 and flags it, exactly as the plain model would at threshold 0.1. Now suppose the trained model, at its chosen threshold, flags 180 scans: 90 of the 100 cancers and 90 of the 900 healthy scans. Sensitivity and specificity are both 0.90, precision is 90/180 = 0.50, and accuracy is (90 + 810)/1000 = 0.90. That is the same 90% the all-negative model scores, so accuracy cannot separate a model that catches 90 cancers from one that catches none.

PropertyWeighted BCE (pos_weight 9)Focal loss (gamma 2)Plain BCE + tuned threshold
What it changesScales every cancer’s loss and gradient by 9Shrinks the loss of confident examples by (1 – p_t)^gamma, plus an alpha class weightNothing in training; only the decision cut-off moves
ProbabilitiesOdds inflated 9x; undo by subtracting log 9 from the logitDistorted with no closed-form fix; recalibrate on validationAs calibrated as the model itself
Extra knobsOne, set by the class ratio or the cost ratioTwo (alpha, gamma), hard to tune with 100 positivesNone beyond the threshold
Best whenModerate imbalance with a real cost asymmetry, as in this screening setExtreme imbalance flooded by easy negatives, such as dense detection near 1:1000Plenty of data and a flexible model, where weighting reduces to a threshold shift
Main riskAmplifies any label noise among the 100 positivesUnderweights easy cases and adds tuning variance on a tiny positive setNegatives dominate early updates and small batches may hold no cancers

Login to view more content
Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *