SVD,Eigendecomposition and Mahalanobis (manobala) Distance

6 July 2026

Need for Mahalanobis distance

Distance calculated using euclidean geometry provides the absolute value between an origin and the target, yet this quantity lacks on stating how unique/unremarkable a particular point is in a data cluster. Standard deviation of a probability distibution comments on how the variables are spread. Mahalanobis distance is that quantity that tells the measure of a point from the mean in terms of standard deviation. This is particularly required in point clouds because this helps in determining outliers/noise while performing Iterative Closing Point Algorithm.

Formulas to get familiar with

  • The Mahalanobis distance is calculated as:
dM(x)=(xμ)Σ1(xμ)d_M (x) = \sqrt{(x - \mu)^\top \Sigma^{-1}(x - \mu)}
  • Setting e=xμe = x - \mu (the deviation from the mean):
dM2=eΣ1e=eΣ2d_M^2 = e^\top \Sigma^{-1} e = \Vert e \Vert_\Sigma^2
  • Σ\Sigma is the d×dd \times d covariance matrix
Σd×d=1n1i=1n(xixˉ)(xixˉ)\Sigma_{d \times d} = \frac{1}{n - 1} \sum_{i=1}^{n} (\mathbf{x}_i - \bar{\mathbf{x}})(\mathbf{x}_i - \bar{\mathbf{x}})^\top
  • Let d=2d=2, so each observation is a 2D vector xi=[xi1xi2]\mathbf{x}_i = \begin{bmatrix}x_{i1}\\ x_{i2}\end{bmatrix} and the mean is xˉ=[xˉ1xˉ2]\bar{\mathbf{x}} = \begin{bmatrix}\bar{x}_1\\ \bar{x}_2\end{bmatrix}. The deviation vector for observation ii
xixˉ=[xi1xˉ1xi2xˉ2]\mathbf{x}_i - \bar{\mathbf{x}} = \begin{bmatrix}x_{i1} - \bar{x}_1\\ x_{i2} - \bar{x}_2\end{bmatrix}
  • The outer product is (xixˉ)(xixˉ)(\mathbf{x}_i - \bar{\mathbf{x}})(\mathbf{x}_i - \bar{\mathbf{x}})^\top. This is a column vector times a row vector — giving a 2×22\times2 matrix:
(xixˉ)(xixˉ)=[xi1xˉ1xi2xˉ2][xi1xˉ1xi2xˉ2](\mathbf{x}_i - \bar{\mathbf{x}})(\mathbf{x}_i - \bar{\mathbf{x}})^\top = \begin{bmatrix}x_{i1}-\bar{x}_1\\ x_{i2}-\bar{x}_2\end{bmatrix}\begin{bmatrix}x_{i1}-\bar{x}_1 & x_{i2}-\bar{x}_2\end{bmatrix} =[(xi1xˉ1)2(xi1xˉ1)(xi2xˉ2)(xi2xˉ2)(xi1xˉ1)(xi2xˉ2)2]= \begin{bmatrix}(x_{i1}-\bar{x}_1)^2 & (x_{i1}-\bar{x}_1)(x_{i2}-\bar{x}_2)\\ (x_{i2}-\bar{x}_2)(x_{i1}-\bar{x}_1) & (x_{i2}-\bar{x}_2)^2\end{bmatrix}
note: d is number of features(columns) and n is number of observations (rows)
  • Sum over all nn observations
i=1n(xixˉ)(xixˉ)=[i(xi1xˉ1)2i(xi1xˉ1)(xi2xˉ2)i(xi2xˉ2)(xi1xˉ1)i(xi2xˉ2)2]\sum_{i=1}^{n}(\mathbf{x}_i - \bar{\mathbf{x}})(\mathbf{x}_i - \bar{\mathbf{x}})^\top = \begin{bmatrix}\sum_i(x_{i1}-\bar{x}_1)^2 & \sum_i(x_{i1}-\bar{x}_1)(x_{i2}-\bar{x}_2)\\ \sum_i(x_{i2}-\bar{x}_2)(x_{i1}-\bar{x}_1) & \sum_i(x_{i2}-\bar{x}_2)^2\end{bmatrix}
  • Divide by n1n-1
