All problems
PCA First Component Variance
Problem
Compute the variance explained by the first principal component of a two-column data matrix, using the population covariance. For the 2x2 covariance matrix with entries a, b, b, c, return the largest eigenvalue: (a + c) / 2 + sqrt(((a - c) / 2)^2 + b^2).
Return 0.0 for an empty matrix. Each row of data is one observation of two series.
Hint
The first PC variance is the largest eigenvalue of the covariance matrix.
Test cases
case 1
in: [[[1,1],[2,2],[3,3]]]
expected: 1.3333333333333333
case 2
in: [[[1,2],[2,4],[3,6]]]
expected: 3.333333333333333
case 3
in: [[[1,0],[0,1],[-1,0],[0,-1]]]
expected: 0.5
case 4
in: [[[2,1],[4,2],[6,3]]]
expected: 3.333333333333333
Starter codePython
def pca_first_component_variance(data):
# Your code here
pass4 test cases · run your code against them in the editor