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.

library(tidyverse)
── 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   4.0.0     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.1.0     
── 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
head(USairpollution)
            SO2 temp manu popul wind precip predays
Albany       46 47.6   44   116  8.8  33.36     135
Albuquerque  11 56.8   46   244  8.9   7.77      58
Atlanta      24 61.5  368   497  9.1  48.34     115
Baltimore    47 55.0  625   905  9.6  41.31     111
Buffalo      11 47.1  391   463 12.4  36.11     166
Charleston   31 55.2   35    71  6.5  40.75     148
components <- svd(USairpollution)


U <- components$u
U %>% round(2)
       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]
 [1,] -0.02  0.10  0.21 -0.25  0.01 -0.07 -0.04
 [2,] -0.03  0.15  0.00  0.04 -0.19 -0.40  0.03
 [3,] -0.09  0.08  0.11  0.11 -0.06  0.13 -0.02
 [4,] -0.16  0.13 -0.02 -0.19 -0.11  0.10  0.07
 [5,] -0.09  0.06  0.20  0.11  0.44 -0.11  0.15
 [6,] -0.01  0.09  0.25 -0.05  0.09 -0.04 -0.37
 [7,] -0.67 -0.44 -0.20  0.01 -0.04  0.05 -0.08
 [8,] -0.09 -0.01  0.19  0.16  0.12 -0.02 -0.28
 [9,] -0.18 -0.24  0.32  0.03  0.08 -0.12 -0.01
[10,] -0.08  0.19  0.05 -0.14  0.14 -0.01 -0.10
[11,] -0.15  0.07 -0.04  0.29 -0.13 -0.03  0.16
[12,] -0.10  0.01  0.07  0.13 -0.01 -0.35 -0.03
[13,] -0.03  0.10  0.13  0.04  0.02 -0.07  0.28
[14,] -0.26  0.17 -0.17 -0.17  0.21 -0.06  0.00
[15,] -0.06 -0.15  0.35  0.02 -0.12  0.08  0.03
[16,] -0.20  0.26 -0.19  0.10 -0.01  0.14  0.09
[17,] -0.11  0.24 -0.05 -0.19  0.07  0.05  0.07
[18,] -0.07  0.28 -0.02  0.01 -0.07  0.21 -0.08
[19,] -0.09  0.07  0.07  0.16  0.00  0.02  0.14
[20,] -0.02  0.07  0.17  0.22 -0.12  0.15 -0.03
[21,] -0.09  0.20  0.02 -0.14  0.01  0.09 -0.09
[22,] -0.10  0.18  0.00  0.13 -0.02  0.18  0.03
[23,] -0.06  0.12  0.16  0.29 -0.06  0.18 -0.17
[24,] -0.13  0.06  0.06  0.07  0.24 -0.10  0.24
[25,] -0.15 -0.02  0.13  0.03  0.26 -0.17  0.07
[26,] -0.08  0.13  0.09  0.09  0.01  0.10 -0.14
[27,] -0.06  0.13  0.11  0.25 -0.07  0.22 -0.10
[28,] -0.04  0.18  0.10 -0.09 -0.10  0.09  0.15
[29,] -0.06  0.12  0.07  0.05  0.01 -0.08  0.24
[30,] -0.37 -0.05 -0.14 -0.13 -0.07  0.10 -0.02
[31,] -0.08  0.22 -0.15  0.06 -0.37 -0.44 -0.32
[32,] -0.09  0.12  0.13 -0.38  0.00 -0.04 -0.06
[33,] -0.05 -0.09  0.32 -0.39 -0.38  0.10  0.21
[34,] -0.05  0.09  0.14  0.04 -0.06  0.06 -0.15
[35,] -0.03  0.05  0.14 -0.02 -0.10 -0.31 -0.02
[36,] -0.12  0.13 -0.08  0.10 -0.12 -0.19  0.02
[37,] -0.09  0.11  0.16 -0.08  0.29 -0.06 -0.15
[38,] -0.14 -0.16  0.21  0.08 -0.21 -0.03  0.02
[39,] -0.12  0.19 -0.03 -0.10 -0.03  0.04  0.01
[40,] -0.04  0.12  0.06  0.15 -0.09 -0.08  0.43
[41,] -0.02  0.05  0.22 -0.03 -0.12  0.04  0.03
head(U)
            [,1]       [,2]          [,3]        [,4]         [,5]        [,6]
