forked from MarcSerraPeralta/variational_QMonteCarlo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
248 lines (196 loc) · 6.74 KB
/
tests.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import lib as lib
import inputs as inputs
def check_random_walker_1D(tm_sigma, prob_density=None, alpha=None, N_steps=50000):
"""
Check for lib.random_walker() function with 1D input using visual inspection.
Parameters
----------
tm_sigma : float
Initial guess of the initial trial move variance for lib.random_walker()
prob_density : function or None
Probability density distribution for random_walker()
If None, it uses a Gaussian probability density distribution
alpha : np.ndarray
Parameters for prob_density()
If None, alpha=np.array([1])
N_steps : int
Number of steps of the random walker
Returns
-------
None
"""
if prob_density is None: prob_density = Gaussian
if alpha is None: alpha = np.array([1])
x_points, acceptance_probability, acceptance_ratio = lib.random_walker(prob_density, alpha, N_steps, np.zeros(1), tm_sigma)
plt.hist(x_points, bins=40, density=True)
xmin, xmax = np.min(x_points), np.max(x_points)
x = np.linspace(xmin, xmax, 1000)
plt.plot(x, prob_density(x, alpha), "--")
plt.xlim(xmin, xmax)
plt.xlabel("r")
plt.ylabel("Probability density function")
plt.tight_layout()
plt.show()
x1 = np.linspace(1, N_steps, N_steps)
print(x.shape, acceptance_probability.shape)
plt.scatter(x1, acceptance_probability, s=1)
plt.plot(x1, acceptance_ratio*np.ones(N_steps), 'r')
plt.ylim(0, 1)
plt.xlabel("step")
plt.ylabel("Acceptance probability")
plt.tight_layout()
plt.show()
graph=sns.jointplot(x=x1, y=acceptance_probability, s=1, hue_norm=(0,1))
graph.ax_joint.axhline(y=acceptance_ratio, c='r')
plt.show()
return
def check_random_walker_3D(tm_sigma, prob_density=None, alpha=None, N_steps=50000):
"""
Check for lib.random_walker() function with 3D input using visual inspection.
Parameters
----------
tm_sigma : float
Initial guess of the initial trial move variance for lib.random_walker()
prob_density : function or None
Probability density distribution for random_walker()
If None, it uses a Gaussian probability density distribution
alpha : np.ndarray
Parameters for prob_density()
If None, alpha=np.array([1])
N_steps : int
Number of steps of the random walker
Returns
-------
None
"""
if prob_density is None: prob_density = lambda x,y,z,std: Gaussian(x,std)*Gaussian(y,std)*Gaussian(z,std)
if alpha is None: alpha = np.array([1])
x_points = lib.random_walker(prob_density, alpha, N_steps, np.zeros(3), tm_sigma)
plt.hist(x_points[:,0], bins=40, density=True, alpha=0.3, label="r1")
plt.hist(x_points[:,1], bins=40, density=True, alpha=0.3, label="r2")
plt.hist(x_points[:,2], bins=40, density=True, alpha=0.3, label="r3")
plt.xlabel("r_i")
plt.ylabel("Probability density function")
plt.legend()
plt.tight_layout()
plt.show()
return
def check_random_walkers_1D(tm_sigma, prob_density=None, alpha=None, N_steps=50000):
"""
Check for lib.random_walker() function with 3D input using visual inspection.
Parameters
----------
tm_sigma : float
Initial guess of the initial trial move variance for lib.random_walker()
prob_density : function or None
Probability density distribution for random_walker()
If None, it uses a Gaussian probability density distribution
alpha : np.ndarray
Parameters for prob_density()
If None, alpha=np.array([1])
N_steps : int
Number of steps of the random walker
Returns
-------
None
"""
if prob_density is None: prob_density = Gaussian
if alpha is None: alpha = np.array([1])
x_points = lib.random_walkers(prob_density, alpha, N_steps, np.zeros((3,1)), tm_sigma)
plt.hist(x_points[:,0,0], bins=40, density=True, alpha=0.3, label="RW 1")
plt.hist(x_points[:,1,0], bins=40, density=True, alpha=0.3, label="RW 2")
plt.hist(x_points[:,2,0], bins=40, density=True, alpha=0.3, label="RW 3")
xmin, xmax = np.min(x_points), np.max(x_points)
x = np.linspace(xmin, xmax, 1000)
plt.plot(x, prob_density(x, alpha), "--")
plt.xlim(xmin, xmax)
plt.xlabel("r_i")
plt.ylabel("Probability density function")
plt.legend()
plt.tight_layout()
plt.show()
return
def monitor_ar_hydrogen(trial_move, N_walkers=10000, N_steps=10000):
"""
Plots a histogram of the acceptance ratio for hydrogen for all the random walkers.
Parameters
----------
trial_move : float
Standard deviation that defines the trial move according to a normal distribution
N_walkers : int
Number of random walkers
N_steps : int
Number of steps that each random walker takes
Returns
-------
None
"""
L_start = 5
dim = 3
alpha = np.array([1])
prob_density = inputs.prob_density_Hydrogen_atom
init_points = lib.rand_init_point(L_start, dim, N_walkers)
_, _, acceptance_ratio = lib.random_walkers(prob_density, alpha, N_steps, init_points, trial_move)
plt.hist(acceptance_ratio, bins=40, density=True)
plt.xlabel("acceptance ratio")
plt.ylabel("density of walkers")
plt.show()
return
def Gaussian(x, std):
"""
Returns the probability of x in a 1D Gaussian distribution of
zero mean and standard deviation of std.
Parameters
----------
x : float
Variable of the Gaussian distribution
std : float
Standard deviation of the aussian distribution
Returns
-------
f : float
Probability of x in a 1D Gaussian distribution N(0, std)
"""
f = np.exp(-x**2/(2*std**2))/(std*np.sqrt(2*np.pi))
return f
if __name__ == '__main__':
print("CHECK acceptance ratio Hydrogen (tm_sigma=opt)...")
opt_sigma = lib.find_optimal_trial_move(inputs.prob_density_Hydrogen_atom,np.array([1]),3,2)
print("The optimal trial move is: ", opt_sigma)
monitor_ar_hydrogen(opt_sigma)
print("DONE")
print("CHECK GAUSSIAN 1D (tm_sigma=opt)...")
opt_sigma = lib.find_optimal_trial_move(Gaussian,np.array([1]),1,1)
print("The optimal trial move is: ", opt_sigma)
check_random_walker_1D(opt_sigma)
print("DONE")
print("CHECK X^2*GAUSSIAN 1D (tm_sigma=opt)...")
f = lambda x, std: std*x**2*Gaussian(x, std)
opt_sigma = lib.find_optimal_trial_move(f,np.array([1]),1,1)
print("The optimal trial move is: ", opt_sigma)
check_random_walker_1D(opt_sigma, prob_density=f)
print("DONE")
print("CHECK GAUSSIAN 1D (tm_sigma=1)...")
check_random_walker_1D(1)
print("DONE")
print("CHECK X^2*GAUSSIAN 1D (tm_sigma=1)...")
f = lambda x, std: std*x**2*Gaussian(x, std)
check_random_walker_1D(1, prob_density=f)
print("DONE")
print("CHECK X^2*GAUSSIAN 1D (tm_sigma=0.1)...")
f = lambda x, std: std*x**2*Gaussian(x, std)
check_random_walker_1D(0.1, prob_density=f)
print("DONE")
print("CHECK X^2*GAUSSIAN 1D (tm_sigma=0.01)...")
f = lambda x, std: std*x**2*Gaussian(x, std)
check_random_walker_1D(0.01, prob_density=f)
print("DONE")
print("CHECK GAUSSIAN 3D (tm_sigma=1)...")
check_random_walker_3D(1)
print("DONE")
print("CHECK GAUSSIAN 1D WITH MULTIPLE RANDOM WALKERS (tm_sigma=1)...")
check_random_walkers_1D(1)
print("DONE")