This repository has been archived by the owner on Aug 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
code_TULA.py
410 lines (373 loc) · 16.2 KB
/
code_TULA.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# -*- coding: utf-8 -*-
import numpy as np
class potential:
""" Implements the potentials U and the algorithms described in the article.
"""
# Parameters for ginzburg-landau model
tau=2.0
lamb=0.5
alpha=0.1
# threshold for stopping the trajectory of ULA
threshold=10**5
def __init__(self, typ, d):
""" Initialize the object.
:param typ: potential U
:param d: dimension
"""
self.type = typ
self.d = d
self.acc_mala = 0 # acceptance probability for MALA
self.acc_rwm = 0 # acceptance probability for RWM
self.acc_tmala = 0 # acceptance probability for TMALA
self.acc_tmalac = 0 # acceptance probability for TMALAc
def potential(self, X):
""" Definition of the potential
:param X: point X of Rd where the potential is evaluated
:return: U(X)
"""
if self.type == "ill-cond-gaussian":
d = self.d
v = np.array(np.arange(1,d+1), dtype = float)
InvSigma = 1. / v
return 0.5*np.dot(X, np.multiply(InvSigma, X))
elif self.type == "double-well":
return (1./4)*np.linalg.norm(X)**4 - (1./2)*np.linalg.norm(X)**2
elif self.type == "Ginzburg-Landau":
tau = self.tau
alpha = self.alpha
lamb = self.lamb
dim = int(np.rint((self.d)**(1./3)))
X = np.reshape(X, (dim,dim,dim))
temp = np.linalg.norm(np.roll(X, -1, axis=0) - X)**2 \
+ np.linalg.norm(np.roll(X, -1, axis=1) - X)**2 \
+ np.linalg.norm(np.roll(X, -1, axis=2) - X)**2
return 0.5*(1-tau)*np.linalg.norm(X)**2 + 0.5*tau*alpha*temp \
+ (1./4)*tau*lamb*np.sum(np.power(X,4))
else:
print("Error potential not defined")
def gradpotential(self, X):
""" Definition of the gradient of the potential
:param X: point X of Rd where the gradient is evaluated
:return: gradU(X)
"""
if self.type == "ill-cond-gaussian":
d = self.d
v = np.array(np.arange(1,d+1), dtype = float)
InvSigma = 1. / v
return np.multiply(InvSigma, X)
elif self.type == "double-well":
return (np.linalg.norm(X)**2 - 1)*X
elif self.type == "Ginzburg-Landau":
tau = self.tau
alpha = self.alpha
lamb = self.lamb
dim = int(np.rint((self.d)**(1./3)))
X = np.reshape(X, (dim,dim,dim))
temp = np.roll(X, 1, axis=0)+np.roll(X, -1, axis=0) \
+np.roll(X, 1, axis=1)+np.roll(X, -1, axis=1) \
+np.roll(X, 1, axis=2)+np.roll(X, -1, axis=2)
gradU = (1.0-tau)*X + tau*lamb*np.power(X,3) + tau*alpha*(6*X - temp)
return gradU.flatten()
else:
print("Error gradpotential not defined")
def ULA(self, x0, step, N=10**6):
""" Algorithm ULA
:param x0: starting point
:param step: step size of the algorithm
:param N: number of iterations (after burn in period)
:return: empirical averages of 1st and 2nd moments
"""
d = self.d
X = x0
m1 = np.zeros(d)
m2 = np.zeros(d)
burnin = 10**4 # burn in period
for k in np.arange(burnin):
if np.linalg.norm(X, np.inf)>self.threshold:
m1[:] = np.nan
m2[:] = np.nan
return (m1,m2)
gradU = self.gradpotential(X)
X = X - step*gradU + np.sqrt(2*step)*np.random.normal(size=d)
for k in np.arange(N):
if np.linalg.norm(X, np.inf)>self.threshold:
m1[:] = np.nan
m2[:] = np.nan
return (m1,m2)
m1 += X / float(N)
m2 += np.power(X, 2) / float(N)
gradU = self.gradpotential(X)
X = X - step*gradU + np.sqrt(2*step)*np.random.normal(size=d)
return (m1,m2)
def TULA(self, x0, step, N=10**6):
""" Algorithm TULA
:param x0: starting point
:param step: step size of the algorithm
:param N: number of iterations (after burn in period)
:return: empirical averages of 1st and 2nd moments
"""
d = self.d
X = x0
m1 = np.zeros(d)
m2 = np.zeros(d)
burnin = 10**4
for k in np.arange(burnin):
gradU = self.gradpotential(X)
gradUTamed = gradU / (1.0 + step*np.linalg.norm(gradU))
X = X - step*gradUTamed + np.sqrt(2*step)*np.random.normal(size=d)
for k in np.arange(N):
m1 += X / float(N)
m2 += np.power(X, 2) / float(N)
gradU = self.gradpotential(X)
gradUTamed = gradU / (1.0 + step*np.linalg.norm(gradU))
X = X - step*gradUTamed + np.sqrt(2*step)*np.random.normal(size=d)
return (m1,m2)
def TULAc(self, x0, step, N=10**6):
""" Algorithm TULAc
:param x0: starting point
:param step: step size of the algorithm
:param N: number of iterations (after burn in period)
:return: empirical averages of 1st and 2nd moments
"""
d = self.d
X = x0
m1 = np.zeros(d)
m2 = np.zeros(d)
burnin = 10**4
for k in np.arange(burnin):
gradU = self.gradpotential(X)
gradUTamed = np.divide(gradU, 1.0 + step*np.absolute(gradU))
X = X - step*gradUTamed + np.sqrt(2*step)*np.random.normal(size=d)
for k in np.arange(N):
m1 += X / float(N)
m2 += np.power(X, 2) / float(N)
gradU = self.gradpotential(X)
gradUTamed = np.divide(gradU, 1.0 + step*np.absolute(gradU))
X = X - step*gradUTamed + np.sqrt(2*step)*np.random.normal(size=d)
return (m1,m2)
def TMALA(self, x0, step, N=10**6):
""" Algorithm TMALA
:param x0: starting point
:param step: step size of the algorithm
:param N: number of iterations (after burn in period)
:return: empirical averages of 1st and 2nd moments
"""
acc=0 # to store the empirical acceptance probability
m1 = np.zeros(d)
m2 = np.zeros(d)
X = x0
burnin = 10**4
for k in np.arange(burnin):
U_X = self.potential(X)
grad_U_X = self.gradpotential(X)
Tgrad_U_X = grad_U_X / (1. + step*np.linalg.norm(grad_U_X))
Y = X - step * Tgrad_U_X + np.sqrt(2*step)*np.random.normal(size=self.d)
U_Y = self.potential(Y)
grad_U_Y = self.gradpotential(Y)
Tgrad_U_Y = grad_U_Y / (1. + step*np.linalg.norm(grad_U_Y))
logratio = - U_Y + U_X + (1./(4*step))*(np.linalg.norm(Y-X+step*Tgrad_U_X)**2 \
- np.linalg.norm(X-Y+step*Tgrad_U_Y)**2)
if np.log(np.random.uniform(size=1))<=logratio:
X = Y
for k in np.arange(N):
m1 += X / float(N)
m2 += np.power(X, 2) / float(N)
U_X = self.potential(X)
grad_U_X = self.gradpotential(X)
Tgrad_U_X = grad_U_X / (1. + step*np.linalg.norm(grad_U_X))
Y = X - step * Tgrad_U_X + np.sqrt(2*step)*np.random.normal(size=self.d)
U_Y = self.potential(Y)
grad_U_Y = self.gradpotential(Y)
Tgrad_U_Y = grad_U_Y / (1. + step*np.linalg.norm(grad_U_Y))
logratio = - U_Y + U_X + (1./(4*step))*(np.linalg.norm(Y-X+step*Tgrad_U_X)**2 \
- np.linalg.norm(X-Y+step*Tgrad_U_Y)**2)
if np.log(np.random.uniform(size=1))<=logratio:
X = Y
acc+=1
self.acc_tmala = float(acc)/N # empirical acceptance probability
return (m1,m2)
def TMALAc(self, x0, step, N=10**6):
""" Algorithm TMALAc
:param x0: starting point
:param step: step size of the algorithm
:param N: number of iterations (after burn in period)
:return: empirical averages of 1st and 2nd moments
"""
acc=0
m1 = np.zeros(d)
m2 = np.zeros(d)
X = x0
burnin=10**4
for k in np.arange(burnin):
U_X = self.potential(X)
grad_U_X = self.gradpotential(X)
Tgrad_U_X = np.divide(grad_U_X, 1. + step*np.absolute(grad_U_X))
Y = X - step * Tgrad_U_X + np.sqrt(2*step)*np.random.normal(size=self.d)
U_Y = self.potential(Y)
grad_U_Y = self.gradpotential(Y)
Tgrad_U_Y = np.divide(grad_U_Y, 1. + step*np.absolute(grad_U_Y))
logratio = - U_Y + U_X + (1./(4*step))*(np.linalg.norm(Y-X+step*Tgrad_U_X)**2 \
- np.linalg.norm(X-Y+step*Tgrad_U_Y)**2)
if np.log(np.random.uniform(size=1))<=logratio:
X = Y
for k in np.arange(N):
m1 += X / float(N)
m2 += np.power(X, 2) / float(N)
U_X = self.potential(X)
grad_U_X = self.gradpotential(X)
Tgrad_U_X = np.divide(grad_U_X, 1. + step*np.absolute(grad_U_X))
Y = X - step * Tgrad_U_X + np.sqrt(2*step)*np.random.normal(size=self.d)
U_Y = self.potential(Y)
grad_U_Y = self.gradpotential(Y)
Tgrad_U_Y = np.divide(grad_U_Y, 1. + step*np.absolute(grad_U_Y))
logratio = - U_Y + U_X + (1./(4*step))*(np.linalg.norm(Y-X+step*Tgrad_U_X)**2 \
- np.linalg.norm(X-Y+step*Tgrad_U_Y)**2)
if np.log(np.random.uniform(size=1))<=logratio:
X = Y
acc+=1
self.acc_tmalac = float(acc)/N
return (m1,m2)
def MALA(self, x0, step, N=10**6):
""" Algorithm TMALA
:param x0: starting point
:param step: step size of the algorithm
:param N: number of iterations (after burn in period)
:return: empirical averages of 1st and 2nd moments
"""
acc=0
d = self.d
m1 = np.zeros(d)
m2 = np.zeros(d)
X = x0
burnin = 10**4
for k in np.arange(burnin):
U_X = self.potential(X)
grad_U_X = self.gradpotential(X)
Y = X - step * grad_U_X + np.sqrt(2*step)*np.random.normal(size=self.d)
U_Y = self.potential(Y)
grad_U_Y = self.gradpotential(Y)
logratio = - U_Y + U_X + (1./(4*step))*(np.linalg.norm(Y-X+step*grad_U_X)**2 \
- np.linalg.norm(X-Y+step*grad_U_Y)**2)
if np.log(np.random.uniform(size=1))<=logratio:
X = Y
for k in np.arange(N):
m1 += X / float(N)
m2 += np.power(X, 2) / float(N)
U_X = self.potential(X)
grad_U_X = self.gradpotential(X)
Y = X - step * grad_U_X + np.sqrt(2*step)*np.random.normal(size=self.d)
U_Y = self.potential(Y)
grad_U_Y = self.gradpotential(Y)
logratio = - U_Y + U_X + (1./(4*step))*(np.linalg.norm(Y-X+step*grad_U_X)**2 \
- np.linalg.norm(X-Y+step*grad_U_Y)**2)
if np.log(np.random.uniform(size=1))<=logratio:
X = Y
acc+=1
self.acc_mala = float(acc)/N
return (m1,m2)
def RWM(self, x0, step, N=10**6):
""" Algorithm RWM
:param x0: starting point
:param step: step size of the algorithm
:param N: number of iterations (after burn in period)
:return: empirical averages of 1st and 2nd moments
"""
acc=0
d = self.d
m1 = np.zeros(d)
m2 = np.zeros(d)
X = x0
burnin = 10**4
for k in np.arange(burnin):
U_X = self.potential(X)
Y = X + np.sqrt(2*step)*np.random.normal(size=self.d)
U_Y = self.potential(Y)
logratio = - U_Y + U_X
if np.log(np.random.uniform(size=1))<=logratio:
X = Y
for k in np.arange(N):
m1 += X / float(N)
m2 += np.power(X, 2) / float(N)
U_X = self.potential(X)
Y = X + np.sqrt(2*step)*np.random.normal(size=self.d)
U_Y = self.potential(Y)
logratio = - U_Y + U_X
if np.log(np.random.uniform(size=1))<=logratio:
X = Y
acc+=1
self.acc_rwm = float(acc)/N
return (m1,m2)
def sampler(self, x0, step, N=10**6, choice="ULA"):
""" General call to a sampler ULA, TULA, TULAc, MALA, RWM, TMALA or TMALAc
:param x0: starting point
:param step: step size of the algorithm
:param N: number of iterations (after burn in period)
:param choice: choice of the algorithm
:return: empirical averages of 1st and 2nd moments
"""
if choice=="ULA":
return self.ULA(x0, step, N=N)
elif choice=="TULA":
return self.TULA(x0, step, N=N)
elif choice=="TULAc":
return self.TULAc(x0, step, N=N)
elif choice=="MALA":
return self.MALA(x0, step, N=N)
elif choice=="RWM":
return self.RWM(x0, step, N=N)
elif choice=="TMALA":
return self.TMALA(x0, step, N=N)
elif choice=="TMALAc":
return self.TMALAc(x0, step, N=N)
else:
print("error sampler not defined")
def analysis(self, x0, NSimu=100, N=10**6, \
stepTab = np.array([10**(-3), 10**(-2), 10**(-1), 1.0]), \
choice="ULA"):
""" Analysis for one given algorithm, different step sizes, and NSimu
independent simulations
Warning: this function may need high computational power
:param x0: starting point
:param NSimu: number of independent simulations
:param N: number of iterations (after burn in period)
:param stepTab: table of different step sizes for the algorithm
:param choice: choice of the algorithm
:return: algorithm, stepTab, initial condition,
average acceptance probability for MALA, RWM, TMALA and TMALAc,
empirical averages of 1st and 2nd moments for the first and last coordinate,
the different step sizes and the different independent simulations (NSimu)
"""
d = self.d
nStep = stepTab.size
# Acceptance probabilities
accMala = np.zeros(nStep)
accRwm = np.zeros(nStep)
accTmala = np.zeros(nStep)
accTmalac = np.zeros(nStep)
# To store the empirical evrages of 1st and 2nd moments for the
# first and last coordinate
moment_1 = np.zeros((nStep, NSimu, 2))
moment_2 = np.zeros((nStep, NSimu, 2))
for i in np.arange(nStep):
m1tab = np.zeros((NSimu, d))
m2tab = np.zeros((NSimu, d))
for k in np.arange(NSimu):
m1,m2 = self.sampler(x0, step=stepTab[i], N=N, choice=choice)
m1tab[k,:] = m1
m2tab[k,:] = m2
accMala[i] += float(self.acc_mala)/NSimu
accRwm[i] += float(self.acc_rwm)/NSimu
accTmala[i] += float(self.acc_tmala)/NSimu
accTmalac[i] += float(self.acc_tmalac)/NSimu
moment_1[i, :] = m1tab[:,[0,-1]]
moment_2[i, :] = m2tab[:,[0,-1]]
if choice=="MALA":
return (choice, stepTab, x0, accMala, moment_1, moment_2)
elif choice=="RWM":
return (choice, stepTab, x0, accRwm, moment_1, moment_2)
elif choice=="TMALA":
return (choice, stepTab, x0, accTmala, moment_1, moment_2)
elif choice=="TMALAc":
return (choice, stepTab, x0, accTmalac, moment_1, moment_2)
else:
return (choice, stepTab, x0, moment_1, moment_2)