Is it the model or the database? Separating label-scope and prevalence effects from real failure in balanced medical metrics

Medical classifiers are usually judged with balanced metrics. The most common is macro-F1, which scores each disease class separately and then averages, so a rare condition counts as much as a common one. These scores are almost always computed inside a single database: MIMIC for clinical-note coding, TCGA for cancer subtyping. They are then read as properties of the model. They are not. Two quantities come from the database itself. The first is which classes enter the average at all, since classes with no positive test cases are dropped. The second is how prevalent each class is, and per-class precision, and therefore F1, moves with prevalence even when the model's behaviour on each patient is unchanged. Move the same model to a database with a different scope (another era, another coding system, another care unit, another contributing site) and its macro-F1 can collapse or inflate for reasons that have nothing to do with how well it recognises disease.

This note proposes to measure that effect directly. We will split any cross-database change in a balanced metric into three parts: a term from which classes are in scope, a term from prevalence shift within shared classes, and a residual we call the "conditional term", which captures real changes in how patients with a given label look to the model. The working hypothesis is that, for long-tail medical labels, the first two parts explain a large and currently unreported share of apparent degradation and of model-ranking flips. A second claim follows. Once per-class true-positive and false-positive rates are measured in the source database, a model's target macro-F1 and its target decision thresholds can be predicted from an estimate of target prevalence alone, with no target labels for rare classes.

Where this sits

Three strands of the file motivate the idea. On the metric side, Edin et al. re-implement six medical coding models on MIMIC-III and MIMIC-IV. They show that macro-F1 had been miscomputed and that correcting it, which includes dropping codes absent from the test set, roughly doubles reported scores. They also show that tuning decision thresholds matters more than architecture and that rare-code sparsity, not document length, is the bottleneck. Their correction is right within a split. It also makes the evaluated class set depend on the split, and once databases differ in scope, macro-F1 values from different splits are no longer comparable. Our scope term formalises exactly this. Nguyen et al. build the MIMIC-IV-ICD9 and MIMIC-IV-ICD10 benchmarks and report Macro AUC up to 97.07% alongside Macro F1 as low as 5.42% on ICD-10, where half the codes occur in three or fewer summaries. They also find that a synonym-matching model (MSMN) beats a pretrained language model (PLM-ICD) on the tail. A prevalence-invariant ranking score next to a prevalence-dependent decision score is precisely the gap our decomposition explains: the AUC barely notices that tail prevalence is tiny, while F1 is crushed by it.

Kleiman and Page supply a method we reuse. Their AUCμ is a multi-class AUC built on the argmax decision rule; it scores perfect models at 1 and random ones at 0.5, and it is insensitive to class skew. That skew insensitivity makes it a diagnostic here: if AUCμ is stable across databases while macro-F1 moves, the movement is prevalence arithmetic, not lost discrimination.

On the database side, bibliometric work shows that single-repository queries silently filter what is visible. Examples include Web-of-Science-only maps (Zou, Li et al.) and pipelines that drop records lacking a digital identifier (Yang et al.). That literature never asks what such filters do to the class marginals that downstream metrics depend on. Abbasi et al. benchmark 35 classifiers on 153 TCGA datasets with macro metrics but never leave TCGA.

One paper constrains our design: Xu et al. report that their global-AUC decision rule for when weak labels help hides class-level effects on imbalanced NIH-CXR. This warns that any database-level summary must be broken out per class, which is why our decomposition is built per class.

Core Claim

For each class $c$, write the source database's true-positive rate as $t_c$, its false-positive rate as $f_c$, and the class prevalence as $\pi_c$. At a fixed threshold, the per-class F1 is

$$F1_c(\pi_c) = \frac{2 t_c \pi_c}{t_c \pi_c + \pi_c + f_c (1-\pi_c)},$$

which follows from writing true positives, false positives and false negatives as rates times prevalence. If the conditional behaviour ($t_c, f_c$) holds fixed, the target F1 is just this curve evaluated at the target prevalence. For a fixed $f_c$ it falls steeply as $\pi_c$ shrinks, which is the regime of tail codes.

Let $C_S$ and $C_T$ be the in-scope class sets of the source and target databases. The observed change splits exactly into three telescoping parts:

$$\Delta = \underbrace{\big[\bar F_{C_T}(\pi^S) - \bar F_{C_S}(\pi^S)\big]}_{\text{scope}} + \underbrace{\big[\bar F_{C_T}(\pi^T) - \bar F_{C_T}(\pi^S)\big]}_{\text{prevalence}} + \underbrace{\big[\text{observed} - \bar F_{C_T}(\pi^T)\big]}_{\text{conditional}}.$$

Here $\bar F_{C}(\pi)$ is the macro average over class set $C$ of the per-class F1 computed from source rates at prevalences $\pi$. For classes in $C_T$ that are absent from the source test set, source rates come from validation or pooled estimates; we report how many classes need this.

The novelty is not the formula. It is applying it as a standard accounting step for long-tail medical benchmarks, showing how big each term is in practice, and using it to re-select thresholds. Thresholds get re-selected per class by maximising the transported F1 under an estimated target prevalence. That estimate can come from coding frequencies, which hospitals usually know, or from a confusion-matrix label-shift estimator applied to unlabeled target predictions.

Research Program

We will build three kinds of database pairs, ordered from most to least controlled.

The first is synthetic re-prevalence within MIMIC-IV. We resample test documents so that class frequencies change while documents within a class are drawn from the same pool. This holds the conditional term near zero by construction and checks that the estimator recovers macro-F1 when it should.

