Symmetric matrices and quadratic forms
The matrices whose eigenvectors behave perfectly: the spectral theorem, quadratic forms and their ellipses, positive definiteness, why a covariance is always PSD, the Rayleigh quotient and the direction of largest variance.
Why symmetric matrices are special
The topic Determinants and eigenvalues ended on a warning. A general matrix may have too few eigenvectors, or complex ones: a rotation by has eigenvalues , and the shear has a single eigen-direction. Even when an eigenbasis exists, it is usually not orthogonal: the running example of that topic has eigenvectors and at an angle of about .
This topic is about matrices where none of that happens. A square matrix is symmetric if it equals its transpose:
The entries mirror each other across the main diagonal. That sounds like a special case, but in ML symmetric matrices are the rule:
- a covariance matrix, since ;
- the Gram matrix of a data matrix , which already appeared in the normal equations (see the topic Inner products, norms and orthogonality);
- a kernel matrix built from a symmetric similarity ;
- the Hessian of a loss, its matrix of second derivatives (the topic Gradients, Jacobians and Hessians).
Why is symmetric takes one line with the rule :
Nothing here needs to be square: the data matrix is , with one example per row.
The running example of the topic is
Later it will turn out to be the covariance of a tiny dataset.
The spectral theorem
For symmetric matrices the eigenvalue story is as good as it gets.
Spectral theorem. A real symmetric matrix has real eigenvalues and an orthonormal basis of eigenvectors . Put the into the columns of an orthogonal matrix and the eigenvalues into . Then
This is the diagonalization from the previous topic with one upgrade: is now orthogonal, so its inverse is just . Each is the orthogonal projection onto the line through . Geometrically, a symmetric matrix stretches space by along perpendicular axes and does nothing else: no rotation, no shear.
Orthogonality is the easy part to prove. Let and with . Move from one side of the inner product to the other:
So , and . Symmetry was used exactly once, in . A similar trick shows the eigenvalues are real, and an induction handles repeated eigenvalues; we take both on trust.
Now the running example. Its trace is and its determinant , so the characteristic polynomial is . For the matrix sends to zero; for , sends to zero. These two vectors are orthogonal, as promised. Divided by their length , they give
Rotate, stretch, rotate back
Read from right to left. It is three simple moves:
- : the coordinates of in the eigenbasis, . These are plain inner products, with no linear system to solve as for a general .
- : stretch each coordinate by .
- : return to the standard coordinates.
In one formula,
Take in the running example. Its eigen-coordinates are and . Stretching gives , the same as multiplying directly.
The payoff is that functions of act on the eigenvalues only. In the middle cancels, and the same happens for every power:
So exists exactly when no is zero; for the running example it has eigenvalues and along the same . The square root needs , a condition the next blocks make precise.
An ML use is whitening. Let centered features have covariance with all . For any matrix , the covariance of is the average of , that is . Take , which is symmetric: the features have covariance , uncorrelated, each with variance .
Quadratic forms and their level sets
A quadratic form is the number
a polynomial with only degree-two terms. For the running example, : the cross coefficient is split between and .
Any square matrix defines a quadratic form, but only its symmetric part matters. The number equals its own transpose , so . That is why quadratic forms are always written with a symmetric matrix.
What shape is ? Positive coefficients do not decide it: also has them, yet equals at . The spectral theorem does decide it. Switch to eigen-coordinates :
The cross terms are gone. The running example becomes . When all , the level set is an ellipse (an ellipsoid in higher dimensions) with axes along the and semi-axes . Here the level has semi-axis along and along . A large eigenvalue means a steep direction and a short axis.
Mixed signs give a different picture. The matrix has eigenvalues and , so its form is : hyperbolas and a saddle at the origin.
Why ML cares: a quadratic loss is a quadratic form centered at its minimum. Take with all , and let . Put and multiply out; the two cross terms are equal, since is a number and equals its transpose :
Least squares is this in the variable , with and , plus the constant , and solves the normal equations. So its level sets are these ellipses, centered at the minimum. Near a minimum every smooth loss looks the same, with its Hessian in place of . When is large the ellipses are long and thin, and gradient descent zigzags across the valley (the topic Unconstrained optimization).
Positive definite matrices
The sign of the quadratic form sorts symmetric matrices into classes:
- positive definite, : for every ;
- positive semidefinite (PSD), : for every ;
- indefinite: the form takes both signs.
The eigen-coordinates give the test at once. As runs over all nonzero vectors, so does , and stays positive exactly when every is positive:
Eigenvalues are not the cheapest check. Sylvester's criterion says exactly when all its leading principal minors are positive: the determinants of the top-left , , …, blocks. For the running example they are and , so , which agrees with . For they are and : not positive definite.
A trap: for semidefiniteness, non-negative leading minors are not enough. The matrix has leading minors and , yet its form is at .
In code the standard test is the Cholesky factorization with lower triangular and a positive diagonal. It exists exactly when , costs about operations, and libraries report failure when the matrix is not positive definite. Near the boundary rounding decides: a matrix that is positive definite on paper but nearly singular can fail in floating point.
One more tool. Adding shifts every eigenvalue by and keeps the eigenvectors: . So a small multiple of turns a PSD matrix into a positive definite one. Ridge regression does exactly this with , and Gaussian processes call it "jitter".
A covariance is always positive semidefinite
Here is a tiny dataset: examples with features, already centered so that each feature has mean zero. The rows of are
Its sample covariance is
our running example. (Many libraries divide by instead; that rescales and changes no eigenvector.)
Why is a covariance never indefinite? Pick any direction and build a new feature, the projection of each example. It has mean zero and variance
A variance cannot be negative, so every covariance matrix is PSD. The same holds for the covariance of a random vector: .
When is only semidefinite? When for some , that is, : some combination of features is constant on the data. A duplicated feature does it. So does having fewer examples than features, since by rank–nullity (the topic Matrices and linear systems) the null space of is then nonzero.
The argument works for any matrix of the form . A kernel matrix is , where row of is the feature map , so . The Hessian of least squares, , is PSD too; the topic Convex sets and functions will read this as convexity.
The Rayleigh quotient: the extreme directions
How large can a quadratic form be? Its value grows with the length of , so divide that out. The Rayleigh quotient is
Scaling does not change , so it depends on the direction only. For a unit vector , ; when is a covariance, that is the variance of the data along .
In eigen-coordinates the lengths agree, , because is orthogonal. Hence
The weights are non-negative and sum to , so is a weighted average of the eigenvalues. An average lies between the smallest and the largest value:
with the maximum at , the eigenvector of , and the minimum at .
Check it on the running example at : . In eigen-coordinates , so the weights are and , and indeed .
The Rayleigh quotient is also how power iteration (the topic Determinants and eigenvalues) reads off the eigenvalue. Starting from and repeating , the values are , , , : they reach quickly.
The direction of largest variance
Back to the four data points. Suppose we may keep only one number per example, its projection onto a unit direction . To keep as much of the spread as possible, we maximize the variance . That is a Rayleigh quotient, so the answer is the top eigenvector:
The projections of the four points are , and the mean of their squares is indeed . Along the variance is only .
More generally, the variance along each is , and the total variance of all features is . Projecting onto the top eigenvectors keeps the largest possible share of it. This is principal component analysis (PCA): the are the principal components.
Everything in this topic needed a square symmetric matrix, so we formed . But the data matrix itself is , and so is a typical weight matrix: rectangular, not symmetric. Is there a decomposition of itself, with perpendicular axes on both sides? That is the next topic, The singular value decomposition.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.