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)
Warning: package 'HSAUR2' was built under R version 4.4.3
Loading required package: tools
library(tidyverse)
Warning: package 'ggplot2' was built under R version 4.4.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.2     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(magick)
Warning: package 'magick' was built under R version 4.4.3
Linking to ImageMagick 6.9.13.29
Enabled features: cairo, freetype, fftw, ghostscript, heic, lcms, pango, raw, rsvg, webp
Disabled features: fontconfig, x11
data(USairpollution)
USairpollution<- scale(USairpollution)

A)

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

matrix D looks like a list of eigen values, it is worth noting that the numbers get smaller as you go from left to right.

components <- scale(USairpollution) %>% as.matrix() %>%   svd()

u <- components$u
d<-components$d# creation of matrix D.
v <- components$v

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.

recombined <-(u %*% diag(d) %*% t(v)) %>% round(1)

plot(USairpollution %>% as.matrix() 
, recombined); 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?

using k = 4 is plenty enough to get to a value of > .9

k <- 4

Xtilde <- u[,1:k] %*% diag(d[1:k]) %*% t(v[,1:k])# %>% round(1)

plot(USairpollution %>% as.matrix() 
, Xtilde); abline(0,1)

cor(as.vector(USairpollution %>% as.matrix()), as.vector(Xtilde))
[1] 0.9656544

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)\)
covariance <-cov(USairpollution) %>%  as.matrix()
eggy <- eigen(covariance) 
vector  <-eggy$vectors 

## shows how vector and my svd v are the same.
vector 
              [,1]        [,2]       [,3]        [,4]       [,5]        [,6]
[1,]  0.4896988171 -0.08457563 -0.0143502  0.40421007  0.7303942 -0.18334573
[2,] -0.3153706901  0.08863789 -0.6771362 -0.18522794  0.1624652 -0.61066107
[3,]  0.5411687028  0.22588109 -0.2671591 -0.02627237 -0.1641011  0.04273352
[4,]  0.4875881115  0.28200380 -0.3448380 -0.11340377 -0.3491048  0.08786327
[5,]  0.2498749284 -0.05547149  0.3112655 -0.86190131  0.2682549 -0.15005378
[6,]  0.0001873122 -0.62587937 -0.4920363 -0.18393719  0.1605988  0.55357384
[7,]  0.2601790729 -0.67796741  0.1095789  0.10976070 -0.4399698 -0.50494668
             [,7]
[1,] -0.149529278
[2,]  0.023664113
[3,]  0.745180920
[4,] -0.649125507
[5,] -0.015765377
[6,]  0.010315309
[7,] -0.008217393
v
              [,1]        [,2]       [,3]        [,4]       [,5]        [,6]
[1,] -0.4896988171 -0.08457563 -0.0143502  0.40421007 -0.7303942 -0.18334573
[2,]  0.3153706901  0.08863789 -0.6771362 -0.18522794 -0.1624652 -0.61066107
[3,] -0.5411687028  0.22588109 -0.2671591 -0.02627237  0.1641011  0.04273352
[4,] -0.4875881115  0.28200380 -0.3448380 -0.11340377  0.3491048  0.08786327
[5,] -0.2498749284 -0.05547149  0.3112655 -0.86190131 -0.2682549 -0.15005378
[6,] -0.0001873122 -0.62587937 -0.4920363 -0.18393719 -0.1605988  0.55357384
[7,] -0.2601790729 -0.67796741  0.1095789  0.10976070  0.4399698 -0.50494668
             [,7]
[1,] -0.149529278
[2,]  0.023664113
[3,]  0.745180920
[4,] -0.649125507
[5,] -0.015765377
[6,]  0.010315309
[7,] -0.008217393
## shows how vector and my svd v are the same.
eggy$values
[1] 2.72811968 1.51233485 1.39497299 0.89199129 0.34677866 0.10028759 0.02551493
d^2 / (nrow(USairpollution) - 1)
[1] 2.72811968 1.51233485 1.39497299 0.89199129 0.34677866 0.10028759 0.02551493

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

this image is dogwash, no seriously it looks like an image of the aftermath of bathing a dirty dog… muddy and murky.

mystery_man <-(mysteryU4 %*% diag(mysteryD4) %*% t(mysteryV4)) %>% round(1)

R <- max(mystery_man)-min(mystery_man)
recomposed_mystery <- (mystery_man-min(mystery_man))/R


(mystery_incarnate <- recomposed_mystery
     %>% as.raster
  %>% image_read
)

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?

k = 10 I can tell that it’s likely a person now. nothing more.

mystery_man10 <-(mysteryU10 %*% diag(mysteryD10) %*% t(mysteryV10)) %>% round(1)

R <- max(mystery_man10)-min(mystery_man10)
recomposed_mystery10 <- (mystery_man10-min(mystery_man10))/R


(mystery_incarnate10 <- recomposed_mystery10
     %>% as.raster
  %>% image_read
)

K = 50 EHHHHHHHHHH! IT’S DR IVERSON!

mystery_man50 <-(mysteryU50 %*% diag(mysteryD50) %*% t(mysteryV50)) %>% round(1)

R <- max(mystery_man50)-min(mystery_man50)
recomposed_mystery50 <- (mystery_man50-min(mystery_man50))/R


(mystery_incarnate50 <- recomposed_mystery50
     %>% as.raster
  %>% image_read
)

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? 2800+2400+4 = 5204
  • A 10-dimensional approximation? 10+7000+6000= 13010
  • A 50-dimensional approximation? 30000+35000+50= 65010