The second is realistic scope filters inside one repository. These are the clinical analogue of single-database queries: restrict MIMIC-IV by admission year, by care unit, or by admission type, and treat each restriction as a target "database". These filters change both marginals and conditionals, as real repository boundaries do.

The third is real repository shifts. One is MIMIC-III to the MIMIC-IV ICD-9 benchmark, which is the same hospital in a different era and release, with a different code footprint. Another is source archives within ISIC 2019, an 8-class single-label task where AUCμ applies directly. A third is TCGA subtyping split by tissue-source site, extending Abbasi et al. beyond pooled TCGA.

For each pair we report the three terms, with bootstrap intervals for all of them, since multi-class rank metrics lack closed-form intervals. We report AUCμ on the multi-class tasks and per-class AUC on the multi-label ones as a conditional-shift check. We also track pairwise model-ranking flips, for example PLM-ICD against MSMN and Bi-GRU against CNN, broken down by code-frequency tier. The ranking question echoes the BioBench finding that accuracy mis-ranks strong models (Stevens et al.): we ask whether cross-database rank flips on the tail are mostly prevalence-driven and therefore predictable.

Baselines

The naive reference point is to carry over source-database macro-F1 as the predicted target score and deploy source-tuned thresholds unchanged. This is what every single-database benchmark implicitly does. Our estimator beats it if its absolute error in predicting target macro-F1 is clearly smaller across pairs, and if transported thresholds yield higher realised target macro-F1 than source thresholds.

The strongest existing pipeline is that of Edin et al.: corrected macro-F1, stratified splits that keep tail codes in every partition, and thresholds tuned on the source validation set. We run their reproduced models (Bi-GRU, CAML, LAAT, PLM-ICD), add MSMN following Nguyen et al., and use identical splits, training compute and tuning budgets across conditions. The file gives no numeric macro-F1 from Edin et al.; it says only that the correction "effectively doubles" earlier scores. The concrete reference numbers are therefore Nguyen et al.'s Macro AUC of 97.07% and Macro F1 of 5.42% on MIMIC-IV-ICD10, and PLM-ICD's micro-F1 of 59.8% on MIMIC-III (Huang et al.). We first reproduce these within-database figures, and treat a reproduction outside a small tolerance as a pipeline bug to fix before any cross-database claim.

The most dangerous competitor is threshold re-tuning on a small labeled target sample. We run it with budgets of 100, 500 and 2,000 labeled target documents and plot realised target macro-F1 against budget. Our label-free method counts as beating it if it matches or exceeds the realised score at budgets typical of a local audit.

Two ablations isolate the key ingredient: transport with the prevalence correction only, keeping the source class set, and the scope correction only, keeping source prevalences. Together they show which term carries the gain.

Why not just…?

Why not just report prevalence-invariant metrics such as macro AUC or AUCμ and stop? Those metrics are stable across databases precisely because they ignore operating points. The 97.07% versus 5.42% gap in Nguyen et al. shows that a ranking score can look excellent while every deployed decision on the tail is poor. We use AUCμ as a diagnostic of conditional shift, not as a substitute for a decision metric that clinicians act on.

Why not just re-tune thresholds on a few labeled target cases? This is a real threat for frequent codes, and the budget curve will show where it wins. For tail codes it fails structurally. When half of ICD-10 codes appear in three or fewer summaries in a database of 122,000 documents, a local sample of a few hundred documents contains essentially no positives for most tail codes, so per-class thresholds cannot be fit. If the budget curve shows that small samples suffice on the tail too, we will report that and narrow our claim to frequent and mid-frequency codes.

Why not just pool databases or use stratified splits so the problem disappears? Stratification, as in Edin et al.'s MIMIC-III clean split, fixes which classes appear within one database. It does not change the fact that the deployment site has its own scope and prevalences. Pooling trains a better model but still evaluates it at the pooled marginals. Both are compatible with our accounting and neither replaces it. To make this concrete, we include a pooled-training condition and show its decomposition too.

Why not just oversample rare classes? Oversampling changes the training marginals, not the evaluation marginals. It also tends to memorise the few minority examples available, as the file notes. We report it as an extra training condition, but it does not address what the score means at a new site.

Proposed Experiments and Decisive Checks

Three checks decide the proposal.

The first is the calibration test. On synthetic re-prevalence, the conditional term should be near zero and transported macro-F1 should match observed macro-F1 within bootstrap error. If it does not, the estimator is wrong and the program stops there.

The second is the central empirical claim. On the realistic scope filters and real repository shifts, we report the share of the absolute macro-F1 change explained by the scope and prevalence terms, separately for head, mid and tail frequency tiers. The hypothesis is supported if those two terms account for the majority of the change on the tail. It is refuted if the conditional term dominates there, which would mean real distribution shift is the main story and is itself worth reporting. Cross-checking against AUCμ and per-class AUC tells the two cases apart: stable rank metrics alongside a large conditional term would point to an error in our rate estimates.

The third is the practical payoff. Across all pairs, transported thresholds should raise realised target macro-F1 over source thresholds. They should also predict which pairwise model-ranking flips occur, for example whether MSMN's tail advantage over PLM-ICD survives the move. We will report the rate of correctly predicted flips against a coin-flip reference.

All three use only MIMIC-III, MIMIC-IV-ICD9/ICD10, ISIC 2019 and TCGA, with code and resampling seeds released so that other single-database benchmarks can attach the same three-term accounting to their scores.