Why Classification Can Estimate a Density Ratio
Under covariate shift, source and target covariates have distributions \(P_s\) and \(P_t\), with densities \(p_s\) and \(p_t\) relative to a common dominating measure. The density ratio \(r(x)=p_t(x)/p_s(x)\), where \(p_s(x)>0\), tells us how the target distribution reweights source covariate values. A classifier trained to distinguish observations drawn from these two distributions provides another way to estimate that ratio: its class probabilities encode the same relative density information.
The conversion is not simply “take the classifier odds.” It depends on the proportions of source and target observations used to train the classifier. It also depends on estimating the relevant conditional class probability well. These details matter whether the samples are naturally pooled or deliberately assembled in fixed numbers.
The Classifier-to-Ratio Identity
The target and source labels have prior probabilities \(\pi\) and \(1-\pi\). By Bayes’ rule, the conditional probability of a target label at a covariate value \(x\) is proportional to the target density multiplied by the target prior. This yields an exact identity for the population classifier probability.
Proof. The joint densities for the two possible labels and covariate value are \((1-\pi)p_s(x)\) for \(D=0\) and \(\pi p_t(x)\) for \(D=1\). Their sum is the marginal density of \(X\). Dividing the target joint density by that sum gives
When \(p_s(x)>0\) and \(p_t(x)>0\), the resulting probability lies strictly between zero and one. Its odds are
Rearranging proves the stated ratio formula. If \(p_t(x)=0\) while \(p_s(x)>0\), then \(\eta(x)=0\) and \(r(x)=0\), consistent with the formula. If \(p_s(x)=0\), the source-to-target ratio is not defined there; absolute continuity rules out target mass on such a set. \(\square\)
The factor \((1-\pi)/\pi\) is essential. The odds \(\eta(x)/(1-\eta(x))\) estimate the density ratio multiplied by the target-to-source sampling odds, not the density ratio alone. In a pooled data set containing \(n_s\) source and \(n_t\) target observations, the sample target proportion is \(n_t/(n_s+n_t)\). If the classifier is fit to that pooled data without reweighting, this is the relevant training proportion. When class weighting or resampling changes the effective class proportions, the correction must use those effective proportions.
Worked Example: A Gaussian Shift and the Classifier Intercept
Let the source covariate satisfy \(X\sim N(0,1)\) and the target covariate satisfy \(X\sim N(1,1)\). Suppose the classifier-training sample has target proportion \(\pi=1/4\). Find the density ratio and express the population classifier probability in terms of it.
The two normal densities are \(p_s(x)=(2\pi)^{-1/2}\exp(-x^2/2)\) and \(p_t(x)=(2\pi)^{-1/2}\exp(-(x-1)^2/2)\). Therefore
The prior odds are \(\pi/(1-\pi)=1/3\). The classifier odds are thus \(r(x)/3\), so its log odds are
At \(x=1\), the true ratio is \(r(1)=\exp(1/2)\), and the target-class probability is \(\eta(1)=\exp(1/2)/(3+\exp(1/2))\). Applying the corrected-odds formula gives \[ 3\frac{\eta(1)}{1-\eta(1)} =3\frac{\exp(1/2)}{3} =\exp(1/2), \] as required. The intercept correction is visible in the log-odds expression: the classifier’s log odds differ from \(\log r(x)\) by \(-\log 3\).
Calibration, Sampling, and Practical Estimation
For a fitted classifier, let \(\widehat{\eta}(x)\) be its estimated target-class probability. The corresponding ratio estimate is
This transformation is appropriate when \(\widehat{\eta}(x)\) estimates the conditional probability \(P(D=1\mid X=x)\) under the classifier’s training mixture. A classifier that ranks observations well need not estimate that probability accurately. In particular, calibration in the weaker sense that predicted probabilities match class frequencies after grouping observations by score does not, by itself, ensure that the score equals the conditional class probability at each covariate value. Ratio recovery requires an adequate estimate of the latter, or assumptions that make the score sufficient for it.
The odds transformation is especially sensitive near probabilities zero and one. A small probability error near one can produce a large change in the estimated ratio. Thus calibration, overlap, and the sampling design all affect the quality of the result. A value \(\widehat{\eta}(x)=1\) produces an infinite transformed ratio; this can indicate extreme estimated separation rather than reliable evidence of an infinite population ratio.
Worked Example: Correcting for an Unequal Training Mixture
Suppose \(X\) has two possible values, \(a\) and \(b\), with source probabilities \(P_s(a)=0.8\), \(P_s(b)=0.2\), and target probabilities \(P_t(a)=0.3\), \(P_t(b)=0.7\). A classifier-training sample has target proportion \(\pi=0.4\). Compute the classifier probabilities and recover both ratios.
At \(a\), the target joint probability is \(0.4(0.3)=0.12\), while the source joint probability is \(0.6(0.8)=0.48\). Hence \(\eta(a)=0.12/(0.12+0.48)=0.2\). The corrected odds yield
At \(b\), the target and source joint probabilities are \(0.4(0.7)=0.28\) and \(0.6(0.2)=0.12\). Therefore \(\eta(b)=0.28/(0.28+0.12)=0.7\), and
The raw classifier odds are \(0.25\) at \(a\) and \(7/3\) at \(b\); neither equals the corresponding density ratio. Multiplying each by the prior-odds correction \(0.6/0.4=1.5\) recovers the correct values.
Direct Ratio Fitting as an Alternative
A classifier first estimates a class probability and then transforms its odds. Direct ratio fitting instead chooses a function \(g\) to approximate the density ratio using source and target covariate samples. One population objective is the square-loss functional below. Its minimizer is exactly the density ratio under a second-moment condition; absolute continuity alone is not enough for this conclusion.
Proof. By the definition of the Radon–Nikodym derivative, \(E_t[g(X)]=E_s[r(X)g(X)]\) whenever the expectation is integrable. The Cauchy–Schwarz inequality gives
Thus \(E_t[|g(X)|]\) is finite. Both \(J(g)\) and \(J(r)\) are finite, since \(E_s[r^2]<\infty\) also implies \(E_t[|r(X)|]=E_s[r(X)^2]<\infty\). Using \(E_t[g]=E_s[rg]\) and \(E_t[r]=E_s[r^2]\), we obtain
Equality holds exactly when \(E_s[(g-r)^2]=0\), which is equivalent to \(g=r\) \(P_s\)-almost everywhere. This proves the identity and uniqueness. \(\square\)
Worked Example: Checking the Direct Objective on a Discrete Space
Let \(X\in\{a,b\}\), with \(P_s(a)=P_s(b)=1/2\) and \(P_t(a)=1/4\), \(P_t(b)=3/4\). The density ratios are \(r(a)=1/2\) and \(r(b)=3/2\). Compare \(J(g)\) for \(g(a)=0,\ g(b)=2\) with its value at \(r\).
First, \(E_s[g^2]=\tfrac12(0^2)+\tfrac12(2^2)=2\), and \(E_t[g]=\tfrac14(0)+\tfrac34(2)=3/2\). Thus \(J(g)=\tfrac12(2)-3/2=-1/2\). For the ratio itself,
The objective gap is \(J(g)-J(r)=-1/2+5/8=1/8\). The square-completion identity gives the same result:
This verifies both the minimizer and the scale of the objective difference for a concrete example.
Choosing a Route and Avoiding Common Errors
Classification is attractive when a flexible classifier is readily available and the class probability can be estimated and calibrated. Direct fitting targets the ratio itself and can avoid an intermediate probability-to-odds transformation. Neither route removes the need for support overlap or reliable information in regions where the target differs from the source. For the direct square-loss objective, the finite-moment requirement is substantive: if \(E_s[r^2]=\infty\), the square-completion identity above does not apply as a finite identity on \(L^2(P_s)\).
Common mistake: treating raw odds as the density ratio. The incorrect reasoning is that Bayes’ rule gives \(\eta/(1-\eta)=p_t/p_s\). In fact, Bayes’ rule gives \(\eta/(1-\eta)=[\pi/(1-\pi)](p_t/p_s)\), so the odds must be multiplied by \((1-\pi)/\pi\). A related error is to use the intended population proportions when the classifier was actually trained with resampling or class weights that changed its effective prior. Use the proportions corresponding to the classifier probabilities being transformed.
A second pitfall is to assume that any well-ranked classifier supplies good ratios. Ranking quality concerns ordering; the ratio formula uses probability magnitudes. Probability estimation and calibration therefore matter, and errors can be amplified when \(\widehat{\eta}(x)\) is close to one.
Check Your Understanding
Work through these questions before applying the conversion to data.
- Why does a classifier trained with equal numbers of source and target observations require a different odds correction from one trained with a target proportion of \(1/4\)?
- If \(\pi=0.2\) and a classifier estimates \(\eta(x)=0.6\), what density ratio does the corrected-odds formula give?
- In the square-loss direct-fitting result, why is \(E_s[r(X)^2]<\infty\) needed in addition to absolute continuity?
- Explain why good ranking performance alone does not guarantee an accurate density-ratio estimate.
- If class weighting changes the effective training proportion, which proportion belongs in the classifier-to-ratio conversion?