-
Notifications
You must be signed in to change notification settings - Fork 1
/
langevin_traj.py
310 lines (253 loc) · 11.4 KB
/
langevin_traj.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
import numpy as np
from numpy.linalg import norm
from numpy.random import normal, uniform
import matplotlib.pyplot as plt
from time import clock
from scipy.stats import multivariate_normal as MVN
class Potential:
""" Represents a potential function. """
def __init__(self, potential="gaussian", dimension=1):
self.dim = dimension
self.name = potential
# To add a potential, add it to the dictionary below and implement it/its gradient.
self.function, self.gradient, self.gradient2, self.vector_lap_grad = {
"gaussian": (self.gaussian, self.gaussian_grad, None, None),
"double_well": (self.double_well, self.double_well_grad, self.double_well_grad2, self.double_well_vector_lap_grad),
"Ginzburg_Landau": (self.Ginzburg_Landau, self.Ginzburg_Landau_grad, None, None)
}[potential]
# Quantities to store in the Potential class, to avoid needless re-computation.
self.inv_sigma = 1. / np.arange(1, dimension+1, dtype=float) # for Gaussian
def gaussian(self, x):
return 0.5 * np.dot(x, np.multiply(self.inv_sigma, x))
def gaussian_grad(self, x):
return np.multiply(self.inv_sigma, x)
def double_well(self, x):
normx = norm(x)
return 0.25 * normx**4 - 0.5 * normx**2
def double_well_grad(self, x):
return (norm(x)**2 - 1) * x
def double_well_grad2(self, x):
mx = np.matrix(x)
return (norm(x)**2 - 1) * np.identity(self.dim) + 2 * np.transpose(mx) * mx
def double_well_vector_lap_grad(self, x):
return 6*x
def Ginzburg_Landau(self, x, tau=2.0, lamb=0.5, alpha=0.1):
d_ = round(self.dim ** (1./3))
x = np.reshape(x, (d_,d_,d_))
nabla_tilde = sum( norm(np.roll(x, -1, axis=a) - x)**2 for a in [0,1,2] )
return 0.5 * (1. - tau) * norm(x)**2 + \
0.5 * tau * alpha * nabla_tilde + \
0.25 * tau * lamb * np.sum(np.power(x, 4))
def Ginzburg_Landau_grad(self, x, tau=2.0, lamb=0.5, alpha=0.1):
d_ = round(self.dim ** (1./3))
x = np.reshape(x, (d_,d_,d_))
temp = sum( np.roll(x, sgn, axis=a) for sgn in [-1,1] for a in [0,1,2] )
return ((1. - tau) * x + \
tau * lamb * np.power(x, 3) + \
tau * alpha * (6*x - temp)).flatten()
class Evaluator:
""" Evaluates a set of Langevin algorithms on given potentials. """
def __init__(self, potential="gaussian", dimension=1, N=10**2, burn_in=10**2, N_sim=5, x0=[0], step=0.01, timer=None):
self.dim = dimension
# To add an algorithm, add it to the dictionary below and implement it as a class method.
self.algorithms = {
"ULA": self.ULA,
"tULA": self.tULA,
"tULAc": self.tULAc,
"MALA": self.MALA,
"RWM": self.RWM,
"tMALA": self.tMALA,
"tMALAc": self.tMALAc,
"tHOLA": self.tHOLA,
"LM": self.LM,
"tLM": self.tLM,
"tLMc": self.tLMc
}
self.N = N
self.burn_in = burn_in
self.N_sim = N_sim
self.x0 = x0
self.step = step
self.timer = timer
if timer:
self.N = 10**10 # ~~practically infinity
self.start_time = clock()
self.potential = Potential(potential, dimension)
# invoked by self.potential.funtion(parameters), self.potential.gradient(parameters)
def ULA(self):
x = np.array(self.x0)
sample = np.zeros((self.dim, self.N + self.burn_in), dtype=float)
sqrtstep = np.sqrt(2*self.step)
for i in range(self.burn_in + self.N):
if self.timer and clock() - self.start_time > self.timer:
break
sample[:,i]=x
x += -self.step * self.potential.gradient(x) + sqrtstep * normal(size=self.dim)
return sample #i+1 = no. of iterations
def tULA(self, taming=(lambda g, step: g/(1. + step*norm(g)))):
x = np.array(self.x0)
sample = np.zeros((self.dim, self.N + self.burn_in), dtype=float)
sqrtstep = np.sqrt(2*self.step)
for i in range(self.burn_in + self.N):
if self.timer and clock() - self.start_time > self.timer:
break
sample[:,i]=x
x += -self.step * taming(self.potential.gradient(x), self.step) + sqrtstep * normal(size=self.dim)
return sample
def tULAc(self):
# coordinate-wise taming function
return self.tULA(lambda g, step: np.divide(g, 1. + step*np.absolute(g)))
def MALA(self):
acc = 0 # acceptance probability
sample = np.zeros((self.dim, self.N + self.burn_in), dtype=float)
x = np.array(self.x0)
for i in range(self.burn_in + self.N):
if self.timer and clock() - self.start_time > self.timer:
break
sample[:,i]=x
U_x, grad_U_x = self.potential.function(x), self.potential.gradient(x)
y = x - self.step * grad_U_x + np.sqrt(2 * self.step) * normal(size=self.dim)
U_y, grad_U_y = self.potential.function(y), self.potential.gradient(y)
logratio = -U_y + U_x + 1./(4*self.step) * (norm(y - x + self.step*grad_U_x)**2 \
-norm(x - y + self.step*grad_U_y)**2)
if np.log(uniform(size = 1)) <= logratio:
x = y
if i >= self.burn_in:
acc += 1
return sample
def RWM(self):
acc = 0 # acceptance probability
sample = np.zeros((self.dim, self.N + self.burn_in), dtype=float)
x = np.array(self.x0)
for i in range(self.burn_in + self.N):
if self.timer and clock() - self.start_time > self.timer:
break
sample[:,i]=x
y = x + np.sqrt(2*self.step)*normal(size=self.dim)
logratio = self.potential.function(x) - self.potential.function(y)
if np.log(uniform(size = 1)) <= logratio:
x = y
if i >= self.burn_in:
acc += 1
return sample
def tMALA(self, taming=(lambda g, step: g/(1. + step*norm(g)))):
acc = 0 # acceptance probability
sample = np.zeros((self.dim, self.N + self.burn_in), dtype=float)
x = np.array(self.x0)
for i in range(self.burn_in + self.N):
if self.timer and clock() - self.start_time > self.timer:
break
sample[:,i]=x
U_x, grad_U_x = self.potential.function(x), self.potential.gradient(x)
tamed_gUx = taming(grad_U_x, self.step)
y = x - self.step * tamed_gUx + np.sqrt(2*self.step) * normal(size=self.dim)
U_y, grad_U_y = self.potential.function(y), self.potential.gradient(y)
tamed_gUy = taming(grad_U_y, self.step)
logratio = -U_y + U_x + 1./(4*self.step) * (norm(y - x + self.step*tamed_gUx)**2 - norm(x - y + self.step*tamed_gUy)**2)
if np.log(uniform(size = 1)) <= logratio:
x = y
if i >= self.burn_in:
acc += 1
return sample
def tMALAc(self):
return self.tMALA(lambda g, step: np.divide(g, 1. + step * np.absolute(g)))
def tHOLA(self):
sample = np.zeros((self.dim, self.N + self.burn_in), dtype=float)
x = np.array(self.x0)
for i in range(self.burn_in + self.N):
if self.timer and clock() - self.start_time > self.timer:
break
sample[:,i]=x
norm_x = norm(x)
grad_U = self.potential.gradient(x)
norm_grad_U = norm(grad_U)
grad_U_gamma = grad_U / (1 + (self.step * norm_grad_U)**1.5)**(2./3)
grad2_U = self.potential.gradient2(x)
norm_grad2_U = norm(grad2_U)
grad2_U_gamma = grad2_U / (1 + self.step * norm_grad2_U)
laplacian_grad_U = self.potential.vector_lap_grad(x)
laplacian_grad_U_gamma = laplacian_grad_U / (1 + self.step**0.5 * norm_x * norm(laplacian_grad_U))
grad2_U_grad_U_gamma = np.matmul(grad2_U, grad_U).A1 / (1 + self.step * norm_x * norm_grad2_U * norm_grad_U)
x += -self.step * grad_U_gamma + 0.5 * self.step**2 * (grad2_U_grad_U_gamma - laplacian_grad_U_gamma) + \
np.sqrt(2*self.step) * normal(size=self.dim) - np.sqrt(2) * np.matmul(grad2_U_gamma, normal(size=self.dim)).A1 * np.sqrt(self.step**3/3)
return sample
def LM(self):
x = np.array(self.x0)
sample = np.zeros((self.dim, self.N + self.burn_in), dtype=float)
sqrtstep = np.sqrt(0.5 * self.step)
gaussian = normal(size=self.dim)
for i in range(self.burn_in + self.N):
if self.timer and clock() - self.start_time > self.timer:
break
sample[:,i]=x
gaussian_plus1 = normal(size=self.dim)
x += -self.step * self.potential.gradient(x) + sqrtstep * (gaussian + gaussian_plus1) * 0.5
gaussian = gaussian_plus1
return sample
def tLM(self, taming=(lambda g, step: g/(1. + step * norm(g)))):
x = np.array(self.x0)
sample = np.zeros((self.dim, self.N + self.burn_in), dtype=float)
sqrtstep = np.sqrt(0.5 * self.step)
gaussian = normal(size=self.dim)
for i in range(self.burn_in + self.N):
if self.timer and clock() - self.start_time > self.timer:
break
sample[:,i]=x
gaussian_plus1 = normal(size=self.dim)
x += -self.step * taming(self.potential.gradient(x), self.step) + sqrtstep * (gaussian + gaussian_plus1) * 0.5
gaussian = gaussian_plus1
return sample
def tLMc(self):
return self.tLM(lambda g, step: np.divide(g, 1. + step * np.absolute(g)))
def sampler(self, algorithm="ULA"):
if self.timer:
self.start_time = clock()
return self.algorithms[algorithm]()
potential = 'double_well'
d = 2
N = 10**5
burn_in = 10**2
N_sim = 1
x0 = np.array([0] + [0]*(d-1), dtype=float)
step = 0.1
# TIMER MODE: number of seconds which we allow the algorithms to run
# To run normally without a timer, omit the last parameter
timer = 2.5
e = Evaluator(potential, dimension=d, N=N, burn_in=burn_in, N_sim=N_sim, x0=x0, step=step)
MALA_data = e.sampler("MALA")
tULA_data = e.sampler("RWM")
plt.hist(MALA_data[0,burn_in:-1], density=True, bins=100)
plt.hist(tULA_data[0,burn_in:-1], density=True, bins=100)
plt.show()
# fig = plt.figure()
# plt.title("Trace" )
# plt.plot(MALA_data[0],MALA_data[1],'r')
# plt.plot(tULA_data[0],tULA_data[1],'g')
#
# from matplotlib import cm
# from scipy.stats import multivariate_normal as MVN
#
# """Plot heatmap of 2D MVN distribution """
# # Our 2-dimensional distribution will be over variables X and Y
# N = 600
# X = np.linspace(min(np.append(MALA_data[0], tULA_data[0])), max(np.append(MALA_data[0],tULA_data[0])), N)
# Y = np.linspace(min(np.append(MALA_data[1], tULA_data[1])), max(np.append(MALA_data[1],tULA_data[1])), N)
# X, Y = np.meshgrid(X, Y)
#
# # Mean vector and covariance matrix
# mu = np.array([0., 0])
# Sigma = np.diag(np.arange(1, 3, dtype=float))
#
# # Pack X and Y into a single 3-dimensional array
# pos = np.empty(X.shape + (2,))
# pos[:, :, 0] = X
# pos[:, :, 1] = Y
#
# F = MVN(mu, Sigma)
# Z = F.pdf(pos)
#
# ax = fig.gca()
# ax.contourf(X, Y, Z, zdir='z', levels=9, offset=-0.15, cmap=cm.hot, alpha=0.5)
# cset = plt.contour(X, Y, Z, cmap=cm.hot, alpha=0.5)
#
# plt.show()