[1,] -0.01841623 0.10181630  0.2091659528 -0.25110469  0.006626339 -0.06864647
[2,] -0.03130764 0.14812091 -0.0004043841  0.04012803 -0.190598264 -0.40490988
[3,] -0.08887282 0.08371317  0.1077274000  0.11286840 -0.060174208  0.13096949
[4,] -0.15620922 0.13142656 -0.0218222317 -0.18626470 -0.107291147  0.10220939
[5,] -0.08773289 0.05962107  0.1957870856  0.10730132  0.441622268 -0.10577782
[6,] -0.01291040 0.08879269  0.2527249241 -0.05135988  0.090737537 -0.03524838
            [,7]
[1,] -0.03732041
[2,]  0.03112816
[3,] -0.02364724
[4,]  0.07385071
[5,]  0.14913872
[6,] -0.37039600
D <- components$d
D %>% round(2)
[1] 7051.95  931.12  540.46   92.71   85.24   52.95   10.14
head(D)
[1] 7051.94936  931.12109  540.46297   92.70909   85.23724   52.94650
V <- components$v
V %>% round(2)
      [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]
[1,] -0.03  0.00  0.21 -0.82 -0.54  0.03  0.01
[2,] -0.03  0.20  0.30  0.50 -0.67 -0.39 -0.11
[3,] -0.65 -0.71  0.26  0.09  0.00 -0.01  0.00
[4,] -0.75  0.57 -0.33 -0.07  0.01  0.01  0.00
[5,] -0.01  0.03  0.05  0.04 -0.04 -0.11  0.99
[6,] -0.02  0.13  0.24  0.24 -0.22  0.91  0.06
[7,] -0.07  0.34  0.79 -0.11  0.47 -0.12 -0.04
#Specifically looking at the matrix D after the decomposition   
D
[1] 7051.94936  931.12109  540.46297   92.70909   85.23724   52.94650   10.14090
#The component matrix of D has 7 columns of numerical values with only 1 row 
#by implementing the component that basically takes the diagonal values of 
#7x7 matrix (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.

#Slides with the 

USairpollution
               SO2 temp manu popul wind precip predays
