-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestPCA.py
More file actions
37 lines (28 loc) · 1.13 KB
/
testPCA.py
File metadata and controls
37 lines (28 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import matrix
import abe
X=matrix.Matrix(abe.X)
Y=matrix.Matrix(abe.Y)
P=matrix.Matrix(abe.P)
I=matrix.Matrix(abe.I)
print P.transpose()*P == I # verify P is orthogonal
## This may be FALSE! depending on how test is done.
print P.transpose()*P
print I
print P.transpose()*P == (1.0)*I ## Maybe this is True, depending on implementation
print P.transpose()*P + (-1.0)*I ## maybe this shows machine erroe
print P*P.transpose() + (-1.0)*I # verify P is orthogonal, again
print Y
print P*X
print Y == P*X
print Y + (-1.0)*P*X # verfiy P is coordinate change from X to Y
n=X.dimension()[1]
CX = (1.0/float(n-1))*(X*X.transpose()) # correlation matrix, n = X.dimension()[1]
print CX
CY = (1.0/float(n-1))*(Y*Y.transpose()) # correlation matrix, same n
print CY
print CY + (-1)*P*CX*P.transpose() # verify coordinate change works for correlation, bottom of left side of page 7
variances = [ CY[i,i] for i in range(CY.dimension()[0] ) ] ## CY is diagonal, its entries are variances
print variances
## finally, let's import numpy to see the "allclose" routine
from numpy import allclose
print allclose([0.00000000000001], [0])