来自 John Kruschke 的书,第 7 章,Pg。120(为简洁起见):
一个政治家不断地在一系列岛屿上从一个岛屿到另一个岛屿旅行......他的目标是按比例访问所有岛屿,以便他在人口较多的岛屿上花费更多的时间......他没有甚至确切地知道有多少个岛屿!.. 他的顾问可以向岛上的市长询问他们在岛上有多少人。当政客提议访问相邻的岛屿(向东或向西,通过掷硬币)时,他们可以询问该相邻岛屿的市长该岛上有多少人。
我写了一些 Python 代码,A岛链在哪里run_Metropolis(A, N, start, burn_in)并进行计算。代码被故意简化以使其更易于阅读。
import random
def run_Metropolis(A, N, start, burn_in):
''' A: the distribution to be predicted (a list).
N: the number of trials / moves
start: starting index in A
burn_in: the number of initial moves to be discarded.
'''
Posterior = [0] * len(A) # to be returned
cur_pos = start # starting index
for i in range(N):
# proposal distribution is (-1, +1) cordinates, takng into account the list boundaries
if cur_pos<len(A)-1 and cur_pos>0:
proposed_positions = [cur_pos-1, cur_pos+1]
elif cur_pos == len(A)-1:
#proposed_positions =[cur_pos-1] # if this is enforced, the posterior does not look like A
proposed_positions = [0,cur_pos-1]
elif cur_pos == 0 :
#proposed_positions = [cur_pos+1] # if this is enforced, the posterior does not look like A
proposed_positions = [len(A)-1, cur_pos+1]
proposed_pos = random.choice(proposed_positions)
# decide whether you will move
if A[proposed_pos] > A[cur_pos]: # definitely move
cur_pos = proposed_pos
else: # move with a prob proportional to how close A[cur_pos] is to A[proposed_pos]
u = random.random() # sample from uniform distribution from 0 to 1
if float(A[proposed_pos])/A[cur_pos] > u: # move to proposed
cur_pos = proposed_pos
else: # stay at cur_pos
pass
# if past burn_in, increment the location by 1 in the posterior distribution.
if i > burn_in:
Posterior[cur_pos] += 1
return Posterior
A = [20,30,10,50,80,20,90] # populations of the islands
posterior_A = run_Metropolis(A, 4000, 3, 1000)
# Validate the sampling works:
[n/float(sum(A)) for n in A]
> [0.0666, 0.1, 0.0333, 0.166, 0.266, 0.066, 0.3]
[n/float(sum(posterior_A)) for n in posterior_A]
> [0.0593, 0.0910, 0.0393, 0.177, 0.263, 0.0666, 0.302]
所以,posterior_A确实反映了人口分布A。Kruschke 没有说清楚的是,为了使这个工作,岛链需要是循环的,即你需要能够从第 7 个岛旅行到第 1 个岛,反之亦然。否则,posterior_A看起来不像A。您可以通过使用注释掉的边界提案分布来测试这一点,这些分布不假设周期性。
问题:
1.这是因为违反了遍历性假设吗?
2.我们能否修改接受标准(移动决策)以解决最东和最西岛屿缺乏对称性的问题?(我猜这是 Metropolis-Hastings)
3.如果可以,能否请您修改代码说明一下?