Albany          46 47.6   44   116  8.8  33.36     135
Albuquerque     11 56.8   46   244  8.9   7.77      58
Atlanta         24 61.5  368   497  9.1  48.34     115
Baltimore       47 55.0  625   905  9.6  41.31     111
Buffalo         11 47.1  391   463 12.4  36.11     166
Charleston      31 55.2   35    71  6.5  40.75     148
Chicago        110 50.6 3344  3369 10.4  34.44     122
Cincinnati      23 54.0  462   453  7.1  39.04     132
Cleveland       65 49.7 1007   751 10.9  34.99     155
Columbus        26 51.5  266   540  8.6  37.01     134
Dallas           9 66.2  641   844 10.9  35.94      78
Denver          17 51.9  454   515  9.0  12.95      86
Des Moines      17 49.0  104   201 11.2  30.85     103
Detroit         35 49.9 1064  1513 10.1  30.96     129
Hartford        56 49.1  412   158  9.0  43.37     127
Houston         10 68.9  721  1233 10.8  48.19     103
Indianapolis    28 52.3  361   746  9.7  38.74     121
Jacksonville    14 68.4  136   529  8.8  54.47     116
Kansas City     14 54.5  381   507 10.0  37.00      99
Little Rock     13 61.0   91   132  8.2  48.52     100
Louisville      30 55.6  291   593  8.3  43.11     123
Memphis         10 61.6  337   624  9.2  49.10     105
Miami           10 75.5  207   335  9.0  59.80     128
Milwaukee       16 45.7  569   717 11.8  29.07     123
Minneapolis     29 43.5  699   744 10.6  25.94     137
Nashville       18 59.4  275   448  7.9  46.00     119
New Orleans      9 68.3  204   361  8.4  56.77     113
Norfolk         31 59.3   96   308 10.6  44.68     116
Omaha           14 51.5  181   347 10.9  30.18      98
Philadelphia    69 54.6 1692  1950  9.6  39.93     115
Phoenix         10 70.3  213   582  6.0   7.05      36
Pittsburgh      61 50.4  347   520  9.4  36.22     147
Providence      94 50.0  343   179 10.6  42.75     125
Richmond        26 57.8  197   299  7.6  42.59     115
Salt Lake City  28 51.0  137   176  8.7  15.17      89
San Francisco   12 56.7  453   716  8.7  20.66      67
Seattle         29 51.1  379   531  9.4  38.79     164
St. Louis       56 55.9  775   622  9.5  35.89     105
Washington      29 57.3  434   757  9.3  38.89     111
Wichita          8 56.6  125   277 12.7  30.58      82
Wilmington      36 54.0   80    80  9.0  40.25     114
X <- (U %*% diag(D) %*% t(V) %>% round(1))
head(X) 
     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]   46 47.6   44  116  8.8 33.4  135
[2,]   11 56.8   46  244  8.9  7.8   58
[3,]   24 61.5  368  497  9.1 48.3  115
[4,]   47 55.0  625  905  9.6 41.3  111
[5,]   11 47.1  391  463 12.4 36.1  166
[6,]   31 55.2   35   71  6.5 40.7  148
k <-2
#k=2
(X_tilde2 <- U[,1:k] %*% diag(D[1:k]) %*% t(V[,1:k]))%>%round(1)
       [,1]  [,2]   [,3]   [,4] [,5] [,6]  [,7]
 [1,]   3.5  23.8   17.2  151.5  3.6 15.2  42.1
 [2,]   5.9  35.8   45.7  244.5  5.5 22.8  63.7
 [3,]  17.3  37.8  352.3  516.8  6.2 24.6  73.1
 [4,]  30.4  63.4  629.6  900.0 10.5 41.3 123.5
 [5,]  17.1  32.9  363.0  498.0  5.5 21.5  64.8
 [6,]   2.4  20.0    0.5  115.4  3.0 12.7  35.1
 [7,] 131.5  80.9 3371.0 3334.4 17.1 56.6 208.0
 [8,]  18.2  20.7  433.8  488.4  3.8 13.8  44.9
 [9,]  34.5  -2.1  962.8  807.5  1.1  0.1  15.2
