Demo: Mahalanobis Distance Explorer

Demo: Mahalanobis Distance Explorer#

The Block 8 reading says that a residual is suspicious when its Mahalanobis distance \(D^2 = \boldsymbol{\nu}^{\top}\mathbf{S}^{-1}\boldsymbol{\nu}\) crosses a chi-squared threshold. This demo shows why that is the right statistic and why the absolute (Euclidean) size \(|\boldsymbol{\nu}|\) is not. In two dimensions you can see it: a residual of a given length can be completely ordinary in one direction and wildly improbable in another, because the filter’s own predicted spread \(\mathbf{S}\) is not the same in every direction.

The scenario is a two-component innovation from a loosely-coupled GPS position fix,

\[\begin{split} \boldsymbol{\nu} = \begin{bmatrix} \nu_1 \\ \nu_2 \end{bmatrix} = \begin{bmatrix} \nu_E \\ \nu_N \end{bmatrix} = \mathbf{z}_{\text{GPS}} - \hat{\mathbf{p}}^-, \qquad \mathbf{S} = \mathbf{P}^-_{EN} + \mathbf{R}_{\text{GPS}} = \begin{bmatrix} \sigma_1^2 & \rho\,\sigma_1\sigma_2 \\ \rho\,\sigma_1\sigma_2 & \sigma_2^2 \end{bmatrix}, \end{split}\]

with east on the horizontal axis and north on the vertical. Correlated east/north errors (after a turn, or with skewed satellite geometry) make \(\mathbf{S}\) elongated and tilted.

Key equations#

The test. Under the healthy hypothesis \(D^2 \sim \chi^2(m)\) with \(m = 2\) here. For two degrees of freedom the chi-squared CDF has the closed form \(1 - e^{-x/2}\), so the threshold for a false-alarm probability \(P_{\text{FA}}\) is exact:

\[ D^2 = \boldsymbol{\nu}^{\top}\mathbf{S}^{-1}\boldsymbol{\nu} \ \underset{\text{healthy}}{\overset{\text{fault}}{\gtrless}}\ \gamma, \qquad \gamma = \chi^2_{2,\,1-P_{\text{FA}}} = -2\ln P_{\text{FA}}. \]

Principal axes and whitening. Write \(\mathbf{S} = \mathbf{V}\boldsymbol{\Lambda}\mathbf{V}^{\top}\) with eigenvalues \(\lambda_1 \ge \lambda_2\) and unit eigenvectors \(\mathbf{v}_1\) (major axis) and \(\mathbf{v}_2\) (minor axis). Then

\[ D^2 = \sum_{k=1}^{2} \frac{(\mathbf{v}_k^{\top}\boldsymbol{\nu})^2}{\lambda_k}, \qquad \mathbf{u} = \mathbf{S}^{-1/2}\boldsymbol{\nu} = \mathbf{V}\boldsymbol{\Lambda}^{-1/2}\mathbf{V}^{\top}\boldsymbol{\nu}, \qquad D = |\mathbf{u}|. \]

Each square meter of residual along the minor axis adds \(1/\lambda_2\) to \(D^2\); each square meter along the major axis adds only \(1/\lambda_1\). In whitened coordinates \(\mathbf{u} \sim \mathcal{N}(\mathbf{0}, \mathbf{I})\), the threshold ellipse \(D^2 = \gamma\) becomes a circle of radius \(\sqrt{\gamma}\), and the Mahalanobis distance is ordinary distance. The demo uses the symmetric inverse square root \(\mathbf{V}\boldsymbol{\Lambda}^{-1/2}\mathbf{V}^{\top}\) because it stretches along the principal axes without rotating the picture; any other square root (Cholesky, for example) gives the same \(D^2\) but spins the plane.

A fair Euclidean competitor. The comparison test is \(|\boldsymbol{\nu}| > r_E\), with \(r_E\) chosen so that it has the same false-alarm probability under \(\boldsymbol{\nu} \sim \mathcal{N}(\mathbf{0}, \mathbf{S})\). Writing \(\mathbf{u}\) in polar form (\(R^2 \sim \chi^2_2\), angle uniform) gives a one-dimensional integral,

\[ P\big(|\boldsymbol{\nu}| > r\big) = \frac{1}{\pi}\int_0^{\pi} \exp\!\left(-\frac{r^2}{2\,q(\phi)}\right) d\phi, \qquad q(\phi) = \lambda_1\cos^2\phi + \lambda_2\sin^2\phi, \]

