Activity 1.3 - covariance, correlations, and eigendecompositions
For each of the following, fully justify your answers. While I encourage you to show your math work using the “math mode” syntax, I will also accept screenshots of matrix work. To embed screenshots, enter Visual mode on your editor and simply paste the screenshot where you want it to go.
Question 1
Consider the covariance matrix from a data set of temperature and precipitation of 41 US cities:
temp precip
temp 52 33
precip 33 139
A)
If an eigendecomposition of this matrix is done, which of these could be the pair of eigenvalues?
161 and 20
100 and 80
150 and 41
190 and 5
Answer: i. 150 and 41
B)
What is the correlation between temperature and precipitation?
For those that are eigenvectors, can you start to get a feel for what the eigenvalues might be by noticing common themes?
Any vector where both numbers are identical produces an eigenvalue of \(\lambda = 3\).Any vector where the numbers are opposites produces an eigenvalue of \(\lambda = 1\).
B)
Find the eigenvalues of \(M\) by solving the characteristic equation:
\[ det(M - \lambda I) = 0\]
Set up the matrix \(M-\lambda I\) \[M - \lambda I = \begin{pmatrix} 2 - \lambda & 1 \\ 1 & 2 - \lambda \end{pmatrix}\]
For this problem, use the cars data set that comes installed with R.
A)
Create a mean-centered (but not scaled!) version of the data: call it cars_centered. Plot the data with ggplot.
B)
Find the covariance matrix \(\Sigma_{centered}\) and the correlation matrix \(R_{centered}\) of the mean-centered data set, using cov() and cor(). Verify that \(\Sigma_{centered} = \frac{1}{n-1}X_C^T X_C\).
C)
Perform an eigendecomposition of \(\Sigma_{centered}\). Verify that the eigenvalues and the marginal variances sum to the same thing.
D)
Use the eigenvectors to rotate the centered data. Create a plot of the rotated data.
E)
Find the marginal variances of the rotated data, and verify that they equal the eigenvalues.
F)
Now scale the data: call it cars_scaled. Find the covariance of this data \(\Sigma_{scaled}\) and verify that it equals \(R_{centered}\).
G)
Rotate the scaled data using the appropriate eigenvectors, and plot the rotated data.
H)
Verify that the marginal variances of the rotated data equal the eigenvalues of \(R_{centered}\) aka \(\Sigma_{scaled}\). Also verify that they sum to 2 (the diagonals of \(R_{centered}\) aka \(\Sigma_{scaled}\)).
library(ggplot2)data(cars)n <-nrow(cars)# -----------------------------------------------------------# Part A: Mean-center data and plot# -----------------------------------------------------------cars_centered <-as.data.frame(scale(cars, center =TRUE, scale =FALSE))ggplot(cars_centered, aes(x = speed, y = dist)) +geom_point(color ="steelblue", size =2) +geom_hline(yintercept =0, linetype ="dashed") +geom_vline(xintercept =0, linetype ="dashed") +theme_minimal() +labs(title ="Part A: Mean-Centered Cars Data", x ="Centered Speed", y ="Centered Dist")
# -----------------------------------------------------------# Part B: Covariance & Correlation matrices + Formula check# -----------------------------------------------------------Sigma_centered <-cov(cars_centered)R_centered <-cor(cars_centered)Xc <-as.matrix(cars_centered)Sigma_formula <- (1/ (n -1)) * (t(Xc) %*% Xc)cat("Sigma from cov():\n")
cat("\nAre they identical?", all.equal(Sigma_centered, Sigma_formula), "\n")
Are they identical? TRUE
# -----------------------------------------------------------# Part C: Eigendecomposition and sum comparison# -----------------------------------------------------------decomp_cov <-eigen(Sigma_centered)cat("\nEigenvalues of Sigma_centered:\n")
Eigenvalues of Sigma_centered:
print(decomp_cov$values)
[1] 682.528426 9.491574
cat("Sum of eigenvalues:", sum(decomp_cov$values), "\n")
Sum of eigenvalues: 692.02
cat("Sum of marginal variances (trace):", sum(diag(Sigma_centered)), "\n")
Sum of marginal variances (trace): 692.02
cat("Are they equal?", all.equal(sum(decomp_cov$values), sum(diag(Sigma_centered))), "\n")
Are they equal? TRUE
# -----------------------------------------------------------# Part D: Rotate centered data and plot# -----------------------------------------------------------V <- decomp_cov$vectorscars_rotated <-as.data.frame(as.matrix(cars_centered) %*% V)colnames(cars_rotated) <-c("Axis1", "Axis2")ggplot(cars_rotated, aes(x = Axis1, y = Axis2)) +geom_point(color ="firebrick", size =2) +geom_hline(yintercept =0, linetype ="dashed") +geom_vline(xintercept =0, linetype ="dashed") +theme_minimal() +labs(title ="Part D: Rotated Centered Cars Data", x ="First Principal Direction", y ="Second Principal Direction")
# -----------------------------------------------------------# Part E: Verify marginal variances of rotated data# -----------------------------------------------------------rotated_variances <-apply(cars_rotated, 2, var)cat("\nVariances of rotated columns:\n")
Variances of rotated columns:
print(rotated_variances)
Axis1 Axis2
682.528426 9.491574
cat("Eigenvalues:\n")
Eigenvalues:
print(decomp_cov$values)
[1] 682.528426 9.491574
cat("Are they equal?", all.equal(unname(rotated_variances), decomp_cov$values), "\n")
Are they equal? TRUE
# -----------------------------------------------------------# Part F: Scale data and verify Sigma_scaled == R_centered# -----------------------------------------------------------cars_scaled <-as.data.frame(scale(cars, center =TRUE, scale =TRUE))Sigma_scaled <-cov(cars_scaled)cat("\nCovariance of scaled data:\n")