Why Use a Covariance-Adjusted Distance?
Nearest-neighbor matching requires a rule for deciding which control is closest to a treated unit. When covariates are measured in different units, ordinary Euclidean distance can give undue influence to variables with large numerical scales. It can also treat correlated covariates as if they contributed independent information. Mahalanobis distance addresses both issues by measuring a covariate difference relative to the covariance structure of the covariates.
The distance is a ranking tool for covariate similarity. It does not, by itself, establish that a comparison is causally valid, determine which units should be retained, or guarantee that covariate balance is adequate after matching. As with other matching designs, causal interpretation requires appropriate design assumptions, including that relevant confounders have been measured and that treated units have comparable controls available.
Definition and Calculation
The prime denotes transpose. The difference \(d=x_T-x_C\) is a column vector. In practice, \(S\) is commonly estimated from the covariates of the eligible units before matching, using the same covariate definitions for treated and control units. A sample covariance matrix for \(N\) eligible units with covariate vectors \(x_i\) is
Here \(\bar{x}\) is the vector of covariate sample means. The distance calculation uses one common \(S\) for all candidate pairs, rather than a different covariance matrix for each pair. This gives the pairwise distances a consistent scale and makes candidate rankings comparable.
The covariance matrix must be invertible for this definition. Positive definiteness means \(v'Sv>0\) for every nonzero vector \(v\); it ensures that every nonzero covariate difference has a positive squared distance. The matrix reflects both covariate variances and covariances. Thus, a difference along a variable that varies widely in the eligible sample is generally less unusual than the same raw difference along a variable that varies little.
Worked Example: Ranking Two Controls for One Treated Unit
Suppose all covariate values are hypothetical, and a treated unit has covariates \(x_T=(10,5)'\). Two eligible controls have \(x_{C1}=(8,4)'\) and \(x_{C2}=(9,2)'\). Suppose the common covariance matrix, estimated from the eligible units, is \(S=\begin{pmatrix}4&2\\2&4\end{pmatrix}\). Calculate both distances and rank the controls.
First check that the matrix is invertible. Its determinant is \(4(4)-2(2)=12\), which is positive, and its leading diagonal entry is positive. Its inverse is
For control 1, the difference vector is \(d_1=x_T-x_{C1}=(2,1)'\). Therefore,
For control 2, \(d_2=x_T-x_{C2}=(1,3)'\). Thus,
Control 1 is the closer candidate because \(0.5774<1.528\). These distances are relative to the supplied covariance matrix; they are not distances in years, dollars, or any single covariate's units.
How Scale and Correlation Affect the Ranking
If the covariates are uncorrelated, the covariance matrix is diagonal. The squared distance then adds each squared difference divided by that covariate's variance. A difference of two units is treated differently when the variable's standard deviation is one than when its standard deviation is ten. This adjustment is one reason Mahalanobis distance can be preferable to raw Euclidean distance.
When covariates are correlated, the off-diagonal entries matter. Differences in a direction that commonly occurs together across covariates can be less unusual than differences in a direction rarely seen in the data. The next example isolates that feature.
Worked Example: Correlation Changes the Distance
Suppose two standardized covariates have common covariance matrix \(S=\begin{pmatrix}1&0.9\\0.9&1\end{pmatrix}\). A treated unit has two control candidates with difference vectors \(d_A=(1,1)'\) and \(d_B=(1,-1)'\). The ordinary Euclidean distances are both \(\sqrt{2}\), so Euclidean distance ties the candidates. Find their Mahalanobis distances.
The determinant of \(S\) is \(1-(0.9)^2=0.19\), so
For candidate A, the two covariates differ in the same direction, which is consistent with their strong positive correlation:
For candidate B, the covariates differ in opposite directions:
Mahalanobis distance ranks A as much closer. The result does not mean that opposite-direction differences are always undesirable; it means they are unusual relative to this particular covariance structure. The covariance matrix, and therefore the ranking, should be chosen and checked as part of the design rather than treated as an automatic truth about substantive importance.
Why the Covariance Adjustment Works
The adjustment can be understood as measuring ordinary Euclidean distance after transforming the covariates to remove their covariance scale. If \(S=LL'\), where \(L\) is an invertible matrix (for example, a Cholesky factor), define transformed covariates \(z=L^{-1}x\). For a pair, the transformed difference is \(L^{-1}d\). Its squared Euclidean length is
Thus, the Mahalanobis distance is ordinary distance in transformed coordinates. This also explains why the numerical units of the original covariates do not determine the distance by themselves: their scaling and linear dependence are incorporated through \(S\). More generally, an invertible linear re-expression of the covariates leaves the distance unchanged when the covariance matrix is transformed consistently. If \(y=Ax\), then its covariance is \(ASA'\), and
This invariance does not make the choice of covariates irrelevant. Adding, omitting, or nonlinearly transforming a covariate can change the geometry and the candidate rankings.
Worked Example: Recognizing a Singular Covariance Matrix
In a hypothetical dataset, two covariates are exact duplicates: \(X_2=X_1\) for every eligible unit. Suppose their covariance matrix is \(S=\begin{pmatrix}4&4\\4&4\end{pmatrix}\). A treated-control pair differs by \(d=(2,2)'\). Explain why the two-variable Mahalanobis distance cannot be calculated with this matrix, and give a defensible calculation if the duplicate variable is removed.
The determinant is \(4(4)-4(4)=0\). Therefore \(S^{-1}\) does not exist, so substituting the two-dimensional matrix into the Mahalanobis formula is not possible. The problem is not a computational inconvenience: the duplicated variables contain only one distinct dimension of variation.
If the design retains \(X_1\) and removes its exact duplicate \(X_2\), the one-dimensional covariance is \(S_1=(4)\), and the pair's difference on \(X_1\) is 2. The resulting distance is
This is a valid distance for the reduced, prespecified covariate representation. It is not the distance from the original singular two-variable matrix. A generalized inverse or other regularization may also be used in some settings, but it changes the metric and requires an explicit design rationale; it should not be applied merely to make software return a number.
Using Distances in a Matching Design
To use Mahalanobis distance, first specify the covariates, the eligible units, and the common covariance matrix. Then calculate the distance from each treated unit to each eligible control candidate and rank candidates from smallest to largest for that treated unit. The matching design must separately state rules such as replacement, ratio, calipers, and how to handle units with no acceptable candidate. Those rules affect which pairs are ultimately formed; a distance alone does not settle them.
Use relevant pretreatment covariates, with clear units and coding. Avoid including an exact duplicate or a deterministic linear combination of other covariates.
Estimate \(S\) from the specified eligible units before using outcomes. Check its rank, invertibility, and numerical conditioning.
For each treated-control candidate pair, calculate \(d=x_T-x_C\), then \(d'S^{-1}d\) and its square root. Smaller values rank as closer.
Apply the stated matching rules and inspect covariate balance and overlap in the matched sample. A low distance for selected pairs does not establish adequate balance for the design as a whole.
Mahalanobis ranking differs from nearest-neighbor matching on a propensity score: the former uses distances in the chosen covariate space, while the latter ranks units using a scalar score. It also differs from exact matching, which requires equality on specified covariates, and from coarsened exact matching, which requires agreement within chosen categories or intervals. Each approach encodes similarity differently. The choice depends on the design objective and available overlap, not on a universal claim that one distance is best.
Diagnostics, Failure Modes, and Limitations
Several checks help identify when the calculation or its use is problematic:
- Check invertibility and conditioning. Exact redundancy makes \(S\) singular. Near-redundancy can make \(S^{-1}\) numerically unstable, producing distances that are highly sensitive to small changes in the data.
- Inspect candidate distances and overlap. If a treated unit has only very large distances to all controls, the ranking still identifies the closest available control, but that does not make the pair close in an absolute design sense. A separate restriction or a decision to leave the unit unmatched may be appropriate.
- Check balance after matching. Compare covariate distributions and standardized differences in the matched sample. A distance ranking is not a proof that all covariates are balanced, particularly when the design has constraints or many covariates.
- Examine sensitivity to the covariance estimate. The pooled covariance matrix depends on which units and covariates are included. A few extreme observations or a poorly conditioned estimate can materially alter rankings.
- Respect pretreatment timing. Matching on variables affected by treatment can induce bias. Covariates should be selected for the causal design, not solely because they improve a numerical distance.
Mahalanobis distance also has limitations in high dimensions. As the number of covariates grows relative to the number of eligible units, estimating and inverting the covariance matrix becomes difficult; exact or near collinearity becomes more likely, and distances may be less discriminating. Removing variables, combining them, or regularizing a covariance estimate can address numerical problems, but each choice changes what similarity means and should be justified before looking at outcomes. This distance does not correct unmeasured confounding, create overlap where none exists, or determine a treatment effect on its own.
Common Mistake
Check Your Understanding
Answer each question, then compare your reasoning with the solutions.
- For \(S=\begin{pmatrix}9&0\\0&1\end{pmatrix}\) and \(d=(3,1)'\), calculate \(D^2\) and \(D\).
- Why can two candidate pairs with equal Euclidean distances have different Mahalanobis distances?
- What does a singular covariance matrix indicate, and why can its inverse not be used in the distance formula?
- A treated unit's closest available control has distance 5, while other candidates have still larger distances. What can and cannot be concluded?
- Why should balance be assessed after matching even if all selected pairs have small Mahalanobis distances?
Solutions. (1) Here \(S^{-1}=\begin{pmatrix}1/9&0\\0&1\end{pmatrix}\), so \(D^2=3^2/9+1^2=2\), and \(D=\sqrt{2}\approx1.414\). (2) Mahalanobis distance incorporates covariate variances and correlations, whereas Euclidean distance does not; directions can have different covariance-adjusted lengths. (3) Singularity means at least one covariate dimension is redundant or has no independent variation. A singular matrix has determinant zero and no ordinary inverse, so the formula is undefined. (4) The control is the closest among those candidates under the selected metric, but distance 5 does not establish that it is acceptably close or that causal assumptions hold. (5) Pairwise closeness under a metric does not guarantee adequate balance across all covariate distributions in the matched sample; balance diagnostics are still needed.