-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCMC.py
269 lines (183 loc) · 6.92 KB
/
MCMC.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import numpy as np
import matplotlib.pyplot as plt
#import scipy.special
#from numba import jit
class Discrete:
'''Monte-Carlo with Markov Chains implementation'''
def run(self, sample, log_prob, iterations=5000):
'''
Executes the MCMC algorithm
Input :
sample (1D array): initial spins of the sample
Return : list of used samples (2D array)
'''
list_of_samples = np.tile( 0*sample, (iterations,1) )
#Choose randomly spin to flip
spin_to_flip = np.random.randint(0,sample.size, size=iterations)
eta = np.random.uniform(size=iterations) # Uniform random number for MCMC step
accepted = 0 # For the acceptance ratio
for i in range(iterations):
#Choose randomly spin to flip
new_sample = np.copy(sample)
new_sample[spin_to_flip[i]] *= -1
#Compute test (with unnormalized probability bc. of division btw both)
R = np.exp( log_prob(new_sample) - log_prob(sample) )
if R > eta[i]:
sample = new_sample
accepted += 1
# Save the sample
list_of_samples[i] = sample
print(f'Acceptance rate : {accepted/iterations}')
return list_of_samples
class Quantum:
'''Monte-Carlo with Markov Chains implementation'''
def __init__(self, model, hamiltonian, iterations=5000):
self.model = model
self.hamiltonian = hamiltonian
self.length = model.length
self.iterations = iterations
def print_infos(self):
self.hamiltonian.print_infos()
print(f'The model is : {self.model.name}')
print(f'The number of Monte-Carlo iterations is {self.iterations}')
print('-----------------------------------------')
def run(self, sample=None):
'''
Executes the MCMC algorithm
Input :
sample (1D array): initial spins of the sample
if None, random sample
Return : list of used samples (2D array)
'''
E_loc = np.zeros( (self.iterations, 1), dtype=complex )
if sample is None:
sample = np.random.choice([-1.0, 1.0], (self.length,1) )
list_of_samples = np.zeros( (self.iterations, self.length,1) )
#Choose randomly spin to flip
spin_to_flip = np.random.randint(0,self.length, size=self.iterations)
for i in range(self.iterations):
new_sample = np.copy(sample)
new_sample[spin_to_flip[i]] *= -1
#Compute test (with unnormalized probability bc. of division btw both)
R = np.exp( self.model.log_prob(new_sample) - self.model.log_prob(sample) )
#print(R)
eta = np.random.uniform()
if R > eta:
sample[spin_to_flip[i]] *= -1
# Save the sample
list_of_samples[i] = np.copy(sample)
E_loc[i] = self.model.local_energy(self.hamiltonian, sample)
return list_of_samples, E_loc
class Continuous:
'''Monte-Carlo with Markov Chains implementation for continuous variables'''
def __init__(self, std):
self.std = std
def print_infos(self):
print(f'The model is : {self.model.name}')
print(f'The number of Monte-Carlo iterations is {self.iterations}')
print('-----------------------------------------')
def run(self, sample, log_prob, iterations=5000):
'''
Executes the MCMC algorithm
Input :
sample (1D array): initial spins of the sample
Return : list of used samples (2D array)
'''
# Prepare vector to receive all generated samples
list_of_samples = np.tile( 0*sample, (iterations,1) )
#Choose randomly spin to flip
spin_to_flip = np.random.randint(0,sample.size, size=iterations)
moves = np.random.normal(scale=self.std, size=(iterations,1) ) # Gaussian of chosen std
eta = np.random.uniform(size=iterations) # Uniform random number for MCMC step
accepted = 0 # For the acceptance ratio
for i in range(iterations):
k = spin_to_flip[i]
new_sample = np.copy(sample)
new_sample[k] += moves[i]
#Compute test (with unnormalized probability)
R = np.exp(log_prob(new_sample) - log_prob(sample))
#print(R)
if R > eta[i]:
sample = np.copy(new_sample)
accepted += 1
# Save the sample
list_of_samples[i] = np.copy(sample)
print(f'Acceptance rate : {accepted/iterations}')
return list_of_samples
class Multivariate:
'''Monte-Carlo with Markov Chains implementation for continuous variables'''
def __init__(self, cov):
self.cov = cov
def print_infos(self):
print(f'The Covaraiance matrix is : {self.cov}')
print('-----------------------------------------')
def run(self, sample, log_prob, iterations=5000):
'''
Executes the MCMC algorithm
Input :
sample (1D array): initial spins of the sample
Return : list of used samples (2D array)
'''
# Prepare vector to receive all generated samples
list_of_samples = np.tile( 0*sample, (iterations,1) )
#Choose randomly spin to flip
#spin_to_flip = np.random.randint(0,sample.size, size=iterations)
moves = np.random.multivariate_normal(mean=np.zeros(sample.shape),cov=self.cov, size=(iterations) ) # Gaussian of chosen std
eta = np.random.uniform(size=iterations) # Uniform random number for MCMC step
accepted = 0 # For the acceptance ratio
for i in range(iterations):
#k = spin_to_flip[i]
new_sample = sample + moves[i]
#new_sample[k] += moves[i]
#Compute test (with unnormalized probability)
R = np.exp(log_prob(new_sample) - log_prob(sample))
#print(R)
if R > eta[i]:
sample = new_sample
accepted += 1
# Save the sample
list_of_samples[i] = np.copy(sample)
print(f'Acceptance rate : {accepted/iterations}')
return list_of_samples
class Sphere:
'''Monte-Carlo with Markov Chains implementation'''
def move(self, x, i, std):
'''
x : array in cartesian coordinates
std : standard deviation in the cartesian space
(does not correspond to a std for the angles)
'''
x_new = np.copy(x)
x_new[i] = np.random.normal(x[i], std, size=x.shape[-1])
x_new[i] /= np.linalg.norm(x_new[i])
return x_new, self.cartesian_to_spherical(x_new)
def cartesian_to_spherical(self, x):
phi = np.arctan2(x[:,1],x[:,0])
theta = np.arctan2(np.sqrt(x[:,0]**2 + x[:,1]**2), x[:,2])
return np.array([theta, phi])
def run(self, angles, log_prob, std = 1, iterations=5000):
'''
Executes the MCMC algorithm
Input :
Return : list of used samples (2D array)
'''
sample_cart = np.array([np.sin(angles[0])*np.cos(angles[1]), np.sin(angles[0])*np.sin(angles[1]), np.cos(angles[1])])
sample_spher = angles
list_of_samples = np.tile( 0*sample_spher, (iterations,1) )
#Choose randomly spin to flip
to_move = np.random.randint(0,sample.size, size=iterations)
eta = np.random.uniform(size=iterations) # Uniform random number for MCMC step
accepted = 0 # For the acceptance ratio
for i in range(iterations):
#Choose randomly spin to flip
new_sample_cart, new_sample_spher = move(sample_cart, i, std)
#Compute test (with unnormalized probability bc. of division btw both)
R = np.exp( log_prob(new_sample_spher) - log_prob(sample_spher) )
if R > eta[i]:
sample_spher = new_sample_spher
sample_cart = new_sample_cart
accepted += 1
# Save the sample
list_of_samples[i] = sample_spher
print(f'Acceptance rate : {accepted/iterations}')
return list_of_samples