Inner Products
The dot product is one example of an inner product, a rule that multiplies two vectors into a number and gives us lengths, angles and orthogonality. Changing the inner product changes the geometry: a different inner product can make correlated features look "round", which is what the Mahalanobis distance and whitening do.
Definition
An inner product on a real vector space is a map that is
The standard choice on is the dot product . Every inner product gives a norm .
Symmetric positive definite matrices
On , every inner product has the form
for a symmetric positive definite (SPD) matrix : and for all . (Use the basis vectors: .) SPD matrices are everywhere in ML: covariance matrices (when no feature is a combination of others), kernel matrices, Hessians at a strict minimum, and for full-rank data.
import numpy as np
A = np.array([[2.0, 1.0],
[1.0, 2.0]])
print("symmetric:", np.allclose(A, A.T), " eigenvalues:", np.linalg.eigvalsh(A))
rng = np.random.default_rng(0)
xs = rng.normal(size=(5, 2))
print("x^T A x > 0 for random x:", all(x @ A @ x > 0 for x in xs))
x, y = np.array([1.0, 0.0]), np.array([0.0, 1.0])
print("<e1, e2> standard:", x @ y, " <e1, e2>_A:", x @ A @ y)symmetric: True eigenvalues: [1. 3.]
x^T A x > 0 for random x: True
<e1, e2> standard: 0.0 <e1, e2>_A: 1.0
Under the standard basis vectors are no longer orthogonal. Orthogonality depends on the inner product, not just on the vectors.
Cauchy-Schwarz
For any inner product, , with equality only when one vector is a multiple of the other. This guarantees that lies in , so it can be the cosine of an angle (next lesson). It is also why the correlation coefficient is between and .
The Mahalanobis distance
If data has covariance , the distance comes from the inner product with . It counts distance in "standard deviations along each direction", so a point far out along a direction where the data is naturally spread is not flagged as unusual.
import numpy as np
rng = np.random.default_rng(1)
Sigma = np.array([[4.0, 3.0], [3.0, 4.0]]) # strongly correlated features
X = rng.multivariate_normal([0, 0], Sigma, size=2000)
S_inv = np.linalg.inv(np.cov(X.T))
for p in (np.array([3.0, 3.0]), np.array([3.0, -3.0])):
eu = np.linalg.norm(p)
ma = np.sqrt(p @ S_inv @ p)
print(p, f"Euclidean {eu:.3f} Mahalanobis {ma:.3f}")[3. 3.] Euclidean 4.243 Mahalanobis 1.603
[ 3. -3.] Euclidean 4.243 Mahalanobis 4.220
Both points are equally far from the centre in Euclidean terms, but goes against the correlation and is far more unusual.
In software
- Anomaly detectors flag points with a large Mahalanobis distance;
scipy.spatial.distance.mahalanobiscomputes it. - Kernel methods (The Dual SVM and Kernels) replace the dot product with an inner product in a feature space.
- Gaussian process and attention scores are inner products, sometimes through a learned matrix: .
Exercises
Exercise 1: Is it an inner product?
Does define an inner product on ?
Answer
No, it is not positive definite.
Solution
For we get . Its matrix has a negative eigenvalue.
Exercise 2: Length in a new geometry
With , compute and .
Answer
and .
Solution
Vectors of equal Euclidean length get different lengths under .
Exercise 3: Check SPD (code)
Is positive definite? Use its eigenvalues and a test vector.
Answer
No: eigenvalues and ; gives .
Solution
A symmetric matrix is positive definite exactly when all its eigenvalues are positive.
import numpy as np
A = np.array([[1.0, 2.0], [2.0, 1.0]])
x = np.array([1.0, -1.0])
print(np.linalg.eigvalsh(A), x @ A @ x)[-1. 3.] -2.0
Test yourself
Every inner product on can be written as with …
- diagonal
- symmetric positive definite
- orthogonal
- upper triangular
Cauchy-Schwarz says…
- always
The Mahalanobis distance uses the inner product matrix…