Σ2×2=1n1[i(xi1xˉ1)2i(xi1xˉ1)(xi2xˉ2)i(xi2xˉ2)(xi1xˉ1)i(xi2xˉ2)2]\Sigma_{2\times2} = \frac{1}{n-1}\begin{bmatrix}\sum_i(x_{i1}-\bar{x}_1)^2 & \sum_i(x_{i1}-\bar{x}_1)(x_{i2}-\bar{x}_2)\\ \sum_i(x_{i2}-\bar{x}_2)(x_{i1}-\bar{x}_1) & \sum_i(x_{i2}-\bar{x}_2)^2\end{bmatrix}

Reading each entry

Σ2×2=[Var(x1)Cov(x1,x2)Cov(x2,x1)Var(x2)]\Sigma_{2\times2} = \begin{bmatrix}\text{Var}(x_1) & \text{Cov}(x_1,x_2)\\ \text{Cov}(x_2,x_1) & \text{Var}(x_2)\end{bmatrix}
  • (1,1)(1,1): 1n1i(xi1xˉ1)2\frac{1}{n-1}\sum_i(x_{i1}-\bar{x}_1)^2 — variance of feature 1. Same feature squared with itself.
  • (1,2)(1,2): 1n1i(xi1xˉ1)(xi2xˉ2)\frac{1}{n-1}\sum_i(x_{i1}-\bar{x}_1)(x_{i2}-\bar{x}_2) — covariance of feature 1 and 2. Two different features multiplied.
  • (2,1)(2,1): same as (1,2)(1,2) — always symmetric since multiplication is commutative.
  • (2,2)(2,2): 1n1i(xi2xˉ2)2\frac{1}{n-1}\sum_i(x_{i2}-\bar{x}_2)^2 — variance of feature 2.

Connection to 1n1XcXc\frac{1}{n-1}X_c^\top X_c (centered data)

The formula 1n1i(xixˉ)(xixˉ)\frac{1}{n-1}\sum_i(\mathbf{x}_i - \bar{\mathbf{x}})(\mathbf{x}_i - \bar{\mathbf{x}})^\top and 1n1XcXc\frac{1}{n-1}X_c^\top X_c are the same thing written two different ways:

  • The summation form adds up nn outer products one observation at a time
  • The matrix form stacks all observations into XcX_c and does one matrix multiplication

Both produce the identical 2×22\times2 covariance matrix — the summation form makes the meaning explicit, the matrix form is compact and fast to compute.

⚠️ Centering requirement: The matrix form only equals the covariance matrix if XcX_c is mean-centered — each row must be (xixˉ)(\mathbf{x}_i - \bar{\mathbf{x}})^\top, so every column sums to zero. The summation form has xˉ-\bar{\mathbf{x}} built into each term; the matrix form must bake it into XcX_c itself.

On raw (uncentered) data, 1n1XX\frac{1}{n-1}X^\top X gives the second moment matrix instead:

1n1XX=Σ+nn1xˉxˉ\frac{1}{n-1}X^\top X = \Sigma + \tfrac{n}{n-1}\,\bar{\mathbf{x}}\bar{\mathbf{x}}^\top

equal to Σ\Sigma only when xˉ=0\bar{\mathbf{x}} = \mathbf{0}.

Centering ≠ normalising. The ladder:

  • Raw XX → second moment matrix
  • Centered XX → covariance matrix Σ\Sigma ← what Mahalanobis needs
  • Centered + scaled by std dev → correlation matrix RR Worked example (1 feature, n=3n=3): let x=[1,2,3]x = [1, 2, 3], so xˉ=2\bar x = 2.

Centered: xc=[1,0,1]x_c = [-1, 0, 1]

1n1xcxc=12[(1)2+02+12]=1\frac{1}{n-1}x_c^\top x_c = \frac{1}{2}\left[(-1)^2+0^2+1^2\right] = 1

This matches Var(x)=1n1i(xixˉ)2=1\text{Var}(x) = \frac{1}{n-1}\sum_i(x_i-\bar x)^2 = 1 exactly. ✓

Raw (uncentered): using x=[1,2,3]x = [1,2,3] directly

1n1xx=12[12+22+32]=142=7\frac{1}{n-1}x^\top x = \frac{1}{2}\left[1^2+2^2+3^2\right] = \frac{14}{2} = 7

7 is not the variance — it’s inflated by the mean not being zero. This is exactly Σ+nn1xˉ2=1+32(2)2=1+6=7\Sigma + \frac{n}{n-1}\bar x^2 = 1 + \frac{3}{2}(2)^2 = 1 + 6 = 7. ✓

