TExcelTAD
Home › Math for Machine Learning › Inner Products

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 ⟨⋅,⋅⟩\langle \cdot, \cdot \rangle on a real vector space is a map V×V→RV \times V \to \mathbb{R} that is

⟨λx+ψy,z⟩=λ⟨x,z⟩+ψ⟨y,z⟩linear in each argument⟨x,y⟩=⟨y,x⟩symmetric⟨x,x⟩>0 for x≠0positive definite\begin{aligned} &\langle \lambda\mathbf{x} + \psi\mathbf{y}, \mathbf{z} \rangle = \lambda\langle \mathbf{x}, \mathbf{z} \rangle + \psi\langle \mathbf{y}, \mathbf{z} \rangle && \text{linear in each argument} \\ &\langle \mathbf{x}, \mathbf{y} \rangle = \langle \mathbf{y}, \mathbf{x} \rangle && \text{symmetric} \\ &\langle \mathbf{x}, \mathbf{x} \rangle > 0 \text{ for } \mathbf{x} \ne \mathbf{0} && \text{positive definite} \end{aligned}

The standard choice on Rn\mathbb{R}^n is the dot product xTy\mathbf{x}^T\mathbf{y}. Every inner product gives a norm ∥x∥=⟨x,x⟩\lVert \mathbf{x} \rVert = \sqrt{\langle \mathbf{x}, \mathbf{x} \rangle}.

Symmetric positive definite matrices

On Rn\mathbb{R}^n, every inner product has the form

⟨x,y⟩A=xTAy\langle \mathbf{x}, \mathbf{y} \rangle_A = \mathbf{x}^T A\mathbf{y}

for a symmetric positive definite (SPD) matrix AA: A=ATA = A^T and xTAx>0\mathbf{x}^T A\mathbf{x} > 0 for all x≠0\mathbf{x} \ne \mathbf{0}. (Use the basis vectors: aij=⟨ei,ej⟩a_{ij} = \langle \mathbf{e}_i, \mathbf{e}_j \rangle.) SPD matrices are everywhere in ML: covariance matrices (when no feature is a combination of others), kernel matrices, Hessians at a strict minimum, and XTXX^T X for full-rank data.

python · try it yourself
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 ⟨⋅,⋅⟩A\langle \cdot, \cdot \rangle_A 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, ∣⟨x,y⟩∣≤∥x∥ ∥y∥\lvert \langle \mathbf{x}, \mathbf{y} \rangle \rvert \le \lVert \mathbf{x} \rVert\,\lVert \mathbf{y} \rVert, with equality only when one vector is a multiple of the other. This guarantees that ⟨x,y⟩/(∥x∥∥y∥)\langle \mathbf{x}, \mathbf{y} \rangle / (\lVert \mathbf{x} \rVert\lVert \mathbf{y} \rVert) lies in [−1,1][-1, 1], so it can be the cosine of an angle (next lesson). It is also why the correlation coefficient is between −1-1 and 11.

The Mahalanobis distance

If data has covariance Σ\Sigma, the distance (x−y)TΣ−1(x−y)\sqrt{(\mathbf{x} - \mathbf{y})^T\Sigma^{-1}(\mathbf{x} - \mathbf{y})} comes from the inner product with A=Σ−1A = \Sigma^{-1}. 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.

python · try it yourself
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 (3,−3)(3, -3) goes against the correlation and is far more unusual.

In software

  • Anomaly detectors flag points with a large Mahalanobis distance; scipy.spatial.distance.mahalanobis computes 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: qTWk\mathbf{q}^T W\mathbf{k}.

Exercises

Exercise 1: Is it an inner product?

Does ⟨x,y⟩=x1y1−x2y2\langle \mathbf{x}, \mathbf{y} \rangle = x_1y_1 - x_2y_2 define an inner product on R2\mathbb{R}^2?

Answer

No, it is not positive definite.

Solution

For x=(0,1)\mathbf{x} = (0, 1) we get ⟨x,x⟩=−1<0\langle \mathbf{x}, \mathbf{x} \rangle = -1 < 0. Its matrix diag⁡(1,−1)\operatorname{diag}(1, -1) has a negative eigenvalue.

Exercise 2: Length in a new geometry

With A=[2112]A = \begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix}, compute ∥(1,−1)∥A\lVert (1, -1) \rVert_A and ∥(1,1)∥A\lVert (1, 1) \rVert_A.

Answer

2\sqrt{2} and 6\sqrt{6}.

Solution
(1,−1) A (1,−1)T=2−1−1+2=2(1,1) A (1,1)T=2+1+1+2=6\begin{aligned} (1, -1)\,A\,(1, -1)^T &= 2 - 1 - 1 + 2 = 2 \\ (1, 1)\,A\,(1, 1)^T &= 2 + 1 + 1 + 2 = 6 \end{aligned}

Vectors of equal Euclidean length get different lengths under AA.

Exercise 3: Check SPD (code)

Is [1221]\begin{bmatrix} 1 & 2 \\ 2 & 1 \end{bmatrix} positive definite? Use its eigenvalues and a test vector.

Answer

No: eigenvalues −1-1 and 33; (1,−1)(1, -1) gives xTAx=−2\mathbf{x}^T A\mathbf{x} = -2.

Solution

A symmetric matrix is positive definite exactly when all its eigenvalues are positive.

python · try it yourself
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 Rn\mathbb{R}^n can be written as xTAy\mathbf{x}^T A\mathbf{y} with AA…

  1. diagonal
  2. symmetric positive definite
  3. orthogonal
  4. upper triangular

Cauchy-Schwarz says…

  1. ∥x+y∥=∥x∥+∥y∥\lVert \mathbf{x} + \mathbf{y} \rVert = \lVert \mathbf{x} \rVert + \lVert \mathbf{y} \rVert
  2. ∣⟨x,y⟩∣≤∥x∥ ∥y∥\lvert \langle \mathbf{x}, \mathbf{y} \rangle \rvert \le \lVert \mathbf{x} \rVert\,\lVert \mathbf{y} \rVert
  3. ⟨x,y⟩=0\langle \mathbf{x}, \mathbf{y} \rangle = 0 always
  4. ∥x∥≤1\lVert \mathbf{x} \rVert \le 1

The Mahalanobis distance uses the inner product matrix…

  1. II
  2. Σ\Sigma
  3. Σ−1\Sigma^{-1}
  4. XTX^T