-
Notifications
You must be signed in to change notification settings - Fork 0
/
MH.py
33 lines (26 loc) · 880 Bytes
/
MH.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
"""
MH.py
-----
Author: Michael Dickens <[email protected]>
Created: 2016-03-29
Implementation of the Metropolis-Hastings algorithm for approximating Bayesian posteriors. The main purpose of implementing here is so I can easily test and get a handle on the algorithm before implementing it in Visual Basic.
"""
import numpy as np
def get_candidate(q, given=None):
if given == None:
# draw from initial guess distribution intended to approximate
# final distribution
pass
else:
pass
def metropolis_hastings(q, num_iters):
"""
`q`: proposal distribution
`pi`: full joint density
"""
x = get_candidate(q)
for _ in range(num_iters):
x_cand = get_candidate(q, x)
p_accept = (q(x, x_cand) * pi(x_cand)) / (q(x_cand, x) * pi(x))
if np.random.rand() < p_accept:
x = x_cand