Skipping the centering step silently swaps “spread around the mean” for “spread around zero” — the two only agree when xˉ=0\bar x = 0.

Note: The query point xx in dM(x)d_M(x) never needs pre-centering — the (xμ)(x - \mu) inside the formula centers it explicitly. Centering only matters for how Σ\Sigma is computed from the data matrix.

What is covariance, variance and standard deviation? and Ohh my Gaussian Curve

  • σ2\sigma^2 is variance — the average squared deviation of points from the mean; it measures how spread out the distribution is
σ2=1n1i=1n(xixˉ)2\sigma^2 = \frac{1}{n - 1}\sum_{i=1}^{n}(x_i - \bar{x})^2
  • σ\sigma is the standard deviation — the square root of the variance. Taking the root brings the units back to the first power (e.g. meters instead of meters²), same units as the data.

  • cov(x1,x2)\text{cov}(x_1, x_2) covariance tells how two features vary with respect to each other — positive when they move together, negative when they move oppositely, near zero when unrelated

  • variance (σ2\sigma^2) can also be written as

σ2=E(X2)μ2\sigma^2=E(X^2)-\mu^2
  • Where E(X2)E(X^2) is:

    x2f(x)dx\int_{-\infty}^{\infty} x^2 f(x)\, dx

    And f(x)f(x) is the Gaussian density formula:

    1σ2πexp((xμ)22σ2)\frac{1}{\sigma \sqrt{2\pi}} \exp\left(-\frac{(x - \mu)^2}{2\sigma^2}\right)
  • 1σ2π\frac{1}{\sigma \sqrt{2\pi}} is the normalising constant to keep area under curve equal to 1.

  • A Gaussian random variable is written XN(μ,σ2)X \sim N(\mu, \sigma^2) where μ\mu is the mean and σ2\sigma^2 is the variance

  • all Gaussian curves are bell shaped but they can be shifted (by tuning μ\mu) or stretched/squeezed (by tuning σ\sigma)

gaussian curve different size

  • area under all theses curves is 1

  • Probability gives me a confidence/cerainity score of the belief we have on a method/result. A self driving car gets a LiDAR reading of 3.2 m to hit an obstacle, is it safe? It depends entirely on th threshold value of an acceptable uncertanity score. If σ\sigma is less than 0.05m, then it is fine.

So what the f*ck is Singular Value Decomposition?????

Any matrix AA irrespective of its shape; number of independent columns it has (rank); and the numbers present in it, can be decomposed into 3 matrices. That is it!!! It is all that is need to be known to understand SVD apart from the formula.

Now, the formula is made up of 3 matrices which basically rotate, transform, and maybe rotate any input vector again. The 3 stooges of matrices are made of the square roots of the eigen values and eigen vector of ATAA^TA and eigen vector AATAA^T.

A=USVTA=US V^T
  • VV columns are eigen vectors of ATAA^TA
  • SS is the singular values of AA, which are the square roots of the eigen values of ATAA^TA
  • UU matrix consist of columns which are eigenvectors of AATAA^T

NOTE: SVD works only if UU and VV are orthonormal vectors. We will see why.

ATA(vraw)=σ2(vraw) (1)A^TA(v_{raw}) = \sigma^2(v_{raw}) \quad \textbf{--- (1)} (ATAσ2I)vraw=0 (A^TA-\sigma^2 I)v_{raw}=0 vi=vrawvraw (2)v_i = \frac{v_{raw}}{||v_{raw}||} \quad \textbf{--- (2)}

Substituting the normalised input eigen vector from eqn(2)\textbf{(2)} in eqn (1)\textbf{(1)}

ATA(vi)=σ2(vi) (3)A^TA(v_{i}) = \sigma^2(v_{i}) \quad \textbf{--- (3)} Avi2=(Avi)T(Avi)=viTATAvi (4)||Av_i||^2 = (Av_i)^T(Av_i) = v_i^TA^TAv_i \quad \textbf{--- (4)}

Substituting eqn(3)(3) in eqn(4)(4) we get

Avi2=(Avi)T(Avi)=viTATAvi=viTσ2(vi) (5)||Av_i||^2 = (Av_i)^T(Av_i) = v_i^TA^TAv_i = v_i^T\sigma^2(v_{i}) \quad \textbf{--- (5)}

Since viv_i is a unit vector, viTvi=1v_i^Tv_i = 1 (a scalar, not the identity matrix — that only applies when dotting a full orthonormal matrix with its transpose, e.g. VTV=IV^TV = I). Therefore eqn(5)(5) is