[10,]  16.2  56.9  260.0  548.2  9.0 36.6 104.9
[11,]  29.3  50.8  643.7  839.4  8.6 33.3 101.6
[12,]  19.2  26.4  443.0  528.5  4.6 17.4  54.9
[13,]   6.3  26.2   86.1  223.8  4.1 16.8  47.7
[14,]  50.9  96.7 1088.6 1481.9 16.2 63.2 190.8
[15,]  11.3 -15.3  362.9  221.1 -1.8 -9.2 -19.7
[16,]  38.8  98.3  746.5 1199.8 16.0 63.6 186.9
[17,]  22.1  73.6  368.8  736.7 11.7 47.4 136.3
[18,]  13.5  71.1  138.3  526.0 11.0 45.4 127.3
[19,]  17.7  35.8  369.9  520.6  6.0 23.4  70.1
[20,]   4.6  18.8   65.6  163.8  2.9 12.1  34.2
[21,]  17.7  60.9  289.1  595.9  9.6 39.2 112.5
[22,]  19.2  58.8  336.5  624.1  9.4 37.9 109.7
[23,]  11.0  36.3  183.1  364.5  5.8 23.4  67.3
[24,]  25.5  43.8  559.9  728.1  7.4 28.7  87.7
[25,]  28.5  32.6  680.9  766.8  5.9 21.8  70.5
[26,]  14.5  42.3  261.2  465.3  6.8 27.3  79.3
[27,]  11.4  38.9  186.7  382.2  6.2 25.0  71.8
[28,]   8.3  44.1   83.6  324.4  6.8 28.2  78.9
[29,]  10.6  37.2  171.2  359.4  5.9 23.9  68.6
[30,]  71.6  81.4 1712.1 1924.8 14.8 54.4 176.3
[31,]  15.9  62.2  233.5  555.9  9.8 39.9 113.6
[32,]  17.4  45.1  331.6  541.4  7.3 29.2  85.6
[33,]  10.4  -4.0  301.8  233.9 -0.2 -2.1  -1.1
[34,]  10.0  30.3  177.0  324.6  4.8 19.5  56.6
[35,]   6.4  17.3  118.4  200.1  2.8 11.2  32.7
[36,]  23.2  53.9  462.5  703.5  8.8 35.0 103.6
[37,]  18.2  44.1  358.0  558.0  7.2 28.6  84.3
[38,]  27.5   4.6  745.0  660.4  1.8  4.0  22.6
[39,]  23.7  65.8  438.8  751.3 10.6 42.5 123.9
[40,]   8.1  33.2  115.3  288.9  5.2 21.3  60.5
[41,]   3.4  13.3   49.9  118.9  2.1  8.5  24.3
k <- 3 
(X_tilde3<- U[,1:k] %*% diag(D[1:k]) %*% t(V[,1:k]))%>%round(1)
       [,1] [,2]   [,3]   [,4] [,5] [,6]  [,7]
 [1,]  27.4 58.3   46.0  114.5  9.8 42.3 131.8
 [2,]   5.9 35.7   45.6  244.5  5.5 22.8  63.5
 [3,]  29.6 55.5  367.1  497.7  9.4 38.5 119.3
 [4,]  27.9 59.8  626.6  903.9  9.9 38.5 114.1
 [5,]  39.5 65.1  390.0  463.4 11.3 46.8 148.8
 [6,]  31.3 61.6   35.4   70.6 10.5 45.5 143.5
 [7,] 109.1 48.7 3343.9 3369.1 11.3 31.2 123.9
 [8,]  40.5 52.8  460.7  453.9  9.5 39.1 128.4
 [9,]  70.9 50.2 1006.7  751.2 10.5 41.3 151.5
[10,]  22.1 65.4  267.1  539.1 10.5 43.3 127.0
[11,]  25.2 44.7  638.7  845.9  7.5 28.5  85.9
[12,]  27.3 38.0  452.8  515.9  6.7 26.6  85.4
[13,]  20.9 47.2  103.7  201.2  7.9 33.3 102.4
[14,]  31.6 68.9 1065.3 1511.8 11.2 41.3 118.5
[15,]  51.9 43.1  411.9  158.2  8.7 36.8 132.5
[16,]  16.9 66.9  720.2 1233.6 10.3 39.0 105.2
[17,]  16.9 66.1  362.5  744.8 10.3 41.5 116.8
[18,]  11.6 68.3  136.0  529.1 10.5 43.2 120.0
[19,]  25.8 47.6  379.7  508.0  8.1 32.6 100.7
[20,]  24.3 47.0   89.3  133.4  8.0 34.3 107.8
[21,]  20.2 64.5  292.1  592.1 10.3 42.0 121.7
[22,]  18.8 58.2  336.0  624.8  9.3 37.4 108.1
[23,]  28.9 62.1  204.7  336.8 10.4 43.6 134.4
[24,]  32.4 53.8  568.3  717.3  9.2 36.6 113.8
[25,]  43.2 53.8  698.7  744.0  9.7 38.4 125.7
[26,]  25.3 57.9  274.3  448.5  9.6 39.6 119.9
[27,]  24.2 57.2  202.1  362.5  9.5 39.4 119.6
[28,]  19.2 59.8   96.8  307.5  9.6 40.5 119.8
[29,]  18.4 48.4  180.6  347.3  7.9 32.7  97.7
[30,]  55.9 58.7 1693.1 1949.2 10.7 36.5 117.2
[31,]  -1.5 37.2  212.5  582.8  5.2 20.2  48.4
[32,]  32.7 67.1  350.0  517.7 11.3 46.5 143.0
[33,]  47.3 49.0  346.3  176.9  9.4 39.6 137.0
[34,]  26.4 53.8  196.7  299.3  9.1 38.1 117.9
[35,]  21.9 39.7  137.1  176.0  6.8 28.8  91.0
[36,]  14.6 41.6  452.2  716.8  6.6 25.3  71.4
[37,]  36.1 69.8  379.6  530.3 11.8 48.8 151.3
[38,]  51.9 39.7  774.4  622.6  8.1 31.7 114.1
[39,]  20.4 61.0  434.8  756.4  9.7 38.7 111.4
[40,]  15.2 43.3  123.8  278.0  7.0 29.2  86.8
[41,]  28.6 49.5   80.3   79.9  8.6 37.0 118.6
#plotting the entrie '0,1' abline for k =2

