← back to home

A visual derivation of the Sherman-Morrison formula

Here's a somewhat surprising fact about matrix inversion:

The inverse of a rank-one perturbation of AA
equals a rank-one perturbation of A−1A^{-1}

…assuming all the inverses exist, yada yada. This fact is captured by the Sherman-Morrison formula:

(A+p q⊤)−1=A−1−A−1p q⊤A−11+q⊤A−1p.\left(A+p\, q^\top\right)^{-1} = A^{-1} - \frac{A^{-1}p\, q^\top A^{-1}}{1 + q^\top A^{-1} p}.

I thought this formula was super opaque and unintuitive. So, on a plane ride back from NYC, I re-derived it using a visual argument. It turns out this argument is on Wikipedia so sadly my derivation isn't that cool. But I wanna show it to you anyways! Plus, I have a widget, so that's extra points for me.

A visual derivation of the Sherman-Morrison formula

We want to invert M:=A+p q⊤M:=A + p\, q^\top. Let's factor it out: first, we invert AA, then we composite it with a correction that comes from the perturbation:

(A+p q⊤)−1=(I+A−1p q⊤)−1A−1=:(I+p~ q⊤)−1A−1\left(A + p\, q^\top\right)^{-1} = \left(I + A^{-1}p\, q^\top\right)^{-1} A^{-1} =: \left(I + \tilde p\, q^\top\right)^{-1} A^{-1}

where I defined p~=A−1p\tilde p = A^{-1}p. The multiplicative correction from the perturbation is the inverse of a perturbation of the identity. This is something we can understand geometrically. The transformation v′=(I+p~ q⊤)vv' = \left(I + \tilde p\, q^\top\right)v basically says: "Return the original vector vv, but skewed along direction p~\tilde p, and the amount of skew is equal to the overlap of qq and vv." Here's an illustration:

A rank-one perturbation of the identity. Each arrow starts at a point vv and ends at its image v′=(I+p~ q⊤)vv' = \left(I + \tilde p\, q^\top\right) v. Every vv skews along p~\tilde p (blue) by q⊤vq^\top v. The dashed line is the locus of q⊤v=0q^\top v=0, so points there don't move, and rows further from it skew further. Drag the tips of p~\tilde p and qq to change them.

Clearly, (I+p~ q⊤)−1\left(I + \tilde p\, q^\top\right)^{-1} is the "un-skew" operation: from v′v' we should subtract (q⊤v)p~\left(q^\top v\right)\tilde p. Of course, we don't know vv, so we don't immediately know the skew strength q⊤vq^\top v. But here's a trick:

q⊤v′=q⊤(I+p~ q⊤)v=(q⊤v)(1+q⊤p~)  ⟹  q⊤v=q⊤v′(1+q⊤p~).\begin{align*} q^\top v' &= q^\top \left(I + \tilde p\, q^\top\right) v = \left(q^\top v\right)\left(1 + q^\top \tilde p\right)\\ \implies q^\top v &= \frac{q^\top v'}{\left(1 + q^\top \tilde p\right)}. \end{align*}

This equation tells me that, after skewing along p~\tilde p, we might've also moved along the direction qq, depending on how much p~\tilde p and qq are aligned. This means the overlap between qq and the image v′v' misestimates the original skew strength by that alignment factor. We just need to correct for that misestimation by dividing the factor out. Rewriting this same argument mathematically, we get

(I+p~ q⊤)−1v′=v′−p~(q⊤v)=v′−p~ q⊤v′1+q⊤p~=(I−p~ q⊤1+q⊤p~)v′.\begin{aligned} \left(I + \tilde p\, q^\top\right)^{-1} v' &= v' - \tilde p\left(q^\top v\right) = v' - \frac{\tilde p\, q^\top v'}{1 + q^\top \tilde p} \\ &= \left(I - \frac{\tilde p\, q^\top}{1 + q^\top \tilde p}\right) v' . \end{aligned}

