← All Posts
Machine Learning · Regression · Logistic Regression · Part 6 of 10

Decision Thresholds, Class Imbalance, and Error Costs

A logistic model returns a probability. A deployed system often needs a decision such as approve or reject, alert or stay silent, and positive or negative. A threshold connects the probability to that decision.

Fitting the probability model and choosing the threshold are separate tasks. This page explains how error costs determine the threshold, what class imbalance actually changes, and how weighting or resampling can distort probabilities unless the changed class prior is corrected.

From Probability to Decision

The model estimates $p=\Pr(y=1\mid x)$. A hard classifier predicts the positive class when $p\ge t$.

Changing $t$ does not change the model, its probabilities, or the ranking of rows. It only moves the cut on that fixed ranking, so an operating point can be changed after training.

Probability and threshold have different meanings. A calibrated probability describes how likely the outcome is. The threshold describes when acting on that probability is worth the cost. The population prevalence is already reflected in a calibrated probability, so it is not added to the cost threshold a second time.

The Cost-Optimal Bayes Threshold

First consider the common case in which correct decisions cost nothing. If the positive class is predicted, a cost occurs only when the row is actually negative. Its expected cost is $(1-p)C_{\mathrm{FP}}$. If the negative class is predicted, the expected cost is $pC_{\mathrm{FN}}$.

$$\text{cost of predicting positive}=(1-p)C_{\mathrm{FP}},\qquad \text{cost of predicting negative}=pC_{\mathrm{FN}}.$$

The positive decision is cheaper when $(1-p)C_{\mathrm{FP}}\le pC_{\mathrm{FN}}$. Solving this inequality gives the Bayes threshold:

$$t^\star=\frac{C_{\mathrm{FP}}}{C_{\mathrm{FP}}+C_{\mathrm{FN}}}.$$

If a missed positive costs five times as much as a false alarm, then $C_{\mathrm{FN}}=5C_{\mathrm{FP}}$ and $t^\star=1/6\approx0.167$. Acting at a lower probability is reasonable because waiting for more certainty risks the more expensive mistake.

Only the cost ratio matters. Multiplying both costs by the same number leaves the threshold unchanged. The formula also requires calibrated probabilities. If $p=0.2$ does not truly mean about a $20\%$ positive rate, applying a mathematically correct threshold to that number still produces the wrong decision.

When correct actions also carry costs, let $C(a,y)$ be the cost of action $a$ when the truth is $y$. The full expected costs are

$$\mathbb{E}[\text{cost}\mid a{=}1]=p\,C(1,1)+(1-p)\,C(1,0),\qquad \mathbb{E}[\text{cost}\mid a{=}0]=p\,C(0,1)+(1-p)\,C(0,0).$$

Comparing these two values gives the same rule with the extra error costs $A=C(1,0)-C(0,0)$ and $B=C(0,1)-C(1,1)$:

$$t^\star=\frac{A}{A+B}.$$

Sweeping the Threshold to Minimize Cost

A validation sweep checks how the theoretical rule behaves on finite data. The animation uses twelve validation predictions, four positives and eight negatives, with $C_{\mathrm{FP}}=1$ and $C_{\mathrm{FN}}=5$. Each step changes the threshold and computes

$$\text{realized cost}=\mathrm{FP}+5\,\mathrm{FN}.$$

▶ Total Cost as the Decision Threshold Sweeps

Costs are $C_{\mathrm{FP}}=1$, $C_{\mathrm{FN}}=5$. Each bar is the realized cost $1\cdot\text{FP}+5\cdot\text{FN}$ at that threshold on the fixed validation set. Counts and costs are computed at runtime.

The smallest realized cost occurs at $t=0.2$. All four positives are caught, and six false alarms cost $6$. At $t=0.5$, two positives are missed and three false alarms remain, so the cost is $2(5)+3(1)=13$. The empirical best value, $0.2$, differs slightly from the theoretical $0.167$ because the validation set is small and the displayed threshold grid is coarse.

Why One Half Is Rarely the Right Threshold

The value $0.5$ is optimal only when false positives and false negatives have equal cost. Accuracy makes exactly that equal-cost assumption, so the threshold with highest accuracy need not have the lowest real cost.

A threshold of $0.5$ also has probability meaning only when the score is calibrated. For an arbitrary ranking score, it is merely a number on the score scale. A validation sweep can still choose an operating cut, but the closed-form Bayes threshold applies only after calibration.

