Skip to content
BIASED

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 ht+1=Whth_{t+1} = W h_t. 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

A=(4123),x=(10).A = \begin{pmatrix} 4 & 1 \\ 2 & 3 \end{pmatrix}, \qquad x = \begin{pmatrix} 1 \\ 0 \end{pmatrix}.

Apply AA a few times:

Ax=(42),A2x=(1814),A3x=(8678).Ax = \begin{pmatrix} 4 \\ 2 \end{pmatrix}, \qquad A^2 x = \begin{pmatrix} 18 \\ 14 \end{pmatrix}, \qquad A^3 x = \begin{pmatrix} 86 \\ 78 \end{pmatrix}.

The vector grows about fivefold per step, and the ratio of its coordinates creeps towards 11: first 22, then 1.291.29, then 1.101.10. Three questions follow. Does AkxA^k x 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:

(d100d2)k(x1x2)=(d1k x1d2k x2).\begin{pmatrix} d_1 & 0 \\ 0 & d_2 \end{pmatrix}^{k} \begin{pmatrix} x_1 \\ x_2 \end{pmatrix} = \begin{pmatrix} d_1^k\, x_1 \\ d_2^k\, x_2 \end{pmatrix}.

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 e1=(1,0)e_1 = (1, 0) and e2=(0,1)e_2 = (0, 1). A 2×22 \times 2 matrix sends e1e_1 and e2e_2 to its two columns, so the square becomes the parallelogram spanned by the columns.

The determinant det⁡A\det A 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:

  1. det⁡I=1\det I = 1: the identity leaves the square alone.
  2. det⁡A\det A is linear in each column while the others stay fixed. In particular, scaling one column by cc scales the determinant by cc.
  3. If two columns are equal, det⁡A=0\det A = 0: 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 00 by rule 3. Geometrically, such a shear slides the parallelogram but keeps its base and height. Swapping two columns flips the sign: expanding det⁡(u+w, u+w)=0\det(u + w,\, u + w) = 0 by rule 2 leaves det⁡(u,w)+det⁡(w,u)=0\det(u, w) + \det(w, u) = 0.

Now expand both columns in e1,e2e_1, e_2. The terms with a repeated eie_i vanish by rule 3, and the swap rule turns det⁡(e2,e1)\det(e_2, e_1) into −det⁡I=−1-\det I = -1:

det⁡(abcd)=det⁡(ae1+ce2,  be1+de2)=addet⁡(e1,e2)+cbdet⁡(e2,e1)=ad−bc.\det \begin{pmatrix} a & b \\ c & d \end{pmatrix} = \det(a e_1 + c e_2,\; b e_1 + d e_2) = ad \det(e_1, e_2) + cb \det(e_2, e_1) = ad - bc.

Three examples show the three cases:

det⁡(4123)=10,det⁡(1221)=−3,det⁡(2142)=0.\det \begin{pmatrix} 4 & 1 \\ 2 & 3 \end{pmatrix} = 10, \qquad \det \begin{pmatrix} 1 & 2 \\ 2 & 1 \end{pmatrix} = -3, \qquad \det \begin{pmatrix} 2 & 1 \\ 4 & 2 \end{pmatrix} = 0.

The running example multiplies areas by 1010 and keeps the orientation. The second matrix multiplies areas by 33 and mirrors the plane. The third squashes the square onto a line: its columns are dependent, its rank is 11, and no inverse can undo it (see the topic Matrices and linear systems).

In nn dimensions the same three rules define the signed volume of the parallelepiped spanned by the nn columns.

Products, inverses and transposes

The determinant shines on compositions. The product ABAB applies BB first and then AA. BB scales every volume by det⁡B\det B, then AA scales the result by det⁡A\det A, so

det⁡(AB)=det⁡A⋅det⁡B.\det(AB) = \det A \cdot \det B.

