ONLY NEED OUTPUT VALUES FOR C AND D. PLUS FINAL PLOT
Use the following code to show that the power method can be used to calculate the largest eigenvalue and corresponding eigenvector of a covariance matrix.
A. Generate the data:
x <- rnorm(1000)
dim(x) <- c(50,20)
x <- x* 1:9
x[,1] <- x[,1]*2+ x[,3] + x[,20]
x[,5] <- x[,5]*3+ x[,3] +2* x[,20]
B.Calculate the covariance matrix and its powers
vx <- var(x)
vx2 <- vx%*%vx
vx4 <- vx2%*%vx2
vx8 <- vx4%*%vx4
vx16 <- vx8%*%vx8
vx32 <- vx16%*%vx16
vx64 <- vx32%*%vx32
C. Approximate the largest eigenvalue and see how it converges.
# By the (1,1) element
vx2[1,1]^(1/2)
vx4[1,1]^(1/4)
vx8[1,1]^(1/8)
vx16[1,1]^(1/16)
vx32[1,1]^(1/32)
vx64[1,1]^(1/64)
# By the trace
sum(diag(vx2))^(1/2)
sum(diag(vx4))^(1/4)
sum(diag(vx8))^(1/8)
sum(diag(vx32))^(1/32)
D.Check it with the true value.
ei <- eigen(vx)
ei$values[1]
E.Calculate the 1st eigenvector using vx16
# Add the norm command first
norm <- function(x) sqrt(sum(x^2))
h <- vx16[,1]
h <- h/norm(h)
F. Check it with the true values
h0 <- ei$vectors[,1]
plot(h,h0)
abline(0,1)
Get Answers For Free
Most questions answered within 1 hours.