plot(X,X_tilde2);abline(0,1)

#plotting the '0,1' abline for k=3

plot(X,X_tilde3);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?

#Checking the correlation of the dim. approx. with k=2 

cor(as.vector(X),as.vector(X_tilde2))
[1] 0.9965504
#Value is 0.99655

cor(as.vector(X),as.vector(X_tilde3))
[1] 0.9997697
#Value is 0.99977

#The fewest number of dimensions required is k = 2. 

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)\)
#
standardized_X <- scale(X, center = TRUE, scale = TRUE)

sigma <- cov(standardized_X)
sigma
            [,1]        [,2]        [,3]        [,4]        [,5]        [,6]
[1,]  1.00000000 -0.43360020  0.64476873  0.49377958  0.09469045  0.05374692
[2,] -0.43360020  1.00000000 -0.19004216 -0.06267813 -0.34973963  0.38631564
[3,]  0.64476873 -0.19004216  1.00000000  0.95526935  0.23794683 -0.03284543
[4,]  0.49377958 -0.06267813  0.95526935  1.00000000  0.21264375 -0.02642512
[5,]  0.09469045 -0.34973963  0.23794683  0.21264375  1.00000000 -0.01262596
[6,]  0.05374692  0.38631564 -0.03284543 -0.02642512 -0.01262596  1.00000000
[7,]  0.36956363 -0.43024212  0.13182930  0.04208319  0.16410559  0.49598467
            [,7]
[1,]  0.36956363
[2,] -0.43024212
[3,]  0.13182930
[4,]  0.04208319
[5,]  0.16410559
[6,]  0.49598467
[7,]  1.00000000
#The eigen-decomposition of the matrix 

components <- svd(sigma)

U_sigma <- components$u
D_sigma <- components$d
V_sigma <- components$v

U_sigma %>% round(2)
      [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]
[1,] -0.49 -0.08 -0.01  0.40 -0.73  0.18 -0.15
[2,]  0.32  0.09 -0.68 -0.18 -0.16  0.61  0.02
[3,] -0.54  0.23 -0.27 -0.03  0.16 -0.04  0.75
[4,] -0.49  0.28 -0.34 -0.11  0.35 -0.09 -0.65
[5,] -0.25 -0.06  0.31 -0.86 -0.27  0.15 -0.02
[6,]  0.00 -0.63 -0.49 -0.18 -0.16 -0.55  0.01
[7,] -0.26 -0.68  0.11  0.11  0.44  0.50 -0.01
D_sigma %>% round(2)
[1] 2.73 1.51 1.39 0.89 0.35 0.10 0.03
V_sigma %>% round(2)
      [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]