A stack of linear layers W3W2W1W_3 W_2 W_1 scales volume by the product of three determinants. Several facts follow from this one rule.

  • Invertibility. If A−1A^{-1} exists, then det⁡A⋅det⁡A−1=det⁡I=1\det A \cdot \det A^{-1} = \det I = 1, so det⁡A−1=1/det⁡A\det A^{-1} = 1/\det A and det⁡A≠0\det A \ne 0. Conversely, if AA 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 det⁡A=0\det A = 0 by linearity. So det⁡A≠0\det A \ne 0 forces independent columns, and AA is invertible. In short, AA is invertible exactly when det⁡A≠0\det A \ne 0.
  • Transpose. det⁡A⊤=det⁡A\det A^\top = \det A, 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 Q⊤Q=IQ^\top Q = I we get (det⁡Q)2=1(\det Q)^2 = 1, so det⁡Q=±1\det Q = \pm 1: +1+1 for rotations, −1-1 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 det⁡(A+B)≠det⁡A+det⁡B\det(A + B) \ne \det A + \det B.

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 n!n! signed products: for n=20n = 20 that is about 2.4⋅10182.4 \cdot 10^{18} terms.

Elimination is far cheaper. Since det⁡A⊤=det⁡A\det A^\top = \det A, 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 AA to an upper triangular UU with exactly these steps, so

det⁡A=(−1)s u11u22⋯unn,\det A = (-1)^s\, u_{11} u_{22} \cdots u_{nn},

where ss is the number of row swaps and uiiu_{ii} are the pivots. For example,

M=(211433879)  ⟶  U=(211011002),det⁡M=2⋅1⋅2=4.M = \begin{pmatrix} 2 & 1 & 1 \\ 4 & 3 & 3 \\ 8 & 7 & 9 \end{pmatrix} \;\longrightarrow\; U = \begin{pmatrix} 2 & 1 & 1 \\ 0 & 1 & 1 \\ 0 & 0 & 2 \end{pmatrix}, \qquad \det M = 2 \cdot 1 \cdot 2 = 4.

This takes about 23n3\tfrac23 n^3 operations, some five thousand for n=20n = 20.

In ML, determinants usually sit inside logarithms. The log-density of a Gaussian contains −12log⁡det⁡Σ-\tfrac12 \log\det\Sigma. 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 y=Wxy = Wx that is log⁡∣det⁡W∣\log\lvert\det W\rvert; 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 Σ=0.1 I\Sigma = 0.1\, I in 10001000 dimensions, det⁡Σ=10−1000\det\Sigma = 10^{-1000}, far below the smallest float64, and the computer returns 00. Its logarithm, 1000log⁡0.1≈−2302.61000 \log 0.1 \approx -2302.6, 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. 0.1 I0.1\, I has a tiny determinant and is trivial to invert, while (1111+10−12)\begin{pmatrix} 1 & 1 \\ 1 & 1 + 10^{-12} \end{pmatrix} has determinant 10−1210^{-12} 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 AA merely stretch? A nonzero vector vv is an eigenvector of AA with eigenvalue λ\lambda if

Av=λv.Av = \lambda v.

The line through vv maps onto itself, scaled by λ\lambda; a negative λ\lambda also flips it. The running example has two such lines:

A(11)=(55)=5(11),A(1−2)=(2−4)=2(1−2).A \begin{pmatrix} 1 \\ 1 \end{pmatrix} = \begin{pmatrix} 5 \\ 5 \end{pmatrix} = 5 \begin{pmatrix} 1 \\ 1 \end{pmatrix}, \qquad A \begin{pmatrix} 1 \\ -2 \end{pmatrix} = \begin{pmatrix} 2 \\ -4 \end{pmatrix} = 2 \begin{pmatrix} 1 \\ -2 \end{pmatrix}.

How do we find them? Av=λvAv = \lambda v means (A−λI)v=0(A - \lambda I)v = 0 with v≠0v \ne 0, so A−λIA - \lambda I must be singular. By the previous blocks, that happens exactly when

det⁡(A−λI)=0.\det(A - \lambda I) = 0.

The left side is a polynomial of degree nn in λ\lambda, the characteristic polynomial. For a 2×22 \times 2 matrix it is λ2−(tr⁡A) λ+det⁡A\lambda^2 - (\operatorname{tr} A)\,\lambda + \det A, where the trace tr⁡A\operatorname{tr} A is the sum of the diagonal entries. For the running example, λ2−7λ+10=(λ−5)(λ−2)\lambda^2 - 7\lambda + 10 = (\lambda - 5)(\lambda - 2).