That's it! We solved for the inverse using a simple geometric argument. Putting it all together:

(A+p q⊤)−1=(I+A−1p q⊤)−1A−1=(I−A−1p q⊤1+q⊤A−1p)A−1=A−1−A−1p q⊤A−11+q⊤A−1p.\begin{aligned} \left(A + p\, q^\top\right)^{-1} &= \left(I + A^{-1}p\, q^\top\right)^{-1} A^{-1} \\ &= \left(I - \frac{A^{-1}p\, q^\top}{1 + q^\top A^{-1} p}\right) A^{-1} \\ &= A^{-1} - \frac{A^{-1}p\, q^\top A^{-1}}{1 + q^\top A^{-1} p} . \end{aligned}

We derived the Sherman-Morrison formula. My interpretation is that the perturbed inverse is basically just the original inverse, followed by a rank-one correction along the skew direction. The normalizing term q⊤A−1pq^\top A^{-1} p in the denominator ensures that we don't misestimate the effect of the perturbation.

The bias term of generalization in linear regression

We can use the Sherman-Morrison formula to derive the expected bias of linear regression in the heavily overparameterized (limited data) regime. Here's how.

The data matrix XX is short and stout: X∈Rn×dX\in\mathbb{R}^{n\times d}, with n≪dn \ll d. Its rows, which index the nn data samples, are quite outnumbered by its columns, which index the data dimension dd. We should think of XX as a random variable: we've drawn some particular samples from a Gaussian distribution with expected covariance Λ\Lambda. Let's work in the PCA basis of the input distribution, so Λ\Lambda is diagonal and full-rank.This is quite different from the sample covariance X⊤XX^\top X, which is singular due to the overparameterization.

The labels are scalar, noiseless, linear: y=w⋆⊤xy = w_\star^\top x. You can add noise later if you want, but here I'll keep things simple. The unregularized min-norm estimator is well-known: w^(X)=Pw⋆\hat w(X) = P w_\star, where P∈Rd×dP \in \mathbb{R}^{d\times d} is the projector onto the (strict) subspace spanned by the data samples, and is therefore also a random variable. This spanning subspace has dimension nn (assuming the nn data samples are linearly independent, which they should be, unless you got super unlucky somehow). Therefore Tr[P]=n\mathrm{Tr}\mleft[P\mright]=n. This'll be an important fact later.

