Singular Value Decomposition (SVD) Part 1

Singular Value Decomposition (SVD) is a complex computation in Linear Algebra, and it’s widely used in recommendation system. By SVD, the input user-ratings matrix can be divided into three different matrices: user-features, features-features and item-features matrices. This post is to show the full calculation process of SVD and explain each step among it.

The format of SVD:

M=UΣVTM = U \Sigma V^T

M is the input m*n matrix, UU are the left singular vectors (m*k matrix), Σ\Sigma are the diagonal matrix (k*k matrix), VV are the right singular vectors (n*k matrix).

Step 1: Transfer M into a square matrix

In order to finsh SVD, we need to know the eigenvalues of the input matrix at first. But M is not always a square matrix, and it might be retangular matrix sometimes. So firstly, we need to transfer M into a square matrix by multiplying M with its transpose.

M=[110330],MM=[103130][110330]=[101110]M=\left[\begin{array}{ll} 1 & 1 \\ 0 & 3 \\ 3 & 0 \end{array}\right], \quad \quad M^{\top} M=\left[\begin{array}{lll} 1 & 0 & 3 \\ 1 & 3 & 0 \end{array}\right] \cdot\left[\begin{array}{ll} 1 & 1 \\ 0 & 3 \\ 3 & 0 \end{array}\right]=\left[\begin{array}{ll} 10 & 1 \\ 1 & 10 \end{array}\right]

Step 2: Calculate the eigenvalues

To get the eigenvalues, we need to calculate the determinant of MMλIM^{\top} M - \lambda I. In the computation of this determinant, a polynomial is obtained, and the results of this polynomial are the eigenvalues.

MMλI=[10λ1110λ],det(MMλI)=(10λ)21λ=11or9M^{\top} M - \lambda I=\left[\begin{array}{ll} 10 - \lambda & 1 \\ 1 & 10 - \lambda \end{array}\right], \quad \quad \begin{aligned} \operatorname{det}\left(M^{\top} M-\lambda I\right) &=\left(10-\lambda)^{2}-1\right.\\ \lambda &=11 \quad or \quad 9 \end{aligned}

So the eigenvalues of MMM^{\top} M are 11 and 9.

Step 3: Calculate the eigenvectors

After the calculation of eigenvalues, the next step is to calculate the eigenvectors for each eigenvalue. First we plug the eigenvalues sequently as the value of λ\lambda into MMλIM^{\top} M - \lambda I to get a matrix. To obtain eigenvectors, we also have the know the null space of this matrix. Therefore, we set a vector as AA and calculate AA through the equation (MMλI)A=0(M^{\top} M - \lambda I)*A=0. After that, we convert the vector into an unit vector, and this unit vector is the eigenvector.

Forλ=9,MM9I=[1111]For \quad \lambda = 9, \quad M^{\top} M - 9I=\left[\begin{array}{ll} 1 & 1 \\ 1 & 1 \end{array}\right]

(MM9I)A=[1111][a1a2],[a1a2]=[11]\begin{gathered} \left(M^{\top} M-9 I\right) * A=\left[\begin{array}{ll} 1 & 1 \\ 1 & 1 \end{array}\right] \cdot\left[\begin{array}{l} a_{1} \\ a_{2} \end{array}\right], \quad {\left[\begin{array}{l} a_{1} \\ a_{2} \end{array}\right]=\left[\begin{array}{c} 1 \\ -1 \end{array}\right]} \end{gathered}

a12+a22=2, eigenvector: [a1/2a2/2]=[1/21/2]\sqrt{a_{1}{ }^{2}+a_{2}{ }^{2}}=\sqrt{2}, \quad \text { eigenvector: }\left[\begin{array}{l} a_{1} / \sqrt{2} \\ a_{2} / \sqrt{2} \end{array}\right]=\left[\begin{array}{c} 1 / \sqrt{2} \\ -1 / \sqrt{2} \end{array}\right]

\\

Forλ=10,MM10I=[1111]For \quad \lambda = 10, \quad M^{\top} M - 10I=\left[\begin{array}{ll} -1 & 1 \\ 1 & -1 \end{array}\right]

(MM10I)A=[1111][a1a2],[a1a2]=[11]\begin{gathered} \left(M^{\top} M-10 I\right) * A=\left[\begin{array}{ll} -1 & 1 \\ 1 & -1 \end{array}\right] \cdot\left[\begin{array}{l} a_{1} \\ a_{2} \end{array}\right], \quad {\left[\begin{array}{l} a_{1} \\ a_{2} \end{array}\right]=\left[\begin{array}{c} 1 \\ 1 \end{array}\right]} \end{gathered}

a12+a22=2, eigenvector: [a1/2a2/2]=[1/21/2]\sqrt{a_{1}{ }^{2}+a_{2}{ }^{2}}=\sqrt{2}, \quad \text { eigenvector: }\left[\begin{array}{l} a_{1} / \sqrt{2} \\ a_{2} / \sqrt{2} \end{array}\right]=\left[\begin{array}{c} 1 / \sqrt{2} \\ 1 / \sqrt{2} \end{array}\right]

Step 4: Construct Σ\Sigma (diagonal matrix) and V (right singular vectors)

For Σ\Sigma, the diagonal elements are the square root of eigenvalues and the rest are all zero. For V, it consists of eigenvectors and each eigenvector represents one column of data in the matrix.

V=[1/21/21/21/2],D=[11009]V=\left[\begin{array}{cc} 1 / \sqrt{2} & -1 / \sqrt{2} \\ 1 / \sqrt{2} & 1 / \sqrt{2} \end{array}\right], \quad D=\left[\begin{array}{cc} \sqrt{11} & 0 \\ 0 & \sqrt{9} \end{array}\right]