Once λ\lambda is known, its eigenvectors form the null space of A−λIA - \lambda I. For λ=5\lambda = 5 we solve (−112−2)v=0\begin{pmatrix} -1 & 1 \\ 2 & -2 \end{pmatrix} v = 0 and get v=(1,1)v = (1, 1).

Roots can be complex. The rotation by 90°90°, R=(0−110)R = \begin{pmatrix} 0 & -1 \\ 1 & 0 \end{pmatrix}, has characteristic polynomial λ2+1\lambda^2 + 1 and eigenvalues ±i\pm i: a rotation turns every line, so no real direction survives. Over the complex numbers and counted with multiplicity, an n×nn \times n matrix always has nn eigenvalues λ1,…,λn\lambda_1, \dots, \lambda_n, and the characteristic polynomial factors over them. For a 2×22 \times 2 matrix, compare the two forms:

λ2−(tr⁡A) λ+det⁡A=(λ−λ1)(λ−λ2)=λ2−(λ1+λ2) λ+λ1λ2.\lambda^2 - (\operatorname{tr} A)\,\lambda + \det A = (\lambda - \lambda_1)(\lambda - \lambda_2) = \lambda^2 - (\lambda_1 + \lambda_2)\,\lambda + \lambda_1 \lambda_2 .

The coefficients of λ\lambda must agree, and so must the constant terms. In nn dimensions the same comparison, for the coefficient of λn−1\lambda^{n-1} and the constant term, gives

tr⁡A=λ1+⋯+λn,det⁡A=λ1⋯λn.\operatorname{tr} A = \lambda_1 + \dots + \lambda_n, \qquad \det A = \lambda_1 \cdots \lambda_n.

For the running example, 5+2=75 + 2 = 7 and 5⋅2=105 \cdot 2 = 10. Two consequences: AA is singular exactly when 00 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 AA has nn linearly independent eigenvectors v1,…,vnv_1, \dots, v_n, an eigenbasis. Put them as the columns of VV and the eigenvalues on the diagonal of Λ\Lambda. The nn equations Avi=λiviA v_i = \lambda_i v_i become one, AV=VΛAV = V\Lambda. Since VV is invertible,

A=VΛV−1,Ak=VΛkV−1.A = V \Lambda V^{-1}, \qquad A^k = V \Lambda^k V^{-1}.

In AkA^k all the inner factors V−1VV^{-1} V cancel, and Λk\Lambda^k just raises each diagonal entry to the power kk. This is diagonalization. Read it right to left: V−1V^{-1} gives the coordinates cc of xx in the eigenbasis, Λk\Lambda^k scales them, VV maps back. In the eigenbasis, AA is the easy diagonal case from the first block.

For the running example, x=(1,0)=23(1,1)+13(1,−2)x = (1, 0) = \tfrac23 (1, 1) + \tfrac13 (1, -2), so

Akx=23⋅5k(11)+13⋅2k(1−2).A^k x = \tfrac23 \cdot 5^k \begin{pmatrix} 1 \\ 1 \end{pmatrix} + \tfrac13 \cdot 2^k \begin{pmatrix} 1 \\ -2 \end{pmatrix}.

At k=3k = 3 this gives (86,78)(86, 78), as in the first block. The three questions now have answers. The vector grows like 5k5^k, set by the largest eigenvalue. Its direction approaches (1,1)(1, 1), the eigenvector of that eigenvalue. The other component shrinks relative to it by a factor 2/52/5 per step, so the speed is set by ∣λ2/λ1∣\lvert\lambda_2 / \lambda_1\rvert.

This is power iteration. Multiply a starting vector by AA again and again, normalizing to keep the numbers finite, and it lines up with the eigenvector of the largest ∣λ∣\lvert\lambda\rvert, 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 ht=Wth0h_t = W^t h_0. If some ∣λ∣>1\lvert\lambda\rvert > 1, the state explodes; if every ∣λ∣<1\lvert\lambda\rvert < 1, it fades to zero. Gradients flowing back through time are multiplied by W⊤W^\top at every step and explode or vanish the same way.

When does an eigenbasis exist? A sufficient condition: if all nn eigenvalues are distinct, their eigenvectors are independent, and AA 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

P=(0.70.20.10.30.40.30.20.30.5).P = \begin{pmatrix} 0.7 & 0.2 & 0.1 \\ 0.3 & 0.4 & 0.3 \\ 0.2 & 0.3 & 0.5 \end{pmatrix}.