Avi=σ (6)||Av_i|| = \sigma \quad \textbf{--- (6)}

we know that the magnitude of AviAv_i from eqn(6)(6)

In eqn(3)(3) multiply both sides by AA

AATA(vi)=σ2(Avi) (7)AA^TA(v_{i}) = \sigma^2(Av_{i}) \quad \textbf{--- (7)}

normalising AviAv_i we get

ui=Aviσi (8)u_i=\frac{Av_i}{\sigma_i} \quad \textbf{--- (8)}

If we stack this relationship horizontally for all nn vector dimensions simultaneously, it maps perfectly to an equivalent matrix-matrix multiplication:

A[v1v2vn]V=[u1u2um]U[σ1σ2]ΣA \underbrace{\begin{bmatrix} \mathbf{v}_1 & \mathbf{v}_2 & \cdots & \mathbf{v}_n \end{bmatrix}}_{V} = \underbrace{\begin{bmatrix} \mathbf{u}_1 & \mathbf{u}_2 & \cdots & \mathbf{u}_m \end{bmatrix}}_{U} \underbrace{\begin{bmatrix} \sigma_1 & & \\ & \sigma_2 & \\ & & \ddots \end{bmatrix}}_{\Sigma}

This simplifies neatly down to:

AV=UΣAV = U\Sigma

Because VV is an orthogonal matrix built from an orthonormal basis of eigenvectors, its inverse is identical to its transpose (V1=VV^{-1} = V^\top). Right-multiplying both sides by VV^\top completely isolates AA:

AVV=UΣVAVV^\top = U\Sigma V^\top A=UΣVA = U\Sigma V^\top

So we have answered why we are using orthonormal eigenvectors and values of ATAA^TA for decomposing the matrix AA with the above derivation.

SingularValueDecompositionSingular \: Value \: Decomposition svd transformation

Why did you even bring up mahalanobis distance and singular value decomposition together?

Look at the formulas of Mahalanobis distance and SVD they look similar. But that isn’t enough, we need to answer if Mahalanobis distance is doing SVD. This intuition is partly right. Mahalanobis distance is prefere because the formula converts an anisotropic distribution (uneven) to isotropic distribution.

From the previous formulas we can infer that covariance matrix is

Σcov=1n1XcTXc\Sigma_{cov} = \frac{1}{n-1}\mathbf{X_c^T}\mathbf{X_c} Xc=USVT\mathbf{X_c} = US V^T

Substituting:

XcTXc=(USV)(USV)=VSUUSV=VS2V\mathbf{X_c^T X_c} = (USV^\top)^\top(USV^\top) = VS^\top U^\top USV^\top = VS^2V^\top

(using UU=IU^\top U = I and SS=S2S^\top S = S^2 since SS is diagonal)

Therefore:

Σcov=1n1VS2V\Sigma_{cov} = \frac{1}{n-1}\mathbf{V S^2 V^\top}

Note the order — it’s VS2VVS^2V^\top, not VS2VV^\top S^2 V. This matters: it tells you VV (not VV^\top) holds the eigenvectors as its columns, matching the standard eigendecomposition convention Σ=VΛV\Sigma = V\Lambda V^\top.

Hence eigenvalues of Σcov\Sigma_{cov}

λi=σi2n1\lambda_i = \frac{\sigma^2_i}{n-1}

where σi\sigma_i are the singular values of Xc\mathbf{X_c}.


Is Mahalanobis distance doing SVD?

Short answer: partly right, partly wrong. Mahalanobis distance uses the same underlying machinery as SVD — rotation by eigenvectors, scaling by eigenvalues — but it is not performing SVD itself. What it’s actually doing is eigendecomposition, applied specifically to the covariance matrix, followed by a distance computation that SVD alone doesn’t give you.

What eigendecomposition actually is

For a symmetric matrix like Σ\Sigma, eigendecomposition is:

Σ=VΛV\Sigma = V \Lambda V^\top
  • VV: columns are eigenvectors of Σ\Sigma (same VV on both sides — this only works because Σ\Sigma is symmetric)
  • Λ\Lambda: diagonal matrix of eigenvalues λi\lambda_i

Compare this to general SVD:

A=UΣAVA = U \Sigma_A V^\top
  • Two different matrices UU and VV — because AA is not necessarily symmetric, so its “natural input direction” and “natural output direction” can differ
  • ΣA\Sigma_A contains singular values σi0\sigma_i \geq 0, not eigenvalues (which can be negative or complex for a non-symmetric AA)

