-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcmc.py
172 lines (147 loc) · 6.07 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
import torch
########################################
# ULA #
########################################
class ULA:
def __init__(self, model, metric, gradient_step, random_step,
initialization):
self.model = model
self.metric = random_step * torch.sqrt(torch.tensor(2.)) * metric
self.gamma = gradient_step
self.current_point = initialization
def proposition(self, x):
prop = self.metric @ torch.randn_like(x)
prop += x + self.gamma * self.model.log_gradient(x)
return prop
def unadjusted_step(self):
proposition = self.proposition(self.current_point)
self.current_point = proposition
def fit(self, nbr_samples=500):
generated_samples = 0
samples = [self.current_point.numpy()]
while generated_samples < nbr_samples:
self.unadjusted_step()
samples.append(self.current_point.numpy())
generated_samples += 1
return samples
########################################
# MALA #
########################################
class MALA:
def __init__(self, model, metric, gradient_step, random_step,
initialization):
self.model = model
self.metric = random_step * torch.sqrt(torch.tensor(2.)) * metric
self.gamma = gradient_step
self.current_point = initialization
def proposition(self, x):
prop = self.metric @ torch.randn_like(x)
prop += x + self.gamma * self.model.log_gradient(x)
return prop
def adjusted_step(self):
threshold = torch.rand(1).item()
proposition = self.proposition(self.current_point)
alpha = self.model.density(proposition) / \
self.model.density(self.current_point)
alpha = alpha.item()
try:
alpha = min(1., alpha)
except:
print("Numerical Error")
alpha = 1.
if threshold < alpha:
self.current_point = proposition
return (threshold < alpha)
def fit(self, nbr_samples=1000):
generated_samples = 0
rejected_samples = 0
samples = [self.current_point.numpy()]
while generated_samples < nbr_samples:
if self.adjusted_step():
samples.append(self.current_point.numpy())
generated_samples += 1
else:
rejected_samples += 1
print(f"Acceptation ratio : \
{generated_samples/(generated_samples + rejected_samples)}")
return samples
def adjusted_fit(self, nbr_samples=1000, acceptation_ratio=0.63,
update=200, increase_factor=1.2, decrease_factor=0.9,
increase_acceleration=1.05, decrease_acceleration=0.95,
verbose=True):
# Home made scheme - No theoretical guarantee that this works better
# then the classical scheme (i.e. faster convergence).
# Theoretical conditions for convergence are satisfied however
generated_samples = 0
rejected_samples = 0
samples = [self.current_point.numpy()]
while generated_samples < nbr_samples:
if self.adjusted_step():
samples.append(self.current_point.numpy())
generated_samples += 1
else:
rejected_samples += 1
if (generated_samples + rejected_samples) % update == 0:
ratio = generated_samples / \
(generated_samples + rejected_samples)
if ratio > acceptation_ratio:
# increase exploration
self.metric *= increase_factor
# next time, increase exploration even more
increase_factor *= increase_acceleration
decrease_factor *= increase_acceleration
# decrease_acceleration cannot be greater than 1
decrease_factor = min(decrease_factor, 1. - 1e-7)
else:
# decrease exploration
self.metric *= decrease_factor
decrease_factor *= decrease_acceleration
increase_factor *= decrease_acceleration
# increase_acceleration cannot be smaller than 1
increase_factor = max(increase_factor, 1 + 1e-7)
if verbose:
print(f"Acceptation ratio : \
{generated_samples/(generated_samples + rejected_samples)}")
return samples
########################################
# MALA #
########################################
class GMMALA:
def __init__(self, model, metric, gradient_step, random_step,
initialization):
self.model = model
self.metric = lambda x: random_step * \
torch.sqrt(torch.tensor(2.)) * metric(x)
self.gamma = gradient_step
self.current_point = initialization
def proposition(self, x):
prop = self.metric(x) @ torch.randn_like(x)
prop += x + self.gamma * self.model.log_gradient(x)
return prop
def adjusted_step(self):
threshold = torch.rand(1).item()
proposition = self.proposition(self.current_point)
alpha = self.model.density(proposition) / \
self.model.density(self.current_point)
alpha = alpha.item()
try:
alpha = min(1., alpha)
except:
print("Numerical Error")
alpha = 1.
if threshold < alpha:
self.current_point = proposition
return (threshold < alpha)
def fit(self, nbr_samples=1000):
generated_samples = 0
rejected_samples = 0
samples = [self.current_point.numpy()]
while generated_samples < nbr_samples:
if self.adjusted_step():
samples.append(self.current_point.numpy())
generated_samples += 1
else:
rejected_samples += 1
print(f"Acceptation ratio : \
{generated_samples/(generated_samples + rejected_samples)}")
return samples