Activity 3.1 - SVD

SUBMISSION INSTRUCTIONS

  1. Render to html
  2. Publish your html to RPubs
  1. Submit a link to your published solutions

Problem 1

Reconsider the US air pollution data set:

library(HSAUR2)
Loading required package: tools
data(USairpollution)

A)

Perform singular value decomposition of this data matrix. Then create the matrix \(D\). Describe what this matrix looks like.

X <- as.matrix(USairpollution)

svd_result <- svd(X)
U <- svd_result$u
d <- svd_result$d
V <- svd_result$v

D <- diag(d)

The matrix of D is a diagonal matrix. This means that everything in the matrix is a 0 except for the diagonal points.

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.

X_reconstructed <- U %*% D %*% t(V)

plot(as.vector(X), as.vector(X_reconstructed),
     xlab = "Original X",
     ylab = "Reconstructed X",
     main = "Verification of X = UDV",
     pch = 19, col = "blue")

abline(0, 1, col = "red", lwd = 2)

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?

components <- svd(X)
U <- components$u
D <- components$d
V <- components$v

r <- length(D)

cor_vals <- numeric(r)

for (k in 1:r) {
  U_k <- U[, 1:k, drop = FALSE]
  D_k <- diag(D[1:k], nrow = k, ncol = k)
  V_k <- V[, 1:k, drop = FALSE]
  
  Xk <- U_k %*% D_k %*% t(V_k)
  cor_vals[k] <- cor(as.vector(X), as.vector(Xk))
}

cor_vals
[1] 0.9863996 0.9965504 0.9997697 0.9998753 0.9999643 0.9999987 1.0000000

The minimum k value for the correlation to be above .9 is 1.

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)\)
X_scaled <- scale(USairpollution, center = TRUE, scale = FALSE)
svdX <- svd(X_scaled)
eigenSigma <- eigen(cov(X_scaled))

eigenSigma$vectors
              [,1]         [,2]         [,3]        [,4]         [,5]
[1,]  0.0168607518 -0.099835625  0.208775573  0.95883106 -0.152191203
[2,] -0.0011417794  0.025814390 -0.071600745 -0.11014784 -0.477854201
[3,]  0.6968327936 -0.710249079 -0.067182201 -0.07319788 -0.009643654
[4,]  0.7170284512  0.692912523  0.056666935  0.04906669  0.010735457
[5,]  0.0004067530 -0.001011680  0.005386606 -0.01506609  0.025401917
[6,] -0.0004336922  0.001225937  0.265807619 -0.16261712 -0.832729325
[7,]  0.0028836950 -0.069155051  0.934279828 -0.18459052  0.232812295
             [,6]          [,7]
[1,] -0.053952911 -2.704138e-02
[2,] -0.852534945 -1.640507e-01
[3,] -0.002153023  1.136208e-03
[4,]  0.002915751 -5.682006e-05
[5,]  0.176541256 -9.838347e-01
[6,]  0.453206155  6.376788e-02
[7,] -0.183568735 -1.891454e-02
svdX$v
              [,1]         [,2]         [,3]        [,4]         [,5]
[1,] -0.0168607518  0.099835625  0.208775573 -0.95883106  0.152191203
[2,]  0.0011417794 -0.025814390 -0.071600745  0.11014784  0.477854201
[3,] -0.6968327936  0.710249079 -0.067182201  0.07319788  0.009643654
[4,] -0.7170284512 -0.692912523  0.056666935 -0.04906669 -0.010735457
[5,] -0.0004067530  0.001011680  0.005386606  0.01506609 -0.025401917
[6,]  0.0004336922 -0.001225937  0.265807619  0.16261712  0.832729325
[7,] -0.0028836950  0.069155051  0.934279828  0.18459052 -0.232812295
             [,6]          [,7]
[1,] -0.053952911 -2.704138e-02
[2,] -0.852534945 -1.640507e-01
[3,] -0.002153023  1.136208e-03
[4,]  0.002915751 -5.682006e-05
[5,]  0.176541256 -9.838347e-01
[6,]  0.453206155  6.376788e-02
[7,] -0.183568735 -1.891454e-02
eigenSigma$values
[1] 6.384720e+05 1.481204e+04 7.019599e+02 2.050019e+02 1.167047e+02
[6] 1.205705e+01 1.448704e+00
svdX$d^2 / (nrow(X_scaled) - 1)
[1] 6.384720e+05 1.481204e+04 7.019599e+02 2.050019e+02 1.167047e+02
[6] 1.205705e+01 1.448704e+00

Problem 2

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.

load('Data/mystery_person_k4.Rdata')

A)

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].

library(magick)
Linking to ImageMagick 6.9.13.29
Enabled features: cairo, fontconfig, freetype, heic, lcms, pango, raw, rsvg, webp
Disabled features: fftw, ghostscript, x11
show_svd_image <- function(u, d, v) {
  img_mat <- u %*% diag(d) %*% t(v)
  
  img_mat <- (img_mat - min(img_mat)) / (max(img_mat) - min(img_mat))
  
  img <- image_read(as.raster(img_mat))
  
  print(img)
  
  invisible(list(matrix = img_mat, image = img))
}

show_svd_image(mysteryU4, mysteryD4, mysteryV4)
  format width height colorspace matte filesize density
1    PNG   600    700       sRGB  TRUE        0   72x72

B)

I’m giving you slightly higher-dimensional approximations (\(k=10\) and \(k=50\), respectively) in the objects below:

load('Data/mystery_person_k10.Rdata')
load('Data/mystery_person_k50.Rdata')

Create both of the images produced by these approximations. At what point can you tell who the mystery villain is?

show_svd_image <- function(u, d, v, k_label) {
  img_mat <- u %*% diag(d) %*% t(v)
  img_mat <- (img_mat - min(img_mat)) / (max(img_mat) - min(img_mat))
  img <- image_read(as.raster(img_mat))
}

img10 <- show_svd_image(mysteryU10, mysteryD10, mysteryV10, 10)
img50 <- show_svd_image(mysteryU50, mysteryD50, mysteryV50, 50)

img10

img50

The image become clear after k = 50. The image is Todd Iverson.

C)

How many numbers need to be stored in memory for each of the following:

  • A full \(700\times 600\) image? 420000
  • A 4-dimensional approximation? (700 * 4) + (600 * 4) + 4^2 = 5216
  • A 10-dimensional approximation? (700 * 10) + (600 * 10) + 10^2 = 13100
  • A 50-dimensional approximation? (700 * 50) + (600 * 50) + 50^2 = 67500