which the demo evaluates with a 1024-point periodic midpoint rule and solves for \(P(|\boldsymbol{\nu}| > r_E) = P_{\text{FA}}\) by bisection. The answer is deterministic, with no sampling involved. The Monte Carlo panel then confirms that both tests really do false-alarm at \(P_{\text{FA}}\).

Detection under a bias. If a fault adds a bias \(\mathbf{b}\) to every residual, \(D^2\) becomes a noncentral \(\chi^2_2\) with noncentrality \(\mathbf{b}^{\top}\mathbf{S}^{-1}\mathbf{b}\). The D² test’s detection probability therefore depends only on the Mahalanobis size of the bias, which is how surprising the bias is given what the filter expects.

Tie-in to the scalar test. The single-pseudorange test from the reading and the Fault Detection demo, \(D^2 = \nu^2/S \sim \chi^2(1)\), is the \(1 \times 1\) special case of this one. Stacking \(m\) measurements into \(\boldsymbol{\nu}\) gives \(D^2 \sim \chi^2(m)\); this demo is \(m = 2\), where the threshold region is a flat ellipse you can see and drag around in (at \(m = 3\) it becomes an ellipsoid, and beyond that it can no longer be drawn).

Interactive demo#

Open in full screen

The default scenario is the Elongated + tilted preset: \(\sigma_1 = 5\) m east, \(\sigma_2 = 3\) m north, \(\rho = 0.8\), at \(P_{\text{FA}} = 1\%\). Three test points sit on the residual plane: a free point \(\boldsymbol{\nu}\) that you can drag (or click anywhere to move it), and two “twins” A and B at the same \(|\boldsymbol{\nu}|\), one on the major axis and one on the minor axis. Drag either twin, or use the twins’ |ν| slider, and both move together. The Monte Carlo panel uses 200 000 seeded standard-normal pairs that are drawn once and then transformed, so every number is reproducible. The fault bias \(\mathbf{b}\) (8 m due north by default) can be set with the sliders, the axis chips, or by dragging the tip of its arrow.