What Class Imbalance Actually Breaks

A rare positive class does not automatically make logistic regression invalid. With an unpenalized intercept, the fitted score equation gives

$$\sum_i(p_i-y_i)=0\quad\Longrightarrow\quad \frac{1}{n}\sum_i p_i=\frac{1}{n}\sum_i y_i.$$

The average fitted probability equals the training positive rate. This is only an aggregate identity, not proof that every probability is calibrated or that the model shape is correct. Imbalance creates other practical problems:

SymptomMechanism
Accuracy becomes misleadingPredicting the majority class for every row can achieve high accuracy while finding no positives.
Coefficient estimates become noisyFew positive examples provide little information about how the positive class changes with the features.
Validation becomes unstableA handful of changed positive predictions can move recall, precision, and the selected threshold sharply.
The default cut may predict no positivesUseful rankings can still place most rare-event probabilities below $0.5$.
Model selection may ignore rare signalA metric dominated by majority rows can prefer regularization or features that perform poorly on the rare class.

Rarity and cost are different. Rarity limits information and makes some metrics unstable. Cost asymmetry describes which mistakes matter more. A balanced dataset can have unequal costs, and an imbalanced dataset can have equal costs. Unequal costs often need only a threshold change; too little minority information may require more data, careful weighting, or resampling.

Class Weighting, Resampling, and Prior Correction

Class weighting multiplies each row's loss by a class-dependent weight:

$$J_w(\beta)=\frac{1}{\sum_i w_{y_i}}\sum_{i=1}^{n} w_{y_i}\big[\operatorname{softplus}(z_i)-y_i z_i\big],$$

A larger positive-class weight gives positive rows more influence. Integer weighting is equivalent to duplicating those rows in the loss. Random oversampling does this directly, while random undersampling removes majority rows and loses information.

A simple intercept correction is valid only for pure prior changes: class weights are constant within each class, or rows are sampled randomly within each class, so the feature distribution inside each class is preserved. Under those conditions, rebalancing changes the intercept but not the population slopes. Methods such as SMOTE create synthetic feature values and can change the class-conditional feature distribution, so they can change slopes as well.

For a pure prior change, the population and rebalanced-sample log-odds differ by a constant:

$$\operatorname{logit}\Pr_{\text{pop}}(y{=}1\mid x)=\operatorname{logit}\Pr_{\text{sample}}(y{=}1\mid x)+\ln\frac{\pi_1}{\pi_0}-\ln\frac{\rho_1}{\rho_0},$$

where $\pi$ is the true population class proportion and $\rho$ is the effective proportion used for training. The population intercept is therefore

$$\beta_0^{\text{pop}}=\hat\beta_0+\ln\frac{\pi_1}{\pi_0}-\ln\frac{\rho_1}{\rho_0}.$$

Without this correction, a model trained on a 50:50 sample can report probabilities that are far too high in a population where positives are rare. A held-out recalibration can replace the formula when the rebalancing changed more than the prior.

A threshold is the direct tool for unequal costs. Moving the threshold changes decisions without changing the probability model. Weighting or resampling is more appropriate when rare examples provide too little signal for stable fitting, but population probabilities then require prior correction or recalibration.

Choosing the Operating Point in Practice

The operating rule depends on the real constraint:

Each rule is chosen from validation predictions and monitored after deployment. The cost-based Bayes threshold itself depends on costs, not prevalence. However, prevalence drift can make old model probabilities miscalibrated, so the probabilities need correction or recalibration before the same threshold remains meaningful.

Validation Without Test Leakage

Threshold selection is part of model selection. The data roles remain separate:

Trying several thresholds on the test set turns it into another validation set and makes the reported performance optimistic. Nested cross-validation preserves the same separation when data are scarce.

Stratified splits usually keep enough positives in every fold, but temporal and grouped data need time-aware or group-aware splits to prevent leakage. Thresholds based on only a few positive rows have high uncertainty. With cross-validation, one threshold is selected from the pooled out-of-fold predictions; averaging separately selected fold thresholds does not generally minimize the pooled cost.

Probability correction comes first. Case-control sampling, oversampling, and class weighting alter the effective prior. An intercept correction or held-out recalibration restores population probabilities before a cost formula is applied.

Assumptions, Edge Cases, and the Implementation

The C++ threshold and cost section applies a cost matrix and sweeps validation thresholds using stable probability primitives. A dedicated evaluation and calibration chapter is also planned.