Here PijP_{ij} is the probability of moving from state ii to state jj, so every row sums to 11. A distribution over the states is a row vector πt\pi_t, one step is πt+1=πtP\pi_{t+1} = \pi_t P, and so πt=π0Pt\pi_t = \pi_0 P^t.

The eigenvalues decide the long run. Every row sums to 11, so P1=1P\mathbf 1 = \mathbf 1 for the vector of ones, and 11 is an eigenvalue. P⊤P^\top has the same eigenvalues, because det⁡(P⊤−λI)=det⁡((P−λI)⊤)=det⁡(P−λI)\det(P^\top - \lambda I) = \det\big((P - \lambda I)^\top\big) = \det(P - \lambda I). So there is a row vector π\pi with

πP=π,\pi P = \pi,

a left eigenvector of PP with eigenvalue 11. Scaled to sum to 11, it is the stationary distribution: once the chain gets there, it stays. For the weather, π=146(21,13,12)≈(0.457,0.283,0.261)\pi = \tfrac{1}{46}(21, 13, 12) \approx (0.457, 0.283, 0.261).

No eigenvalue of such a PP exceeds 11 in absolute value. Here the other two are (3±3)/10≈0.473(3 \pm \sqrt3)/10 \approx 0.473 and 0.1270.127, and indeed 1+0.473+0.127=1.6=tr⁡P1 + 0.473 + 0.127 = 1.6 = \operatorname{tr} P.

The three eigenvalues are distinct, so P⊤P^\top is diagonalizable. The formula for AkxA^k x from the previous block, applied to P⊤P^\top and π0⊤\pi_0^\top, writes the distribution after tt steps as

πt=π+α2 0.473t w2+α3 0.127t w3,\pi_t = \pi + \alpha_2\, 0.473^t\, w_2 + \alpha_3\, 0.127^t\, w_3,

where w2,w3w_2, w_3 are the other two left eigenvectors and α2,α3\alpha_2, \alpha_3 depend on the start. Why is the first term exactly π\pi? A left eigenvector ww with eigenvalue μ≠1\mu \ne 1 has entries summing to 00, since w1=wP1=μ w1w\mathbf 1 = wP\mathbf 1 = \mu\, w\mathbf 1. Every πt\pi_t sums to 11, so the term along the eigenvalue 11 sums to 11, and that multiple of π\pi is π\pi itself. Starting from a sunny day, the total distance ∑j∣πt(j)−π(j)∣\sum_j \lvert \pi_t(j) - \pi(j) \rvert falls from 1.0871.087 at t=0t = 0 to 0.4870.487 at t=1t = 1 and 5.7⋅10−45.7 \cdot 10^{-4} at t=10t = 10. It shrinks roughly like 0.473t0.473^t, the second-largest ∣λ∣\lvert\lambda\rvert.

The spectral gap 1−∣λ2∣1 - \lvert\lambda_2\rvert 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 nn independent eigenvectors, and a general matrix need not have them. The shear

J=(1101)J = \begin{pmatrix} 1 & 1 \\ 0 & 1 \end{pmatrix}

has characteristic polynomial (λ−1)2(\lambda - 1)^2. The eigenvalue 11 is a double root: its algebraic multiplicity is 22. But J−I=(0100)J - I = \begin{pmatrix} 0 & 1 \\ 0 & 0 \end{pmatrix} has a one-dimensional null space, spanned by (1,0)(1, 0), so its geometric multiplicity is 11. One line of eigenvectors cannot form a basis of the plane. The powers show the damage:

Jk=(1k01).J^k = \begin{pmatrix} 1 & k \\ 0 & 1 \end{pmatrix}.

Each multiplication by JJ on the right adds the first column to the second, so the corner grows by 11 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 ε≠0\varepsilon \ne 0, the matrix (1101+ε)\begin{pmatrix} 1 & 1 \\ 0 & 1 + \varepsilon \end{pmatrix} has distinct eigenvalues 11 and 1+ε1 + \varepsilon, with eigenvectors (1,0)(1, 0) and (1,ε)(1, \varepsilon). These two columns of VV are almost parallel, and computing through V−1V^{-1} 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 X⊤XX^\top X 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.

Full notes

Answers are saved on the server and tied to this browser until you sign in.