For a given XX, the weight-space errorThe typical function-space MSE is almost the same thing, just reweighted by Λ\Lambda. I'm leaving it out for notational clarity, it's easy to add back in. is E(X)=∥w⋆−w^(X)(∥2\mathcal{E}(X) = \norm{w_\star - \hat w(X)}^2. But we want to know what happens on average. Taking expectation over XX, we get

EX[E(X)]=EX[∥w⋆−w^(X)(∥2]=∥w⋆−EX[w^(X)](∥2⏟bias+EX[∥w^(X)−EX[w^(X)](∥2]⏟variance.\begin{aligned} \mathbb{E}_X\mleft[\mathcal{E}(X)\mright] &= \mathbb{E}_X\mleft[\norm{w_\star - \hat w(X)}^2\mright] \\ &= \underbrace{\norm{w_\star - \mathbb{E}_X\mleft[\hat w(X)\mright]}^2}_{\text{bias}} + \underbrace{\mathbb{E}_X\mleft[\norm{\hat w(X) - \mathbb{E}_X\mleft[\hat w(X)\mright]}^2\mright]}_{\text{variance}} . \end{aligned}

The variance term is highkey kinda annoying to calculate, since it requires second-order statistics of PP. We can get a pretty good idea of how generalization works by simply studying the bias term, so let's just do that. We have to evaluate

EX[w^(X)]=EX[Pw⋆]=EX[X⊤(XX⊤)−1X]w⋆\mathbb{E}_X\mleft[\hat w(X)\mright]=\mathbb{E}_X\mleft[P w_\star\mright] = \mathbb{E}_X\mleft[X^\top \left(XX^\top\right)^{-1}X\mright] w_\star

where I used the formula for the projector onto XX. A reasonable hunch is that EX[P]\mathbb{E}_X\mleft[P\mright] is diagonal, since we're in the PCA basis, so by symmetry the average projector should be aligned with it. To check this, let's study this matrix elementwise. The matrix elements can be extracted by picking out columns of XX; e.g., if we denote the ii-th column of XX as ψi\psi_i, then the (i,j)\left(i, j\right) element of PP is

Pij=ψi⊤(∑kψkψk⊤)−1ψj.P_{ij} = \psi_i^\top \left(\sum_k \psi_k \psi_k^\top\right)^{-1} \psi_j .

Note that the vector ψi\psi_i contains the ii-th PCA feature of every xx in the dataset. If we reflect the ii-th coordinate of every xx, ψi→−ψi\psi_i \to -\psi_i, then the sign of PijP_{ij} flips (assuming i≠ji \neq j; if i=ji=j then the sign flips twice, no change). However, since xx is drawn Gaussian, and we're in the PCA basis, this reflection doesn't change the underlying distribution. So, for off-diagonal elements i≠ji\neq j, the distribution of PijP_{ij} (whatever it may be) has to be symmetric around zero, and so E[Pij]=0\mathbb{E}\mleft[P_{ij}\mright]=0.

Nice, so EX[P]\mathbb{E}_X\mleft[P\mright] is diagonal, as we guessed. Let's solve for the diagonal elements PiiP_{ii}. First, it'll be useful to make note of the following facts:

  1. The quantity we're trying to understand is Pii=ψi⊤K−1ψiP_{ii} = \psi_i^\top K^{-1} \psi_i, where K:=XX⊤=∑kψkψk⊤K:=XX^\top=\sum_k \psi_k \psi_k^\top is the sample gram matrix. Clearly, KK contains a bunch of rank-one components, one for each feature.
  2. The data is Gaussian with variance λi\lambda_i along feature ii, so ψi=λiϕi\psi_i=\sqrt{\lambda_i}\phi_i, where ϕi\phi_i is a standard Gaussian random vector.
  3. This means that Pii=λiϕi⊤K−1ϕiP_{ii} = \lambda_i \phi_i^\top K^{-1} \phi_i. This is a random quadratic form; if KK were fully independent of ϕi\phi_i, then in expectation ϕi⊤K−1ϕi\phi_i^\top K^{-1} \phi_i is the trace of K−1K^{-1}. Sadly, KK is definitely not independent of ϕi\phi_i, because one of its rank-one components is λiϕiϕi⊤\lambda_i \phi_i \phi_i^\top. So, if we wanna use the concentration of random quadratic forms, we have to separate that correlated component.

Here goes:

Pii=λi ϕi⊤(λi ϕiϕi⊤+∑k≠iλkϕkϕk⊤⏟=: K∖i)−1ϕi=λi zi⊤(I+λi zizi⊤)−1zi(where zi:=K∖i−1/2ϕi)=λi zi⊤(I−λi zizi⊤1+λizi⊤zi)zi=λi∥zi(∥21+λi∥zi(∥2.\begin{aligned} P_{ii} &= \lambda_i\, \phi_i^\top \left(\lambda_i\, \phi_i \phi_i^\top + \smash[b]{\underbrace{\sum_{k \neq i} \lambda_k \phi_k \phi_k^\top}_{=:\,K_{\setminus i}}}\right)^{-1} \phi_i \\[3.5em] &= \lambda_i\, z_i^\top \left(I + \lambda_i\, z_i z_i^\top\right)^{-1} z_i \qquad \left(\text{where } z_i := K_{\setminus i}^{-1/2} \phi_i\right) \\[1em] &= \lambda_i\, z_i^\top \left(I - \frac{\lambda_i\, z_i z_i^\top}{1 + \lambda_i z_i^\top z_i}\right) z_i \\[1.5em] &= \frac{\lambda_i \norm{z_i}^2}{1 + \lambda_i \norm{z_i}^2} . \end{aligned}

In the first line, I separated the one correlated component from all the independent ones. In the second line, I interpreted K∖i−1/2ϕiK_{\setminus i}^{-1/2} \phi_i as a sort of "ZCA-like" whitening, and wrote the equation in terms of the whitened vector ziz_i. (This step isn't really necessary, I just thought it made the calculation look nicer.) The third line is Sherman-Morrison, and the fourth is just simplification.

Now let's take the expectation. The only random variable is ∥zi(∥2\norm{z_i}^2. Our first approximation is that this random variable concentrates, so the expectation of PiiP_{ii} depends only on E[∥zi(∥2]\mathbb{E}\mleft[\norm{z_i}^2\mright], and not its fluctuations. Now we gotta solve E[∥zi(∥2]\mathbb{E}\mleft[\norm{z_i}^2\mright]; this will involve two steps. First is the trace identity for random quadratic forms: E[∥zi(∥2]=E[ϕi⊤K∖i−1ϕi]=Tr[K∖i−1]\mathbb{E}\mleft[\norm{z_i}^2\mright] = \mathbb{E}\mleft[\phi_i^\top K_{\setminus i}^{-1} \phi_i\mright] = \mathrm{Tr}\mleft[K_{\setminus i}^{-1}\mright]. The second part is a leave-one-out approximation: since d≫nd \gg n, removing any one feature leaves KK essentially unchanged, so for all intents and purposes K∖i≈K∖jK_{\setminus i} \approx K_{\setminus j} for any ii and jj. In particular, the trace of its inverse is basically independent of ii, and we can just name it κ−1:=Tr[K∖i−1]\kappa^{-1} := \mathrm{Tr}\mleft[K_{\setminus i}^{-1}\mright]. Plugging it in, we get

EX[Pii]≈λiκ−11+λiκ−1=λiλi+κ.\mathbb{E}_X\mleft[P_{ii}\mright] \approx \frac{\lambda_i \kappa^{-1}}{1 + \lambda_i \kappa^{-1}} = \frac{\lambda_i}{\lambda_i + \kappa}.

All together, we get EX[P]≈Λ(Λ+κI)−1\mathbb{E}_X\mleft[P\mright] \approx \Lambda \left(\Lambda + \kappa I\right)^{-1}. Using the projector fact from earlier, we know it satisfies the equation Tr[Λ(Λ+κI)−1]=n\mathrm{Tr}\mleft[\Lambda \left(\Lambda + \kappa I\right)^{-1}\mright] = n, so this constrains the value of κ\kappa.

Notice that this solution is exactly what ridge regression would give if it had access to the population covariance Λ\Lambda and was regularized with ridge κ\kappa. For this reason, the constant κ\kappa is called the effective regularization: even though we didn't explicitly regularize, the finite dataset acts like a ridge regularizer. Directions with λi≫κ\lambda_i \gg \kappa are learned, and directions with λi≪κ\lambda_i \ll \kappa are not.

This is a deep idea: in least-squares linear regression, there is really only one kind of regularization; weight decay, finite dataset, and even early stopping all act in essentially the same way, and the behavior of the final estimator depends mainly on whichever of those constraints is the strongest.

Anyways, plugging this into the (weight-space) bias term,

bias=∥(I−EX[P])w⋆(∥2≈∑i(1−λiλi+κ)2w⋆,i2.\text{bias} = \norm{\left(I - \mathbb{E}_X\mleft[P\mright]\right) w_\star}^2 \approx \sum_i \left(1-\frac{\lambda_i}{\lambda_i + \kappa}\right)^2 w_{\star, i}^2 .