-
Notifications
You must be signed in to change notification settings - Fork 0
/
svd.py
98 lines (67 loc) · 1.75 KB
/
svd.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'''
SVD like K, W -> weights
Save W
K * W -> Prediction
Gotta save RowMean, std.dev etc
'''
import numpy as np
import scipy.linalg as SL
import matplotlib.pyplot as plt
#import SetPub
#SetPub.set_pub()
#def rescale_nM():
#return (f - xmin)/(xmax - xmin)
xlim1 = 10
xlim2 = 15
Mass = np.logspace(np.log10(10**xlim1), np.log10(10**xlim2), 500)[::10]
nsize = 5
hmf = np.loadtxt('Data/HMF_5Para.txt')
#OmegaM = hmf[:, 0] ## OmegaM
#deltaH = hmf[:, 1]
#X = rescale01( np.min(X), np.max(X), X)
y = hmf[:,5:].T # n(M) -> all values
yRowMean = np.zeros_like(y[:,0])
for i in range(y.shape[0]):
yRowMean[i] = np.mean(y[i])
for i in range( y[0].shape[0] ):
y[:,i] = (y[:,i] - yRowMean)
stdy = np.std(y)
y = y/stdy
Pxx = y
U, s, Vh = SL.svd(Pxx, full_matrices=False)
assert np.allclose(Pxx, np.dot(U, np.dot(np.diag(s), Vh)))
NoEigenComp = 2
TruncU = U[:, :NoEigenComp] #Truncation
TruncS = s[:NoEigenComp]
TruncSq = np.diag(TruncS)
TruncVh = Vh[:NoEigenComp,:]
K = np.matmul(TruncU, TruncSq)/np.sqrt(NoEigenComp)
W1 = np.sqrt(NoEigenComp)*np.matmul(np.diag(1./TruncS), TruncU.T)
W = np.matmul(W1, y)
Pred = np.matmul(K,W)
for i in range(2, 11):
plt.figure(i)
plt.plot(Mass, y[:,i]*stdy + yRowMean, 'o', label = 'data')
plt.plot(Mass, Pred[:,i]*stdy + yRowMean)
plt.xscale('log')
#plt.yscale('log')
plt.show()
plt.figure(4343)
plt.plot(s, 'o-')
plt.xlim(-1,)
plt.ylim(-1,45)
plt.savefig('Plots/svd_fig1.png')
np.savetxt('Data/K_for2Eval.txt', K) # Basis
np.savetxt('Data/W_for2Eval.txt', W) # Weights
np.savetxt('Data/stdy.txt', [stdy])
np.savetxt('Data/yRowMean.txt',yRowMean)
#s[2:] = 0
#new_a = np.dot(U, np.dot(np.diag(s), Vh))
#print(new_a)
#plt.plot(new_a)
#plt.show()
#
#
#
#plt.figure(10)
#plt.plot()