Determinants and eigenvalues
What a matrix does when applied again and again: the determinant as signed volume and a test of invertibility, eigenvectors and the characteristic polynomial, diagonalization and powers $A^k$, power iteration and the stationary distribution of a Markov chain.
A matrix applied again and again
The topic Inner products, norms and orthogonality gave us lengths, angles and projections. An orthogonal matrix keeps every length; a general one stretches some directions more than others. Which directions does it merely stretch? The question matters most when a matrix is applied again and again.
That situation is everywhere in ML. A linear recurrent network updates its hidden state as . A Markov chain moves a probability distribution forward one step at a time. An iterative algorithm multiplies a vector by the same matrix until it stops changing.
The running example of this topic is
Apply a few times:
The vector grows about fivefold per step, and the ratio of its coordinates creeps towards : first , then , then . Three questions follow. Does grow or shrink? Which direction does it approach? How fast?
For a diagonal matrix all three are easy, because it stretches each coordinate axis on its own:
This topic finds the directions in which any matrix acts like a diagonal one. Along the way we need the determinant: one number that says how a matrix scales volume and whether it can be inverted.
The determinant as signed area
Take the unit square spanned by and . A matrix sends and to its two columns, so the square becomes the parallelogram spanned by the columns.
The determinant is the signed area of that parallelogram. Its absolute value says how many times the map scales areas. Its sign says whether the map keeps the orientation of the plane or flips it, like a mirror.
Three rules pin the determinant down:
- : the identity leaves the square alone.
- is linear in each column while the others stay fixed. In particular, scaling one column by scales the determinant by .
- If two columns are equal, : the parallelogram is flat.
Two more facts follow. Adding a multiple of one column to another does not change the determinant: by rule 2 the change is a multiple of a determinant with two equal columns, which is by rule 3. Geometrically, such a shear slides the parallelogram but keeps its base and height. Swapping two columns flips the sign: expanding by rule 2 leaves .
Now expand both columns in . The terms with a repeated vanish by rule 3, and the swap rule turns into :
Three examples show the three cases:
The running example multiplies areas by and keeps the orientation. The second matrix multiplies areas by and mirrors the plane. The third squashes the square onto a line: its columns are dependent, its rank is , and no inverse can undo it (see the topic Matrices and linear systems).
In dimensions the same three rules define the signed volume of the parallelepiped spanned by the columns.
Products, inverses and transposes
The determinant shines on compositions. The product applies first and then . scales every volume by , then scales the result by , so
A stack of linear layers scales volume by the product of three determinants. Several facts follow from this one rule.
- Invertibility. If exists, then , so and . Conversely, if is singular, its columns are dependent, so one column is a combination of the others. Subtracting that combination from it leaves a zero column without changing the determinant, and a zero column gives by linearity. So forces independent columns, and is invertible. In short, is invertible exactly when .
- Transpose. , so every rule about columns holds for rows too.
- Triangular matrices. The determinant of a triangular matrix is the product of its diagonal entries.
- Orthogonal matrices. From we get , so : for rotations, for reflections. An orthogonal map keeps lengths (see Inner products, norms and orthogonality), so it keeps volumes as well.
Note what is missing: there is no rule for sums. In general .
Computing determinants and their logarithms
How do we compute the determinant of a large matrix? There is an explicit formula, the Leibniz formula, but it sums signed products: for that is about terms.
Elimination is far cheaper. Since , the column rules hold for rows: adding a multiple of one row to another leaves the determinant unchanged, and swapping two rows flips its sign. Gaussian elimination reduces to an upper triangular with exactly these steps, so
where is the number of row swaps and are the pivots. For example,
This takes about operations, some five thousand for .
In ML, determinants usually sit inside logarithms. The log-density of a Gaussian contains . A normalizing flow, a generative model built from invertible layers, adds to the log-likelihood the log of how much each layer scales volume. For a linear layer that is ; for a nonlinear layer it is the same quantity for the matrix of derivatives, the Jacobian (topic Gradients, Jacobians and Hessians). Flows are designed with triangular matrices, so the determinant is just the product of the diagonal.
Long products underflow. For in dimensions, , far below the smallest float64, and the computer returns . Its logarithm, , is a perfectly ordinary number. So libraries compute the log-determinant directly, as a sum of logs of the pivots: that is what np.linalg.slogdet and torch.linalg.slogdet do.
One caution. A small determinant does not mean a matrix is nearly singular. has a tiny determinant and is trivial to invert, while has determinant and is nearly singular. Closeness to singularity is measured by the condition number, which comes with the SVD.
Eigenvectors and the characteristic polynomial
Back to the opening question: which directions does merely stretch? A nonzero vector is an eigenvector of with eigenvalue if
The line through maps onto itself, scaled by ; a negative also flips it. The running example has two such lines:
How do we find them? means with , so must be singular. By the previous blocks, that happens exactly when
The left side is a polynomial of degree in , the characteristic polynomial. For a matrix it is , where the trace is the sum of the diagonal entries. For the running example, .
Once is known, its eigenvectors form the null space of . For we solve and get .
Roots can be complex. The rotation by , , has characteristic polynomial and eigenvalues : a rotation turns every line, so no real direction survives. Over the complex numbers and counted with multiplicity, an matrix always has eigenvalues , and the characteristic polynomial factors over them. For a matrix, compare the two forms:
The coefficients of must agree, and so must the constant terms. In dimensions the same comparison, for the coefficient of and the constant term, gives
For the running example, and . Two consequences: is singular exactly when is an eigenvalue, and the trace of a covariance matrix, the total variance of the data, is the sum of its eigenvalues. PCA starts from that second fact.
Diagonalization and matrix powers
Suppose has linearly independent eigenvectors , an eigenbasis. Put them as the columns of and the eigenvalues on the diagonal of . The equations become one, . Since is invertible,
In all the inner factors cancel, and just raises each diagonal entry to the power . This is diagonalization. Read it right to left: gives the coordinates of in the eigenbasis, scales them, maps back. In the eigenbasis, is the easy diagonal case from the first block.
For the running example, , so
At this gives , as in the first block. The three questions now have answers. The vector grows like , set by the largest eigenvalue. Its direction approaches , the eigenvector of that eigenvalue. The other component shrinks relative to it by a factor per step, so the speed is set by .
This is power iteration. Multiply a starting vector by again and again, normalizing to keep the numbers finite, and it lines up with the eigenvector of the largest , provided it had some component along it. It is the simplest way to find a top eigenvector, and libraries use refined versions of it.
The same mechanism drives a linear recurrent network . If some , the state explodes; if every , it fades to zero. Gradients flowing back through time are multiplied by at every step and explode or vanish the same way.
When does an eigenbasis exist? A sufficient condition: if all eigenvalues are distinct, their eigenvectors are independent, and is diagonalizable.
Markov chains and the stationary distribution
A Markov chain is the cleanest case of a repeated map. Take a weather model with three states, sunny, cloudy and rainy, and the transition matrix
Here is the probability of moving from state to state , so every row sums to . A distribution over the states is a row vector , one step is , and so .
The eigenvalues decide the long run. Every row sums to , so for the vector of ones, and is an eigenvalue. has the same eigenvalues, because . So there is a row vector with
a left eigenvector of with eigenvalue . Scaled to sum to , it is the stationary distribution: once the chain gets there, it stays. For the weather, .
No eigenvalue of such a exceeds in absolute value. Here the other two are and , and indeed .
The three eigenvalues are distinct, so is diagonalizable. The formula for from the previous block, applied to and , writes the distribution after steps as
where are the other two left eigenvectors and depend on the start. Why is the first term exactly ? A left eigenvector with eigenvalue has entries summing to , since . Every sums to , so the term along the eigenvalue sums to , and that multiple of is itself. Starting from a sunny day, the total distance falls from at to at and at . It shrinks roughly like , the second-largest .
The spectral gap measures how fast a chain forgets where it started. MCMC samplers want a large gap. PageRank is the stationary distribution of a random surfer clicking links, computed by power iteration on a matrix with billions of rows.
When diagonalization fails
Diagonalization needs independent eigenvectors, and a general matrix need not have them. The shear
has characteristic polynomial . The eigenvalue is a double root: its algebraic multiplicity is . But has a one-dimensional null space, spanned by , so its geometric multiplicity is . One line of eigenvectors cannot form a basis of the plane. The powers show the damage:
Each multiplication by on the right adds the first column to the second, so the corner grows by per step.
The Jordan form describes such matrices in general; we will not need it.
A nearby case matters more in practice. With a small , the matrix has distinct eigenvalues and , with eigenvectors and . These two columns of are almost parallel, and computing through amplifies rounding errors.
So a general square matrix can disappoint in three ways: complex eigenvalues, like the rotation; eigenvectors that are not orthogonal, like those of the running example; and too few eigenvectors, like the shear.
The matrices ML cares about most avoid all three. A covariance matrix and the matrix of the normal equations are symmetric. Symmetric matrices have real eigenvalues, orthogonal eigenvectors and always a full eigenbasis. That is the topic Symmetric matrices and quadratic forms.
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.