-
Notifications
You must be signed in to change notification settings - Fork 0
/
metropolis.py
37 lines (27 loc) · 979 Bytes
/
metropolis.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
from abc import ABC, abstractmethod
import numpy as np
class Metropolis(ABC):
def __init__(self, itr=1000, cur=1):
self._post = []
self._iter = itr
self._burn = int(itr/5)
self._current = cur
self._accepted = 0
def sample(self):
self._post = [self._current]
for i in range(self._iter):
proposed = self.proposal(self._current)
#print(self._current, proposed)
p = min(self.target(proposed)/self.target(self._current), 1)
if np.random.random() < p:
self._current = proposed
if i >= self._burn:
self._accepted += 1
self._post.append(self._current)
return self._post[self._burn:]
@abstractmethod
def target(self, *args):
raise NotImplementedError('Implement me bro!')
@abstractmethod
def proposal(self, *args):
raise NotImplementedError('Implement me bro!')