Models that are well calibrated on a validation set often become badly overconfident when deployed at a new site, such as a new hospital, camera location, or laboratory batch. Being calibrated means that predictions made with 80% confidence turn out correct about 80% of the time. The usual fix is post-hoc calibration: fit a small correction, such as one "temperature" that softens all logits, on held-out labeled data. Practitioners then mostly ask which correction to fit. This proposal asks a different question that survey statisticians answered decades ago for a closely related problem: given a fixed labeling budget, how should the validation data be sampled so that the calibrator works on sites it has never seen? Field surveys of schools, clinics and villages are run as two-stage designs. They pick some clusters, then some individuals inside each cluster. The precision of the result is governed by how much the quantity of interest varies between clusters versus within them. Calibration for unseen sites has the same structure. A new hospital is a fresh draw of a cluster, so adding more labeled patients from the hospitals already sampled cannot remove the between-hospital part of the error.
The concrete proposal treats the per-cluster calibration parameter (log-temperature) as a random effect nested inside strata. It estimates the between-cluster variance from data and uses that one estimate for three things. First, it decides how to spend a labeling budget across "number of sites" versus "examples per site." Second, it produces shrinkage temperatures that borrow strength across sites in the same stratum. Third, it attaches an honest predictive interval to the calibration a model should expect at a new site. If the between-cluster share of temperature variance is near zero on real benchmarks, the idea fails cleanly and flat temperature scaling is enough. Measuring that share is the first decisive experiment.
Core Claim
The closest existing work is Multi-domain Temperature Scaling, Robust Calibration with Multi-domain Temperature Scaling. It fits one temperature per labeled domain, then trains a linear regressor from penultimate features to temperature so it can predict temperatures on unseen domains. It identified the "average trap": a model can look calibrated on pooled data while being miscalibrated inside every domain. That paper treats domains as flat and treats the validation set as given. It does not ask how the validation data should have been collected. It does not model nesting such as slides within hospitals or experiments within cell types. It gives no statement of how uncertain the predicted temperature is for a new domain.
Our claim is that post-hoc calibration under cluster shift is a two-stage estimation problem, and that its error on a new cluster splits into two parts. One part is reducible by labeling more clusters. The other is reducible only by labeling inside the new cluster. Write the log-temperature of cluster $c$ in stratum $s$ as
$$\log T_{sc} = \mu_s + u_{sc}, \qquad u_{sc} \sim \mathcal{N}(0, \tau_s^2),$$
where $\mu_s$ is the stratum's typical log-temperature and $\tau_s^2$ is the between-cluster variance. A per-cluster estimate from $m$ labeled examples carries additional within-cluster noise $\sigma_s^2/m$. Suppose we label $k$ clusters with $m$ examples each, at cost $c_1$ per cluster and $c_2$ per example. Classical two-stage allocation gives the variance of the stratum estimate as $\tau_s^2/k + \sigma_s^2/(km)$. That variance is minimized at
$$m^\ast = \sqrt{\frac{c_1\,\sigma_s^2}{c_2\,\tau_s^2}},$$
and an unadapted calibrator still incurs the irreducible $\tau_s^2$ on any new cluster. The formula is the textbook design used in, for example, the two-stage school-then-student national assessment of Trends in National Assessment of Educational Achievement (2003~2005). What is new is applying it to the calibration parameter of a frozen network and testing whether its predictions hold on real distribution-shift benchmarks.
Research Program
The work has three parts, built on frozen classifiers and post-hoc fitting only.
The first part is measurement. On benchmarks with genuine nesting, we fit a per-cluster temperature by negative log-likelihood and estimate $\tau_s^2$ and $\sigma_s^2$ by restricted maximum likelihood on the per-cluster estimates and their bootstrap standard errors. From these we report the intra-cluster share $\rho = \tau^2/(\tau^2+\sigma^2)$ at each level of the hierarchy. The nested benchmarks are hospitals and slides in Camelyon17, cell types, experiments and plates in RxRx1, and camera locations in iWildCam. They also include the corruption-family, corruption-type and severity tree in ImageNet-C, all from WILDS: A Benchmark of in-the-Wild Distribution Shifts and the MD-TS evaluation. Prior work motivates expecting $\rho$ to be large. Beyond Post-Hoc Temperature Scaling: Bilevel Optimization for LLM Calibration argues that a single temperature is inherently domain-dependent. Beyond Accuracy: Assessing Calibration of Geospatial Foundation Models and Their Sensitivity to Distribution Shifts shows calibration degrading under geographic and sensor shift. The survey Classifier calibration: a survey on how to assess and improve predicted class probabilities makes explicit that post-hoc calibrators assume the validation distribution persists at deployment. None of these papers quantifies how calibration variance divides across levels of a sampling hierarchy.
The second part is the estimator. We fit a hierarchical empirical-Bayes temperature model in which each cluster's temperature shrinks toward its stratum mean in proportion to its own sampling noise. For a brand-new cluster with $n$ labels, we use the posterior that combines the stratum prior with those labels. With $n=0$, it reduces to the stratum mean plus a predictive band of width governed by $\tau_s$. Crucially, we allow the prior mean to come from the MD-TS feature regressor rather than a constant. The hierarchical model then adds a calibrated variance and a label-efficient update on top of the strongest existing method instead of competing with it. The predictive band on temperature propagates to a band on expected accuracy at the new site, which MD-TS reports as a point estimate only.
The third part is design. Using the estimated $\rho$ and plausible cost ratios (site onboarding is expensive, extra examples are cheap), we predict the optimal $(k, m)$ split. We then verify empirically that this split beats the default practice of a random validation sample pooled across whatever sites happen to be available.
Evaluation must itself respect the hierarchy. Pooled ECE (expected calibration error, the average gap between confidence and accuracy over confidence bins) weights large clusters heavily and hides the average trap. We therefore report cluster-averaged ECE and worst-decile cluster ECE over held-out clusters. Here we borrow the survey-weighting logic of A general class of improved population variance estimators under non-sampling errors using calibrated weights in stratified sampling, which concerns design weights for population estimates rather than classifiers. Per-cluster ECE is biased upward at small cluster sizes, so negative log-likelihood and Brier score serve as co-primary metrics.
Baselines
The naive reference point is one global temperature fitted on the pooled validation set. This is standard practice, and the comparison shows how much the average trap costs on each benchmark. It uses exactly the same labeled examples as every other method.
The strongest existing competitor is MD-TS from Robust Calibration with Multi-domain Temperature Scaling. It is the only method in the file that targets calibration on unseen domains from multi-domain validation data, and it already exploits domain labels. Its reported numbers set the bar. On WILDS-RxRx1 it lowers out-of-distribution ECE from 13.78% with standard temperature scaling to 5.25%. On ImageNet-C with ResNet-50 it lowers ECE from 5.70% to 4.55%, and it cuts accuracy-prediction mean absolute error from 3.96% to 1.70%. We will rerun MD-TS on identical frozen features, identical cluster splits and identical labeling budgets, tuning its regularization on the same leave-cluster-out protocol as ours. For Camelyon17, iWildCam and Amazon we found no reported calibration result on Lacuna for MD-TS or for temperature scaling. On those benchmarks, the comparison rests entirely on our reruns. Beating MD-TS means lower cluster-averaged and worst-decile ECE on held-out clusters at matched budget, or equal ECE together with predictive intervals whose empirical coverage of new-site accuracy is near nominal, which MD-TS does not provide.
Two further comparisons are included. One is per-cluster temperature scaling with no pooling, the opposite extreme from the global temperature. The other is the instance-wise scaler Sample-Dependent Adaptive Temperature Scaling for Improved Calibration. That method predicts a per-example temperature from class-cluster likelihood in a learned latent space and reports CIFAR-100 ECE moving from 3.76% to 2.95%, but it ignores population clusters entirely.
Two ablations isolate the key ingredient. The first, our model with the nesting collapsed to a single level, tests whether hierarchy matters beyond flat partial pooling. The second, our model with the MD-TS prior mean replaced by a constant, tests how much the feature-based prior contributes.
Why not just…?
Why not just use MD-TS, whose feature regressor already generalizes to unseen domains without any labels? This is the real threat. If feature-predicted temperatures already capture almost all between-cluster variation, the residual $\tau^2$ after conditioning on features will be near zero and the shrinkage step adds nothing. We measure exactly this residual, because the hierarchical model uses MD-TS as its prior mean. Two outcomes are possible. If the residual is substantial, the gain comes from the variance and the few-label update. If it is negligible, the contribution narrows to the allocation result and the interval, and we will say so. Either way, MD-TS acknowledges its linear map may be too restrictive, and it offers no guidance on collecting validation data.
Why not just fit one temperature per stratum (urban versus rural, cell type) and stop? This is the cheapest form of grouping and may capture most of the gain if variation lives at the top level. The variance decomposition across levels answers this directly. If $\rho$ at the cluster level is small relative to the stratum level, per-stratum scaling is the right recommendation and the cluster random effect is unnecessary. We report that outcome as a finding, not a failure.
Why not just label more validation examples from the sites you already have? The two-stage formula predicts this saturates at the floor $\tau_s^2$ whenever between-site variance is non-trivial. The allocation experiment tests it head-on. At equal total labels, we compare "many examples from few sites" against "few examples from many sites" and check whether the measured optimum sits where $m^\ast$ predicts. If added within-site labels keep improving held-out-site ECE with no plateau, the design argument is wrong.
Proposed Experiments
Experiments use frozen ERM models released with WILDS on Camelyon17, RxRx1, iWildCam and Amazon, plus a ResNet-50 on ImageNet-C with its natural corruption hierarchy. All splits hold out entire top-level clusters, so every reported number is about sites never seen during calibration. For each benchmark we subsample validation labels under a grid of $(k, m)$ designs at several fixed budgets and repeat over cluster resamplings to obtain confidence intervals on the metrics themselves.
Three checks decide the proposal. The first is whether $\rho$, measured after conditioning on MD-TS features, is materially above zero on at least the cluster-rich benchmarks (RxRx1, iWildCam). If not, the hierarchical estimator is redundant. The second is whether the empirically best $(k, m)$ split on held-out clusters matches the split predicted from $\rho$ and the cost ratio, and whether it beats random pooled sampling at equal budget. The third is whether the 90% predictive interval for new-site accuracy, derived from $\tau_s$, actually contains the realized held-out-site accuracy about 90% of the time, where MD-TS provides only a point estimate. Success on the first two checks with good coverage on the third would establish that the classical survey design question of how many clusters versus how many units per cluster is the right lens for calibration under cluster shift.
References
- Robust Calibration with Multi-domain Temperature Scaling
- Trends in National Assessment of Educational Achievement (2003~2005)
- WILDS: A Benchmark of in-the-Wild Distribution Shifts
- Beyond Post-Hoc Temperature Scaling: Bilevel Optimization for LLM Calibration
- Beyond Accuracy: Assessing Calibration of Geospatial Foundation Models and Their Sensitivity to Distribution Shifts
- Classifier calibration: a survey on how to assess and improve predicted class probabilities
- A general class of improved population variance estimators under non-sampling errors using calibrated weights in stratified sampling
- Sample-Dependent Adaptive Temperature Scaling for Improved Calibration