Skip to content

Instantly share code, notes, and snippets.

@alanzchen
Last active March 10, 2017 16:03
Show Gist options
  • Save alanzchen/49e160763bd183c3a84d53ea219b2b5b to your computer and use it in GitHub Desktop.
Save alanzchen/49e160763bd183c3a84d53ea219b2b5b to your computer and use it in GitHub Desktop.
Metropolis-Hasting Algorithm 2 Markov Chain
# coding: utf-8
#r = 8
# p is a probability constructed in the following way:
# node position: 0-0 0-1 0-2 1-0 1-1 1-2 2-0 2-1 2-2
# probability: 1/16 1/8 1/16 1/8 1/4 1/8 1/16 1/8 1/16
# so we have 8 nodes here, and the output will be a way such that the probability is stationary.
p = [0.0625 ,0.1250 ,0.0625 ,0.1250 ,0.2500 ,0.1250,0.0625 ,0.1250 ,0.0625]
#p = [0.5, 0.33333333, 0.166666667]
N = len(p)
r = N-1
def prob(i, j):
if i != j:
temp = float(1)/r
#print temp
joi = p[j]/p[i]
#print ioj
temp = temp * min(1,joi)
return temp
else:
temp = 0
for j in range(N):
if i != j:
temp += prob(i, j)
return 1 - temp
# print prob(1, 1)
templist = []
for i in range(N):
nlist = []
for j in range(N):
nlist.append(prob(i, j))
#print "Prob(" + str(i) + "->" + str(j) + ") is " + str(prob(i, j))
print nlist
if sum(nlist) != 1:
print "WARNING: Sum is not 1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment