Demo: Covariance Ellipse Explorer#

Every 2D error ellipse in this course (the red dashed ellipse riding on the 4-state filter’s estimate, the GPS fix ellipse, the fault-detection plots) is drawn from a single \(2 \times 2\) block of \(\mathbf{P}\). This demo takes that block apart. You set \(\sigma_x\), \(\sigma_y\) and the correlation \(\rho\) (or, equivalently, the principal axes and their rotation), and watch the 2D Gaussian, its constant-probability contours, the principal axes and a seeded Monte Carlo cloud respond together. The point to leave with: the eigen-decomposition of \(\mathbf{P}\) is the ellipse. Eigenvectors are the axis directions, square roots of eigenvalues are the \(1\sigma\) semi-axes, and the confidence level only scales the picture.

The geometry of a 2×2 covariance#

A \(2 \times 2\) position covariance and its eigen-decomposition:

\[\begin{split} \mathbf{P} = \begin{bmatrix} \sigma_x^2 & \rho\,\sigma_x\sigma_y \\ \rho\,\sigma_x\sigma_y & \sigma_y^2 \end{bmatrix} = \mathbf{V}\boldsymbol{\Lambda}\mathbf{V}^\top, \qquad \mathbf{V} = \begin{bmatrix} \mathbf{v}_1 & \mathbf{v}_2 \end{bmatrix}, \quad \boldsymbol{\Lambda} = \begin{bmatrix} \lambda_1 & 0 \\ 0 & \lambda_2 \end{bmatrix}, \quad \lambda_1 \ge \lambda_2 > 0. \end{split}\]

For a symmetric \(2\times2\) matrix the decomposition has a closed form:

\[ \lambda_{1,2} = \frac{\sigma_x^2 + \sigma_y^2}{2} \pm \sqrt{\left(\frac{\sigma_x^2 - \sigma_y^2}{2}\right)^2 + \left(\rho\,\sigma_x\sigma_y\right)^2}, \qquad \theta = \operatorname{atan2}(v_{1y}, v_{1x}) = \tfrac{1}{2}\operatorname{atan2}\!\left(2\rho\,\sigma_x\sigma_y,\ \sigma_x^2 - \sigma_y^2\right). \]

The error ellipse at confidence \(P_\text{conf}\) is the set of points with the same Mahalanobis distance from the mean:

\[ \left\{\mathbf{x} : \mathbf{x}^\top \mathbf{P}^{-1}\mathbf{x} = s\right\}, \qquad s = -2\ln\!\left(1 - P_\text{conf}\right). \]

The scale \(s\) comes from the fact that \(d^2 = \mathbf{x}^\top \mathbf{P}^{-1}\mathbf{x}\) is \(\chi^2\)-distributed with 2 degrees of freedom, whose CDF is simply \(1 - e^{-s/2}\). The ellipse’s semi-axes lie along \(\mathbf{v}_1\) and \(\mathbf{v}_2\) with lengths

\[ a = \sqrt{s\,\lambda_1}, \qquad b = \sqrt{s\,\lambda_2}, \qquad \det\mathbf{P} = \lambda_1\lambda_2 = \sigma_x^2\sigma_y^2(1-\rho^2), \qquad \text{area} = \pi a b = \pi s\sqrt{\det\mathbf{P}}. \]

This is exactly the convention of navutils/Draw2DErrorBounds.m: the major-axis eigenvector sets the rotation and the semi-axes are \(\sqrt{\chi^2_{2}(P_\text{conf})\cdot\lambda}\). The “\(k\sigma\) ellipse” means \(s = k^2\), so it holds \(1 - e^{-k^2/2}\) of the probability, far less than the 1D \(\pm k\sigma\) interval:

Ellipse

\(P_\text{conf}\)

\(s\)

\(\sqrt{s}\)

1D multiplier for the same probability

\(1\sigma\)

39.3%

1

1

0.515

50%

50%

1.386

1.177

0.674

\(2\sigma\)

86.5%

4

2

1.493

90%

90%

4.605

2.146

1.645

95%

95%

5.991

2.448

1.960

\(3\sigma\)

98.9%

9

3

2.539

99%

99%

9.210

3.035

2.576

Interactive demo#

Open in full screen

The main plane shows the 2D Gaussian density as a blue heat map (normalized to its peak, so the \(P_\text{conf}\) ellipse is the contour at \(1 - P_\text{conf}\) of the peak), the selected-confidence ellipse in red, faint dashed \(1\sigma\) / \(2\sigma\) / \(3\sigma\) reference contours, and the principal axes as arrows \(a\,\mathbf{v}_1\) (purple) and \(b\,\mathbf{v}_2\) (green). The amber dashed lines are the marginal shadows \(x = \pm\sigma_x\) and \(y = \pm\sigma_y\), with their tangent points on the \(1\sigma\) ellipse marked; the strips above and to the right of the plane show the 1D marginal densities they come from. Navy dots are Monte Carlo samples inside the selected ellipse, orange dots are outside. The readout card shows \(\mathbf{P}\), \(\mathbf{V}\) and \(\boldsymbol{\Lambda}\) as matrices plus every derived quantity, and the three cards below it carry the “2D is not 1D” comparison, the Monte Carlo containment, and the axis multiplier \(\sqrt{s}\).

Set P by switches between the two parameterizations, \((\sigma_x, \sigma_y, \rho)\) and \((\sqrt{\lambda_1}, \sqrt{\lambda_2}, \theta)\). Both drive the same \(\mathbf{P}\), and switching keeps it exactly. You can also drag the tips of the two arrows on the plot. The view auto-zooms to fit the largest selectable ellipse (99%), so changing the confidence level never rescales the axes.

Walkthrough#

The demo opens at \(\sigma_x = 3\) m, \(\sigma_y = 2\) m, \(\rho = 0.5\), the 95% ellipse, \(N = 1000\) samples with seed 1, and a \(\pm 10\) m view. Try the following:

  1. Read the default decomposition. The readout shows \(\mathbf{P} = \begin{bmatrix} 9 & 3 \\ 3 & 4 \end{bmatrix}\) m², eigenvalues \(\lambda_1 = 10.41\) and \(\lambda_2 = 2.595\) m², major axis \(\mathbf{v}_1 = (0.906, 0.424)\) and \(\theta = 25.1°\). At 95% (\(s = 5.991\)) the semi-axes are \(a = 7.896\) m and \(b = 3.943\) m, with \(\det\mathbf{P} = 27.00\) m⁴, area 97.81 m² and DRMS \(= \sqrt{\operatorname{tr}\mathbf{P}} = 3.606\) m. The check line confirms that \(\mathbf{V}\boldsymbol{\Lambda}\mathbf{V}^\top\) rebuilds \(\mathbf{P}\) to about \(10^{-16}\).

  2. Find \(\sigma_x\) on the plot. The amber dashed lines at \(x = \pm 3\) m and \(y = \pm 2\) m box in the \(1\sigma\) ellipse. The vertical lines touch it at \((\pm 3.0, \pm 1.0)\) m, which is \((\pm\sigma_x, \pm\rho\sigma_y)\), and the horizontal lines touch it at \((\pm 1.5, \pm 2.0)\) m. None of these points is the end of an axis. The \(1\sigma\) semi-axes are \(\sqrt{\lambda_1}, \sqrt{\lambda_2} = 3.226, 1.611\) m, so with \(\rho \ne 0\) \(\sigma_x\) is the ellipse’s shadow on the \(x\) axis, not a semi-axis. The top strip shows that shadow as a 1D Gaussian whose \(\pm\sigma_x\) band holds 68.3%.

  3. Remove the correlation. Click uncorrelated, σx ≫ σy (\(\sigma_x = 6\), \(\sigma_y = 1.5\), \(\rho = 0\)). \(\mathbf{P}\) is diagonal, \(\mathbf{V} = \mathbf{I}\), \(\theta = 0.0°\), and the eigenvalues are just \(\sigma_x^2 = 36.00\) and \(\sigma_y^2 = 2.250\) m². The ellipse is axis-aligned, the shadows now touch it at the axis ends, and \(a = 14.69\) m, \(b = 3.672\) m. The view zooms out to \(\pm 20\) m. Now click uncorrelated, equal (\(\sigma_x = \sigma_y = 3\) m): \(\lambda_1 = \lambda_2 = 9.000\) m², and the readout reports \(\theta\) as “any (circle) → 0.0°”, not as an undefined angle. Nudge \(\rho\) to \(+0.01\) and \(\theta\) snaps to \(45.0°\). At \(-0.01\) it snaps to \(-45.0°\). Near a circle the eigenvectors are ill-conditioned, even though the ellipse barely changes.

  4. Add strong correlation. Click strong positive ρ (\(\sigma_x = 4\), \(\sigma_y = 3\), \(\rho = 0.9\)). The ellipse tilts to \(\theta = 36.0°\) and thins to \(\sqrt{\lambda_1}, \sqrt{\lambda_2} = 4.884, 1.071\) m. \(\det\mathbf{P} = 27.36\) m⁴, against \(144\) m⁴ for the same \(\sigma\)s with \(\rho = 0\), because \(\det\mathbf{P} = \sigma_x^2\sigma_y^2(1-\rho^2)\).

  5. Approach singularity. Click near-singular (ρ = 0.99). \(\lambda_2\) falls to \(0.1152\) m² (\(b = 0.8306\) m against \(a = 12.21\) m), \(\det\mathbf{P} = 2.866\) m⁴, the area drops to 31.86 m², and the plot flags near-singular: b/a = 0.068. The ellipse has collapsed toward a line along \(\mathbf{v}_1\). Drag \(\rho\) to \(-0.99\): the eigenvalues are unchanged and the line flips to \(\theta = -36.8°\).

  6. Change only the confidence. Press Reset, then step through the confidence chips. At 1σ (39.3%), \(s = 1.000\) and \(a = 3.226\) m \(= \sqrt{\lambda_1}\). At 2σ (86.5%), \(s = 4.000\) and \(a = 6.451\) m. At 99%, \(s = 9.210\), \(a = 9.790\) m and \(b = 4.889\) m. Throughout, \(\theta\) stays at \(25.1°\), the b/a row stays at \(0.4994\) and the axes do not rescale: the ellipse grows by \(\sqrt{s}\) but keeps its shape.

  7. Switch to the principal-axis parameterization. Click √λ1, √λ2, θ. The sliders read \(3.23\) m, \(1.61\) m and \(25.1°\), and every readout is unchanged because it is the same \(\mathbf{P}\). Slide \(\theta\) to \(90°\): \(\mathbf{P}\) becomes \(\operatorname{diag}(2.595, 10.41)\) m², \(\sigma_x, \sigma_y = 1.611, 3.226\) m and \(\rho = 0.000\). Slide it to \(-25°\) and \(\rho\) becomes \(-0.499\). You can also drag the purple arrow tip, which sets \(\sqrt{\lambda_1}\) and \(\theta\), or the green tip, which sets \(\sqrt{\lambda_2}\).

  8. Check containment with Monte Carlo. At the defaults, 940 / 1000 samples (94.0%) fall inside the 95% ellipse, against a nominal \(95.0\% \pm 0.7\%\) binomial \(1\sigma\). With \(N = 200\) the count is 188 / 200 (94.0%, against \(95.0\% \pm 1.5\%\)), and with \(N = 5000\) it is 4756 / 5000 (95.1%, against \(95.0\% \pm 0.3\%\)). Re-seed at \(N = 1000\) gives 941 and then 938. The 2D is not 1D card shows the same samples two ways: 39.3% lie inside the \(1\sigma\) ellipse, while 67.1% satisfy \(|x| \le \sigma_x\).

Key observations#

  • \(\mathbf{P} = \mathbf{V}\boldsymbol{\Lambda}\mathbf{V}^\top\) is the ellipse geometry. The columns of \(\mathbf{V}\) point along the axes, \(\sqrt{\lambda_i}\) are the \(1\sigma\) semi-axes, and \(\theta\) is the angle of \(\mathbf{v}_1\). A symmetric \(2\times2\) \(\mathbf{P}\) has three free numbers, and both of the demo’s parameterizations are just two ways of choosing them.

  • Diagonal means axis-aligned; off-diagonal means tilt. With \(\rho = 0\) the eigenvalues are \(\sigma_x^2\) and \(\sigma_y^2\) and the ellipse sits on the axes. A nonzero \(\rho\) tilts it, and its sign decides whether the major axis runs through the first and third quadrants (\(\rho > 0\)) or the second and fourth (\(\rho < 0\)). For fixed \(\sigma_x\) and \(\sigma_y\), increasing \(|\rho|\) thins the ellipse, because \(\det\mathbf{P} = \sigma_x^2\sigma_y^2(1-\rho^2)\) shrinks while the trace stays put. Overall thinness is \(b/a = \sqrt{\lambda_2/\lambda_1}\), which also depends on \(\sigma_x/\sigma_y\): the uncorrelated, σx ≫ σy preset has \(\rho = 0\) and \(b/a = 0.25\).

  • \(\sigma_x\) and \(\sigma_y\) are projections, not semi-axes. The \(1\sigma\) ellipse is tangent to \(x = \pm\sigma_x\) at \((\pm\sigma_x, \pm\rho\sigma_y)\). The eigenvalues always bracket the diagonal, \(\lambda_1 \ge \max(\sigma_x^2, \sigma_y^2)\) and \(\lambda_2 \le \min(\sigma_x^2, \sigma_y^2)\). Reading \(\pm\sigma_x\) off a filter’s \(\mathbf{P}\) diagonal therefore always understates the worst-direction error when states are correlated.

  • \(\rho \to \pm 1\) means \(\det\mathbf{P} \to 0\). One combination of \(x\) and \(y\) (along \(\mathbf{v}_2\)) becomes almost perfectly known, \(\lambda_2 \to 0\), the ellipse becomes a line and \(\mathbf{P}^{-1}\) blows up. A filter covariance that drifts toward singular is a numerical warning sign, and positive-definiteness is the property that must not be lost.

  • Confidence is a pure scale. Every confidence level gives the same \(\mathbf{V}\) and the same \(b/a\). Only \(a\) and \(b\) scale by \(\sqrt{s}\), and the area by \(s\).

  • 2D is not 1D. The \(1\sigma\) ellipse holds only 39.3%, and a 95% ellipse needs \(2.448\sigma\) along each principal axis, not \(1.960\sigma\). That is why the 4-state filter demo uses 2.4477 for its ellipse but 1.96 for its per-axis error bounds.

  • The Monte Carlo count does not depend on \(\mathbf{P}\). Samples are \(\mathbf{x} = \mathbf{P}^{1/2}\mathbf{z}\) with fixed seeded \(\mathbf{z}\), and \(\mathbf{x}^\top\mathbf{P}^{-1}\mathbf{x} = \mathbf{z}^\top\mathbf{z}\). Every Gaussian is a whitened standard normal in disguise. Moving the \(\mathbf{P}\) sliders deforms the cloud but never changes which samples are inside the ellipses. Only \(N\) and the seed change those counts, within the binomial noise \(\sqrt{P_\text{conf}(1-P_\text{conf})/N}\). This applies to the ellipse counts only. The \(|x| \le \sigma_x\) share on the 2D is not 1D card is a box test, and \(x/\sigma_x\) is a different combination of the fixed \(\mathbf{z}\) for each \(\mathbf{P}\), so that share moves with \(\mathbf{P}\) (66.7% to 68.1% across the presets at the default seed).

  • DRMS ignores orientation. \(\sqrt{\operatorname{tr}\mathbf{P}} = \sqrt{\lambda_1 + \lambda_2}\) is the same in either parameterization and under any rotation. That makes it a convenient single-number accuracy summary, but it cannot tell a round error distribution from a long, thin one.

Source#

MATLAB bundle · demos/CovarianceEllipseDemo.m↓

The MATLAB companion reproduces the default figure (density contours, the \(1\sigma\)/\(2\sigma\)/\(3\sigma\) and 95% ellipses, principal axes, shadows and a Monte Carlo cloud), prints the eigen readout, and tabulates \(s\), \(a\), \(b\) and Monte Carlo containment for every confidence level. It uses the 2D covariance ellipse helper in navutils/Draw2DErrorBounds.m. Its random draws come from MATLAB’s rng(21) and randn, not the demo’s seeded generator, so its Monte Carlo counts differ slightly from the demo’s (948 / 1000 inside the 95% ellipse) while the geometry matches exactly.