Step 5: Calculate U (left singular vectors)

From M=UΣVTM=U \Sigma V^T, we get U=MVΣTU=MV \Sigma^T. First we compute MVMV and change the vectors in this matrix into unit vector. Then we compute MVΣTMV \Sigma^T. After this computation, we make a unit vector transformation again, and this is the result of UU.

MV=[110330][1/21/21/21/2]=[22032323232]MV = \left[\begin{array}{ll} 1 & 1 \\ 0 & 3 \\ 3 & 0 \end{array}\right] \cdot\left[\begin{array}{l} 1/\sqrt{2} & -1/\sqrt{2} \\ 1/\sqrt{2} & 1/\sqrt{2} \end{array}\right] = \left[\begin{array}{l} \frac{2}{\sqrt{2}} & 0 \\ \frac{3}{\sqrt{2}} & \frac{3}{\sqrt{2}} \\ \frac{3}{\sqrt{2}} & -\frac{3}{\sqrt{2}} \end{array}\right]

(22)2+(32)2+(32)2=222,02+(32)2+(32)2=182\sqrt{(\frac{2}{\sqrt{2}})^2+(\frac{3}{\sqrt{2}})^2+(\frac{3}{\sqrt{2}})^2}=\frac{\sqrt{22}}{\sqrt{2}}, \quad \sqrt{0^2+(\frac{3}{\sqrt{2}})^2+(\frac{3}{\sqrt{2}})^2}=\frac{\sqrt{18}}{\sqrt{2}}

MV=[22222021832222322183222232218]=[2220322318322318]MV=\left[\begin{array}{l} \frac{2}{\sqrt{2}} \cdot \frac{\sqrt{2}}{\sqrt{22}} & 0 \cdot \frac{\sqrt{2}}{\sqrt{18}} \\ \frac{3}{\sqrt{2}} \cdot \frac{\sqrt{2}}{\sqrt{22}} & \frac{3}{\sqrt{2}} \cdot \frac{\sqrt{2}}{\sqrt{18}}\\ \frac{3}{\sqrt{2}} \cdot \frac{\sqrt{2}}{\sqrt{22}} & -\frac{3}{\sqrt{2}} \cdot \frac{\sqrt{2}}{\sqrt{18}} \end{array}\right] = \left[\begin{array}{l} \frac{2}{\sqrt{22}} & 0 \\ \frac{3}{\sqrt{22}} & \frac{3}{\sqrt{18}} \\ \frac{3}{\sqrt{22}} & -\frac{3}{\sqrt{18}} \end{array}\right]

MVDT=[2220322318322318][11009]=[211220311223918311223918]=[22032323232]MVD^T = \left[\begin{array}{l} \frac{2}{\sqrt{22}} & 0 \\ \frac{3}{\sqrt{22}} & \frac{3}{\sqrt{18}} \\ \frac{3}{\sqrt{22}} & -\frac{3}{\sqrt{18}} \end{array}\right] \cdot \left[\begin{array}{cc} \sqrt{11} & 0 \\ 0 & \sqrt{9} \end{array}\right] = \left[\begin{array}{l} \frac{2\sqrt{11}}{\sqrt{22}} & 0 \\ \frac{3\sqrt{11}}{\sqrt{22}} & \frac{3\sqrt{9}}{\sqrt{18}} \\ \frac{3\sqrt{11}}{\sqrt{22}} & -\frac{3\sqrt{9}}{\sqrt{18}} \end{array}\right] = \left[\begin{array}{l} \frac{2}{\sqrt{2}} & 0 \\ \frac{3}{\sqrt{2}} & \frac{3}{\sqrt{2}} \\ \frac{3}{\sqrt{2}} & -\frac{3}{\sqrt{2}} \end{array}\right]

(22)2+(32)2+(32)2=222,02+(32)2+(32)2=182\sqrt{(\frac{2}{\sqrt{2}})^2+(\frac{3}{\sqrt{2}})^2+(\frac{3}{\sqrt{2}})^2}=\frac{\sqrt{22}}{\sqrt{2}}, \quad \sqrt{0^2+(\frac{3}{\sqrt{2}})^2+(\frac{3}{\sqrt{2}})^2}=\frac{\sqrt{18}}{\sqrt{2}}

MVΣT=[22222021832222322183222232218]=[2220322318322318]=UMV\Sigma^T = \left[\begin{array}{l} \frac{2}{\sqrt{2}} \cdot \frac{\sqrt{2}}{\sqrt{22}} & 0 \cdot \frac{\sqrt{2}}{\sqrt{18}} \\ \frac{3}{\sqrt{2}} \cdot \frac{\sqrt{2}}{\sqrt{22}} & \frac{3}{\sqrt{2}} \cdot \frac{\sqrt{2}}{\sqrt{18}}\\ \frac{3}{\sqrt{2}} \cdot \frac{\sqrt{2}}{\sqrt{22}} & -\frac{3}{\sqrt{2}} \cdot \frac{\sqrt{2}}{\sqrt{18}} \end{array}\right] = \left[\begin{array}{l} \frac{2}{\sqrt{22}} & 0 \\ \frac{3}{\sqrt{22}} & \frac{3}{\sqrt{18}} \\ \frac{3}{\sqrt{22}} & -\frac{3}{\sqrt{18}} \end{array}\right] = U

*The 5 steps above is the full procedure of SVD. After all UU, Σ\Sigma, VV are obtain, we should compute UΣVTU\Sigma V^T to check if the result is equal to MM. This is to verify the SVD we make is right.


All articles in this blog adopt the CC BY-SA 4.0 agreement except for special statements. Please indicate the source for reprinting!