Why they coincide for Σ\Sigma

Since the covariance matrix Σ\Sigma is symmetric and positive semi-definite:

  • Its eigenvalues are guaranteed real and 0\geq 0
  • Its eigenvectors are orthogonal
  • Therefore U=VU = V in its SVD, and its singular values equal its eigenvalues exactly

So for Σ\Sigma specifically, eigendecomposition and SVD collapse into the same operation. That’s the source of the “Mahalanobis is doing SVD” intuition — it isn’t wrong, it’s just that SVD and eigendecomposition happen to be indistinguishable for this one special (symmetric PSD) case.

Building Mahalanobis distance from the eigendecomposition

Start from:

dM2=eΣ1ed_M^2 = e^\top \Sigma^{-1} e

Substitute Σ=VΛVΣ1=VΛ1V\Sigma = V\Lambda V^\top \Rightarrow \Sigma^{-1} = V\Lambda^{-1}V^\top (valid since V1=VV^{-1} = V^\top for orthogonal VV):

dM2=eVΛ1Ve=(Ve)Λ1(Ve)d_M^2 = e^\top V \Lambda^{-1} V^\top e = (V^\top e)^\top \Lambda^{-1} (V^\top e)

Let e~=Ve\tilde{e} = V^\top e — this is ee rotated into the eigenvector basis. Then:

dM2=e~Λ1e~=i=1de~i2λi=i=1d(e~iλi)2=Λ1/2Ve2d_M^2 = \tilde{e}^\top \Lambda^{-1} \tilde{e} = \sum_{i=1}^{d} \frac{\tilde{e}_i^2}{\lambda_i} = \sum_{i=1}^{d}\left(\frac{\tilde{e}_i}{\sqrt{\lambda_i}}\right)^2 = \left\| \Lambda^{-1/2} V^\top e \right\|^2

The actual comparison: SVD pipeline vs Mahalanobis pipeline

StepSVD applied to a vector (AxA\mathbf{x})Mahalanobis distance
1rotate: VxV^\top \mathbf{x}rotate: VeV^\top e
2scale: multiply by σi\sigma_iscale: divide by λi\sqrt{\lambda_i}
3rotate again: U()U(\cdot)no third rotation — take the norm instead
outputa transformed vector y\mathbf{y}a scalar distance dMd_M

Two concrete differences:

  1. SVD stretches; Mahalanobis compresses. SVD’s ΣA\Sigma_A multiplies by σi\sigma_i (stretch). Mahalanobis’s Λ1/2\Lambda^{-1/2} divides by λi\sqrt{\lambda_i} (compress) — it’s undoing the natural spread of the data, not applying it.
  2. SVD ends in a rotation; Mahalanobis ends in a norm. The third SVD step rotates the scaled vector back into output space, giving another vector. Mahalanobis instead takes 2\|\cdot\|^2 at that point, collapsing everything to a single number.

What this operation is actually called

The two-step transform Λ1/2V\Lambda^{-1/2}V^\top (rotate, then scale-down) is called whitening — it takes an anisotropic (unevenly spread, correlated) distribution and turns it into an isotropic (evenly spread in all directions, uncorrelated) one:

e^=Λ1/2VeCov(e^)=I\hat{e} = \Lambda^{-1/2}V^\top e \quad \Rightarrow \quad \text{Cov}(\hat{e}) = I

Once whitened, ordinary Euclidean distance on e^\hat{e} is the Mahalanobis distance on ee — because in the whitened space every direction is equally spread, so there’s no shape left to correct for. That’s the real punchline: Mahalanobis distance = Euclidean distance, computed after first eliminating the covariance.

whitening-transformation

Verdict

Mahalanobis distance is not doing SVD. It is doing:

  1. Eigendecomposition of Σ\Sigma (which happens to coincide with SVD only because Σ\Sigma is symmetric PSD)
  2. A whitening transform (rotate + inverse-scale) built from that eigendecomposition
  3. A final Euclidean norm — a step SVD itself never includes

The formulas look similar because both are built from the same eigenvector/eigenvalue skeleton. But SVD’s job is to decompose a general transformation into a vector output; Mahalanobis’s job is to measure a distance that accounts for the shape of the data, ending in a scalar. Same skeleton, different destinations.

References

Get a detailed understanding here →