Walkthrough#

  1. Read the covariance. The panel shows \(\mathbf{S} = [25.00\ \ 12.00;\ 12.00\ \ 9.00]\) m². Its principal standard deviations are \(\sqrt{\lambda_1} = 5.61\) m along the major axis at \(\theta = 28.2°\) and \(\sqrt{\lambda_2} = 1.61\) m along the minor axis, an eigenvalue ratio of \(\lambda_1/\lambda_2 = 12.19\). At \(P_{\text{FA}} = 1\%\) the threshold is \(\gamma = 9.21\) (\(\sqrt{\gamma} = 3.03\,\sigma\)). The red ellipse \(D^2 = \gamma\) has semi-axes \(\sqrt{\gamma\lambda_1} = 17.0\) m and \(\sqrt{\gamma\lambda_2} = 4.87\) m. The dashed amber equal-\(P_{\text{FA}}\) circle has \(r_E = 14.53\) m.

  2. Compare the twins. A \(= (5.29, 2.83)\) m and B \(= (-2.83, 5.29)\) m both have \(|\boldsymbol{\nu}| = 6.00\) m, well inside \(r_E\), so the Euclidean test passes both. The Mahalanobis test disagrees sharply. A has \(D^2 = 1.15\) (\(D = 1.07\)), an ordinary residual. B has \(D^2 = 13.97\) (\(D = 3.74\)) and raises an ALARM. The ratio of their \(D^2\) values is exactly \(\lambda_1/\lambda_2 = 12.19\).

  3. Look at the free point. \(\boldsymbol{\nu} = (-14.00, -7.00)\) m has \(|\boldsymbol{\nu}| = 15.65\) m \(> r_E\), so the Euclidean test raises an ALARM. But \(\boldsymbol{\nu}\) lies almost along the major axis, where the filter expects large errors, and \(D^2 = 7.86 < 9.21\) passes. The two tests disagree in both directions: the Euclidean test misses B and false-alarms on \(\boldsymbol{\nu}\).

  4. Read the whitened decomposition bars. \(D^2_{\nu} = 7.79 + 0.07\), \(D^2_A = 1.15 + 0.00\), and \(D^2_B = 0.00 + 13.97\), split into the major-axis (blue) and minor-axis (green) terms \((\mathbf{v}_k^{\top}\boldsymbol{\nu})^2/\lambda_k\). A residual pays for its \(D^2\) almost entirely along the axis it lies on, and the minor axis is twelve times more expensive per square meter.

  5. Toggle the whitened view. The plane morphs through \(\mathbf{W} = \mathbf{S}^{-1/2} = [0.277\ \ {-0.185};\ {-0.185}\ \ 0.524]\) m⁻¹. The red ellipse becomes a circle of radius \(3.03\), the \(D\) contours become concentric circles, and the axis ticks switch to σ units. A sits at distance \(1.07\), \(\boldsymbol{\nu}\) at \(2.80\) (inside), and B at \(3.74\) (outside), so \(D\) is now ordinary distance. The amber Euclidean circle turns into a long ellipse stretched along \(\mathbf{v}_2\). In whitened units it lets a minor-axis residual run out to \(r_E/\sqrt{\lambda_2} = 9.05\,\sigma\) before alarming, but alarms along the major axis at only \(r_E/\sqrt{\lambda_1} = 2.59\,\sigma\). Every readout is byte-for-byte identical with the view on or off, because the morph is presentation only.

  6. Drag the twins outward. B crosses the red ellipse at \(|\boldsymbol{\nu}| = 4.87\) m. A stays inside the ellipse until \(17.0\) m, past the Euclidean circle at \(14.53\) m. Drag \(\boldsymbol{\nu}\) around the ellipse’s edge and watch \(D^2\) hover near \(9.21\) while \(|\boldsymbol{\nu}|\) swings between 4.9 m and 17 m.

  7. Step through the \(P_{\text{FA}}\) chips. The thresholds are \(\gamma\) = 4.61, 5.99, 9.21, 11.62 and 13.82, with \(r_E\) = 9.37, 11.11, 14.53, 16.72 and 18.52 m, for 10%, 5%, 1%, 0.3% and 0.1%. B (\(D^2 = 13.97\)) alarms the D² test at every setting, even 0.1%, yet the Euclidean test never flags it, even at 10%. The free point alarms the Euclidean test at 1% and looser, but alarms the D² test only at 5% and 10%.

  8. Try the presets. Uncorrelated, equal gives \(\mathbf{S} = 16\,\mathbf{I}\). Both twins now have \(D^2 = 2.25\), the circle and ellipse coincide (\(r_E = 12.14\) m \(= 4\sqrt{\gamma}\)), and the two tests make identical decisions: 2 014 false alarms each, and \(P_D = 20.4\%\) for both. Elongated gives \(\mathbf{S} = \mathrm{diag}(36, 4)\) with \(\theta = 0°\). The twins sit on the coordinate axes with \(D^2 = 1.00\) and \(9.00\), so B now just passes (\(9.00 < 9.21\)). Setting \(\rho = 0.8\) with \(\sigma_1 = \sigma_2\) shows that equal diagonal entries are not enough, because correlation alone tilts and elongates \(\mathbf{S}\).

  9. Healthy Monte Carlo. At the defaults, the D² test alarms on 2 014 of 200 000 healthy draws (1.01%) and the Euclidean test on 2 000 (1.00%). Both are about 1% by construction. Now change \(\sigma_1\) to 7 m. The D² count stays at exactly 2 014, because whitening returns the same standard-normal \(\mathbf{z}\) for every \(\mathbf{S}\). The Euclidean count moves to 1 995, still within Monte Carlo noise of 1%.

  10. Faulted Monte Carlo. With \(\mathbf{b} = 8\) m due north (\(\varphi = 90°\)), the D² test detects \(P_D = 93.9\%\) and the Euclidean test only \(6.31\%\). Click b along v₂ (minor) (\(\varphi = 118.2°\)) and the gap widens to 98.1% versus 3.68%. Click b along v₁ (major) (\(\varphi = 28.2°\)) and it flips to 8.67% versus 12.5%. Along the major axis an 8 m bias is barely \(1.4\,\sigma\), so neither test can see it, and the Euclidean circle, which cuts inside the ellipse there, is marginally better. At \(\varphi = 0°\) the figures are 42.6% and 11.1%. The \(P_D\)-vs-direction plot shows the whole story at once: the D² curve swings from about 9% at the major axis to 98% at the minor axis, tracking how surprising the bias is, while the Euclidean curve stays flat between about 4% and 13%.

  11. Shrink the bias. At \(|\mathbf{b}| = 4\) m, the minor-axis direction still gives the D² test 36.0% against the Euclidean test’s 1.36%. Along the major axis both are near the floor, at 2.37% and 3.19%.

  12. Reseed. The counts change only at the Monte Carlo noise level, to 2 015 and 1 988 healthy alarms and \(P_D\) = 93.9% and 6.35%. The cards use all 200 000 draws. The direction curve uses the first 50 000 at each of 61 directions, the same draws in every direction, which keeps the curve smooth.

