D is a 7*7 diagonal matrix with those singular values on the diagonal in descending order, zeros elsewhere. the values are related to the amount each component captures.
The first singular value dominates the decomposition: highest variance.
B)
Verify that \(X=UDV^T\) by plotting all the entries of \(X\) versus all the entries of \(UDV^T\) with the 0/1 line.
k <-3Xtilde3 <- U[,1:k] %*%diag(D[1:k]) %*%t(V[,1:k]) %>%round(1)
plot(X, Xtilde2); abline(0,1)
plot(X, Xtilde3); abline(0,1)
C)
Consider low-dimensional approximations of the data matrix. What is the fewest number of dimensions required to yield a correlation between the entries of \(X\) and \(\tilde X\) of at least 0.9?
cor(as.vector(X), as.vector(Xtilde2))
[1] 0.9965504
cor(as.vector(X), as.vector(Xtilde3))
[1] 0.9997697
The 2-dimensional approximation has a correlation of 0.997 with the original data, and the 3-dimensional approximation has 0.9998. Since both are above 0.9, 2 dimensions are enough.
D)
Find \(\Sigma\), the covariance matrix of this data set. Then perform eigen-decomposition of this matrix. Verify that
The eigenvectors of \(\Sigma\) equal the columns of \(V\)
The eigenvalues of \(\Sigma\) equal the diagonals of \(D^2/(n-1)\)
In this problem we explore how “high” a low-dimensional SVD approximation of an image has to be before you can recognize it.
.Rdata objects are essentially R workspace memory snapshots that, when loaded, load any type of R object that you want into your global environment. The command below, when executed, will load three objects into your memory: mysteryU4, mysteryD4, and mysteryV4. These are the first \(k\) vectors and singular values of an SVD I performed on a 700-pixels-tall \(\times\) 600-pixels-wide image of a well-known villain.
library(magick)
Warning: package 'magick' was built under R version 4.4.3
Write a function that takes SVD ingredients u, d and v and renders the \(700 \times 600\) image produced by this approximation using functions from the magick package. Use your function to determine whether a 4-dimensional approximation to this image is enough for you to tell who the mystery villain is. Recall that you will likely need to rescale your recomposed approximation so that all pixels are in [0,1].