[1,] -0.49 -0.08 -0.01  0.40 -0.73  0.18 -0.15
[2,]  0.32  0.09 -0.68 -0.18 -0.16  0.61  0.02
[3,] -0.54  0.23 -0.27 -0.03  0.16 -0.04  0.75
[4,] -0.49  0.28 -0.34 -0.11  0.35 -0.09 -0.65
[5,] -0.25 -0.06  0.31 -0.86 -0.27  0.15 -0.02
[6,]  0.00 -0.63 -0.49 -0.18 -0.16 -0.55  0.01
[7,] -0.26 -0.68  0.11  0.11  0.44  0.50 -0.01
eig <-eigen(sigma)

eig_vectors <- eig$vectors

eig_values <- eig$values
#Confirm that eigenvectors of sigma equal to col(V)

all.equal(eig$vectors,V_sigma)
[1] "Mean relative difference: 1.15259"
#Not exactly equal has a mean relative diff. of 1.15

all.equal(eig$values, ((D_sigma)^2)/1)
[1] "Mean relative difference: 0.925403"
#Not exactly equal has a mean relative diff. of 0.93

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('C:/Users/lr7273ow/OneDrive - Minnesota State/Documents/GitHub/DSCI_415/Activities/Data/mystery_person_k4.Rdata')

#Now we have mysteryU4, mysteryD4,mysteryV4

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

#At a k = 4 approximation, it will look like this..


library(magick)
Warning: package 'magick' was built under R version 4.5.2
Linking to ImageMagick 6.9.13.29
Enabled features: cairo, freetype, fftw, ghostscript, heic, lcms, pango, raw, rsvg, webp
Disabled features: fontconfig, x11
k<-4

Xtilde <- mysteryU4 %*% diag(mysteryD4) %*% t(mysteryV4)

#Scaling the Xtilde
Xtilde_scaled <- (Xtilde - min(Xtilde)) / (max(Xtilde) - min(Xtilde))
 
(Xtilde_scaled
  %>% as.raster
  %>% image_read)

#No, k = 4 is not enough for me to tell who the villain is based on a glance (tho I think I know who it is)

B)

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

load('C:/Users/lr7273ow/OneDrive - Minnesota State/Documents/GitHub/DSCI_415/Activities/Data/mystery_person_k10.Rdata')

load('C:/Users/lr7273ow/OneDrive - Minnesota State/Documents/GitHub/DSCI_415/Activities/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

#At a k = 10 approximation, it will look like this..

k<-10

X_tilde10 <- mysteryU10 %*% diag(mysteryD10) %*% t(mysteryV10)

Xtilde10_scaled <- (X_tilde10 - min(X_tilde10))/(max(X_tilde10) - min(X_tilde10))

(Xtilde10_scaled
   %>% as.raster
  %>% image_read
)

 #Not quite but I believe the picture is todd iverson  

#At a k = 50 approximation, it will look like this..

k<-50
  
X_tilde50 <- mysteryU50 %*% diag(mysteryD50) %*% t(mysteryV50)

Xtilde50_scaled <- (X_tilde50 - min(X_tilde50))/(max(X_tilde50) - min
(X_tilde50))

(Xtilde50_scaled
  %>% as.raster
  %>% image_read
)

#Yep, thats Todd Iverson :P.

C)

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

  • A full \(700\times 600\) image?
  • A 4-dimensional approximation?
  • A 10-dimensional approximation?
  • A 50-dimensional approximation?

A full 700*600 image would require 420000 storage (total)

k = 4

U4 = 700 *4 = 2800, D4 = 4 = 16, V4 = 600*4 = 2400

2400+ 4 + 2800 = 5204

5204 would be the numbers required to store in memory

k=10

U4 = 700 *10 = 7000, D4 = 10*1 = 10, V4 = 600*10 = 6000

7000 + 100 + 6000 = 13010

k=50

U4 = 700 *50 = 35000, D4 = 50*1 = 10, V4 = 600*50 = 30000

35000 + 50 + 30000 = 650050