Key observations#

  • Same length, different surprise. \(|\boldsymbol{\nu}|\) measures residual size in meters. \(D\) measures it in units of the filter’s own predicted spread in that direction. The twins have identical \(|\boldsymbol{\nu}|\) and \(D^2\) values that differ by a factor of \(\lambda_1/\lambda_2\).

  • Equal \(P_{\text{FA}}\) does not mean equal tests. The ellipse and the circle each enclose 99% of healthy residuals, and the Monte Carlo confirms that both false-alarm at about 1%. They spend that 1% in different places. The ellipse rejects exactly the least likely 1% of healthy residuals, and once whitened that 1% is spread evenly over every direction. The circle spends its whole 1% at the ends of the major axis, where healthy residuals routinely go and it alarms at only about \(2.6\,\sigma\). Along the minor axis it is left blind, tolerating residuals out to about \(9\,\sigma\).

  • The D² test is uniformly sensible; the Euclidean test is direction-blind. \(D^2\) is the generalized likelihood-ratio statistic for a mean shift of unknown size and direction: maximizing the Gaussian likelihood over \(\mathbf{b}\) gives \(2\ln\Lambda = \boldsymbol{\nu}^{\top}\mathbf{S}^{-1}\boldsymbol{\nu}\). Under a bias it is noncentral \(\chi^2_2\) with noncentrality \(\mathbf{b}^{\top}\mathbf{S}^{-1}\mathbf{b}\), so its \(P_D\) depends only on that number and rises monotonically with it. The more improbable a bias is under the healthy model, the more likely the D² test is to catch it, whatever its direction. The Euclidean test ignores direction. At the defaults (\(|\mathbf{b}| = 8\) m with the tilted \(\mathbf{S}\)), its \(P_D\) curve is nearly flat and low. It peaks along the major axis, the direction in which a bias is least surprising. That shape holds for this setting, not in general. Once \(|\mathbf{b}|\) exceeds \(r_E\), the bias alone carries residuals past the circle, and the Euclidean curve climbs in every direction.

  • Whitening turns every case into the easy case. After \(\mathbf{u} = \mathbf{S}^{-1/2}\boldsymbol{\nu}\) the healthy distribution is circular, and “distance from the origin” means the same thing in every direction. The Mahalanobis test is just the Euclidean test applied in the right coordinates. With uncorrelated, equal variances the two tests agree up to the scale factor \(\sigma\).

  • The scalar test is the same idea. The per-pseudorange \(D^2 = \nu^2/S\) in the Fault Detection demo is the \(1 \times 1\) case, where “direction” collapses to a sign. Stacking \(m\) innovations with their joint covariance gives \(\chi^2(m)\); that is how multi-satellite batch tests in the reading get their thresholds. The windowed \(\sum D^2_k \sim \chi^2(M)\) test is the time-stacked version. It needs one more ingredient: a consistent filter’s innovations are white, meaning independent from one time step to the next, so the cross-time blocks of the joint covariance vanish.

Source#

MATLAB bundle · demos/MahalanobisDistanceDemo.m↓

The MATLAB companion recomputes the default case: \(D^2\) for the free point and the twins, \(\gamma\), \(r_E\), the healthy false-alarm rates, the faulted detection rates, and the detection-vs-direction curve, with the exact noncentral-\(\chi^2\) curve for the D² test overlaid. It draws four static figures: the residual plane, the whitened plane, the per-axis decomposition, and \(P_D\) versus direction. The ellipses and \(D\) contours use navutils/Draw2DErrorBounds.m, called with \(\alpha = P_{\text{FA}}\) for the threshold and \(\alpha = e^{-D^2/2}\) for each contour. MATLAB’s random stream differs from the browser’s seeded generator, so the Monte Carlo rates agree with the demo to within sampling noise rather than draw for draw. All deterministic quantities agree exactly.