Tutorials › Statistical Learning (Graduate) › Density-Ratio Estimation By Classification

Robustness and Distribution Shift · Tutorial 970 of 989

Density-Ratio Estimation By Classification

Learn how classifier probabilities encode density ratios, how sampling proportions enter the conversion, and when direct ratio fitting is a useful alternative.

Advanced 12 min read

What You'll Learn

  • Derive the density-ratio formula from Bayes’ rule for a source-versus-target classifier.
  • Correct classifier odds for the target proportion in the pooled sample.
  • Identify the support and calibration conditions needed for the conversion.
  • Compare classification-based estimation with a square-loss direct ratio objective.
  • Verify the direct objective’s minimizer under a finite second-moment condition.
  • Recognize why raw classifier odds and merely score-calibrated probabilities can mislead.

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.

Definition: Let \(D=1\) indicate that an observation is drawn from the target covariate distribution, and let \(D=0\) indicate source. Suppose \(P(D=1)=\pi\), where \(0<\pi<1\), and \(X\mid D=0\sim P_s\), \(X\mid D=1\sim P_t\). The classifier probability is \(\eta(x)=P(D=1\mid X=x)\). The density ratio is \(r(x)=p_t(x)/p_s(x)\) wherever \(p_s(x)>0\).

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.

Theorem: Suppose \(p_s\) and \(p_t\) are densities relative to the same measure and \(0<\pi<1\). At any \(x\) where \(p_s(x)>0\), $$ \eta(x)=\frac{\pi p_t(x)}{(1-\pi)p_s(x)+\pi p_t(x)}. $$ If \(p_t(x)>0\) as well, then $$ r(x)=\frac{1-\pi}{\pi}\frac{\eta(x)}{1-\eta(x)}. $$ If \(P_t\) is absolutely continuous with respect to \(P_s\), this ratio is finite and defined \(P_s\)-almost everywhere.

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

$$ P(D=1\mid X=x) =\frac{\pi p_t(x)}{(1-\pi)p_s(x)+\pi p_t(x)}. $$

When \(p_s(x)>0\) and \(p_t(x)>0\), the resulting probability lies strictly between zero and one. Its odds are

$$ \frac{\eta(x)}{1-\eta(x)} =\frac{\pi p_t(x)}{(1-\pi)p_s(x)} =\frac{\pi}{1-\pi}r(x). $$

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

$$ r(x)=\frac{p_t(x)}{p_s(x)} =\exp\left(-\frac{(x-1)^2}{2}+\frac{x^2}{2}\right) =\exp(x-\tfrac12). $$

The prior odds are \(\pi/(1-\pi)=1/3\). The classifier odds are thus \(r(x)/3\), so its log odds are

$$ \log\frac{\eta(x)}{1-\eta(x)} =x-\tfrac12-\log 3. $$

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

$$ \widehat r(x)=\frac{1-\pi}{\pi} \frac{\widehat{\eta}(x)}{1-\widehat{\eta}(x)}. $$

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

$$ \widehat r(a)=\frac{0.6}{0.4}\frac{0.2}{0.8} =1.5(0.25)=0.375=\frac{0.3}{0.8}. $$

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

$$ \widehat r(b)=\frac{0.6}{0.4}\frac{0.7}{0.3} =1.5\left(\frac{7}{3}\right)=3.5=\frac{0.7}{0.2}. $$

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.

Theorem: Suppose \(P_t\) is absolutely continuous with respect to \(P_s\), with density ratio \(r=dP_t/dP_s\), and \(E_s[r(X)^2]<\infty\). For \(g\in L^2(P_s)\), define $$ J(g)=\frac12 E_s[g(X)^2]-E_t[g(X)]. $$ Then \(E_t[|g(X)|]<\infty\), \(J(g)\) is finite, and $$ J(g)-J(r)=\frac12 E_s[(g(X)-r(X))^2]. $$ Consequently, \(r\) is the unique minimizer in \(L^2(P_s)\), up to equality \(P_s\)-almost everywhere.

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

$$ E_s[|r(X)g(X)|] \leq \big(E_s[r(X)^2]\big)^{1/2} \big(E_s[g(X)^2]\big)^{1/2}<\infty. $$

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

$$ \begin{aligned} J(g)-J(r) &=\frac12E_s[g^2]-E_s[rg] -\frac12E_s[r^2]+E_s[r^2]\\ &=\frac12E_s[g^2-2rg+r^2]\\ &=\frac12E_s[(g-r)^2]\geq 0. \end{aligned} $$

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,

$$ E_s[r^2]=\tfrac12(\tfrac14)+\tfrac12(\tfrac94)=\tfrac54, \qquad E_t[r]=\tfrac14(\tfrac12)+\tfrac34(\tfrac32)=\tfrac54, $$ $$ J(r)=\tfrac12(\tfrac54)-\tfrac54=-\tfrac58. $$

The objective gap is \(J(g)-J(r)=-1/2+5/8=1/8\). The square-completion identity gives the same result:

$$ \frac12E_s[(g-r)^2] =\frac12\left\{\tfrac12(\tfrac12)^2+\tfrac12(\tfrac12)^2\right\} =\frac12(\tfrac14)=\tfrac18. $$

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.

Key takeaway: Under a source–target classification mixture with target proportion \(\pi\), the density ratio is the classifier’s target-versus-source odds multiplied by \((1-\pi)/\pi\). The identity is exact for the population conditional probability; estimated ratios inherit the classifier’s probability-estimation and calibration errors.

Check Your Understanding

Work through these questions before applying the conversion to data.

  1. 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\)?
  2. If \(\pi=0.2\) and a classifier estimates \(\eta(x)=0.6\), what density ratio does the corrected-odds formula give?
  3. In the square-loss direct-fitting result, why is \(E_s[r(X)^2]<\infty\) needed in addition to absolute continuity?
  4. Explain why good ranking performance alone does not guarantee an accurate density-ratio estimate.
  5. If class weighting changes the effective training proportion, which proportion belongs in the classifier-to-ratio conversion?
Clinical Biostats