March 15, 2026 · 2 min read
Calibrated Ensembles & Focal Loss for Ultra-Imbalanced Tabular Risk
Empirical study on combining PyTorch Deep Tabular ResNets with Tree Boosters and Platt probability calibration under <0.18% fraud skew.
Evaluating and deploying machine learning models under extreme class imbalance ($0.17%$ positive prevalence) requires moving beyond standard cross-entropy and uncalibrated decision heuristics.
1. Focal Loss Formulation
Standard Cross-Entropy gradients are heavily dominated by the sheer volume of negative transactions ($99.82%$). We implement a parameterized Binary Focal Loss in PyTorch:
$$\mathcal{L}_{\text{Focal}}(p_t) = -\alpha_t (1 - p_t)^\gamma \log(p_t)$$
Setting $\gamma = 2.0$ dynamically suppresses loss from easily-classified negative transactions ($p_t > 0.5$) and focuses backpropagation on ambiguous risk frontiers.
class BinaryFocalLoss(nn.Module):
def __init__(self, alpha: float = 0.80, gamma: float = 2.0):
super().__init__()
self.alpha = alpha
self.gamma = gamma
def forward(self, logits: torch.Tensor, targets: torch.Tensor) -> torch.Tensor:
probs = torch.sigmoid(logits)
p_t = targets * probs + (1 - targets) * (1 - probs)
alpha_factor = targets * self.alpha + (1 - targets) * (1 - self.alpha)
focal_weight = alpha_factor * ((1 - p_t) ** self.gamma)
bce = F.binary_cross_entropy_with_logits(logits, targets, reduction="none")
return (focal_weight * bce).mean()2. Platt Scaling Calibration
Tree ensembles and neural networks trained with reweighted losses output uncalibrated scores that do not reflect true posterior probability. We fit an out-of-fold logistic calibration mapping:
$$P(y = 1 \mid z) = \frac{1}{1 + \exp(A \cdot z + B)}$$
Minimizing the Expected Calibration Error (ECE) from $0.0624$ down to $0.0001$:
$$\text{ECE} = \sum_{m=1}^{M} \frac{|B_m|}{N} \left| \text{acc}(B_m) - \text{conf}(B_m) \right|$$
3. Empirical Test Results (42,722 Unseen Partition)
- Calibrated Meta-Ensemble: $0.8384$ PR-AUC, $96.77%$ Precision, $81.08%$ Recall, $100.00%$ Specificity, $0.0001$ ECE.
- XGBoost Inference Engine: $0.375\text{ ms}$ P50 latency, $139,185\text{ tx/sec}$ streaming throughput.
- Asymmetric Business Utility: Intercepted $$4,134$ net value on test set while eliminating $96.8%$ of manual compliance review overhead.