Singular Value Decomposition

This post is the third in a 3-part series on building geometric intuition about key concepts in linear algebra.

  1. Linear Transformations
  2. Eigendecomposition
  3. SVD a.k.a Singular Value Decomposition (this)
In [1]:

What if M is not a symmetric matrix?

In the previous post , we looked at the geometric intuition behind decomposing the transformation of a real, symmetric matrix $M$. Now, let's look at what happens when $M$ is real, but not symmetric. For example,

$$ M = \begin{pmatrix} 2 & 0.1 \\ 1 & 2 \\ \end{pmatrix} $$

Let us inspect the eigenvalues and eigenvectors of $M$.

In [2]:
Eigenvalues (D)
 [[2.31622777 0.        ]
 [0.         1.68377223]] 

 Eigenvectors (C)
 [[ 0.30151134 -0.30151134]
 [ 0.95346259  0.95346259]]

Check the eigenvectors

  • They are not orthogonal
  • They are not along the major & minor axes of the ellipse (after transformation)
In [3]:

Vectors along the major / minor axes of ellipse

Let's denote the vectors along the major / minor axes of ellipse on the right as $u_1,u_2$.

In [4]:
Major 
 [1.62412143 2.03032745] 

 Minor 
 [-1.17142204  0.93689404]

Compute the representation of major / minor before transformation

  1. Let $V = [v_1, v_2]$ denote the coordinate representations of $U_v = [u_1,u_2]$ before the transformation.
  2. Let's plot the $u$ and $v$ vectors below.
    1. Note that the vectors are orthogonal.
    2. Note also that the $u$ vectors now correspond to the major / minor axes of the ellipse on the right.
In [5]:

Let's now normalize the column vectors in $U_v$

  1. Let $L$ store the lengths of the column vectors in $U_v$
  2. Let $U$ be the normalised column vectors in $U_v$
  3. Make $L$ a diagonal matrix so that $U_v = UL = MV$
In [6]:
In [7]:
assert np.allclose(U@L, M@V)

Singular Value Decomposition of $M$

\begin{align} UL &= MV \\ \therefore M &= ULV^{-1} = ULV^T \end{align}
  1. As $V$ is an orthogonal matrix, $V^{-1} = V^T$
  2. The columns of $U$ are the left singular vectors of $M$
  3. The diagonal entries of $L$ are the singular values of $M$
  4. The columns of $V$ are the right singular vectors of $M$
In [8]:
assert np.allclose(M, U@L@V.T)

Compare the linear transformation by $M$ vs. $ULV^T$

In [9]:
plot_linear_transformation(M, unit_circle=True)
In [10]:
plot_linear_transformations(V.T,L,U, unit_circle=True)

The linear transformation by $M$ is decomposed into these steps:

  1. $V^T$ first rotates the coordinate axes
  2. $L$ stretches the circle into an ellipse
  3. $U$ rotates the coordinate axes again

Let's now compare this decomposition with the SVD implementation from Numpy.

In [11]:
N_U, N_L, N_VT = np.linalg.svd(M)

As we see below, the blue vectors (from Numpy SVD) are aligned with the green vectors (computed above).

In [12]:

Let's now compare $V$ from Numpy SVD to the eigenvectors of $M^T M$.

In [19]:
V from Numpy SVD of M
 [[-0.78086881 -0.62469505]
 [-0.62469505  0.78086881]] 

 Eigenvectors
 [[ 0.78086881 -0.62469505]
 [ 0.62469505  0.78086881]]

We see that $V$ contains the eigenvectors of $M^TM$. The following derivation explains why this is the case.

Eigendecomposition of $M^T M$ vs. SVD of $M$

Given $M = U L V^T$, \begin{align} M^TM =& ~(U L V^T)^T (U L V^T) \\ =& ~V L^2 V^T \end{align}

We also see that the singular values $L$ of $M$ are the square roots of the eigenvalues of $M^TM$.

In [14]:
assert np.allclose(np.diag(D), L**2)

Similarly, $U$ contains the eigenvectors of $MM^T$.