我已经用 Python 编写了 CRP 问题的代码。问题本身可以在这里找到: http ://cog.brown.edu/~mj/classes/cg168/slides/ChineseRestaurants.pdf
并对其进行简短描述:假设我们要将进入餐厅的人分配给可能无限数量的桌子。如果 $z_i$ 表示为进入餐厅的第 $i$' 个人分配的随机变量,则以下应成立:
对于 $n_a>0$ 的概率 $p(z_i=a|z_1,...,z_{i-1})=\frac{n_a}{i-1+\alpha},第 $i$' 个人将坐在桌 $a$ 并且概率 $p(z_i=a|z_1,...,z_{i-1})=\frac{\alpha}{i-1+\alpha} $i$'th 人将围坐在一张新桌子旁。
我不太确定我的代码是否正确,因为我很惊讶最终的表格数量有多小。
如果有人能说出实施是否正确,如果是,是否有任何可能的改进,我会很高兴。
import numpy as np
def CRP(alpha,N):
"""Chinese Restaurant Process with alpha as concentration parameter and N
the number of sample"""
#Array which will save for each i, the number of people people sitting
#until table i
summed=np.ones(1) #first person assigned to the first table
for i in range(1,N):
#A loop that assigns the people to tables
#randind represent the random number from the interval [1,i-1+alpha]
randind=(float(i)+alpha)*np.random.uniform(low=0.0, high=1.0, size=1)
#update is the index for the table that the person should be placed which
#if greater than the total number, will be placed in a new table
update=np.searchsorted(summed,randind,side='left')
if randind>i:
summed=np.append(summed,i+1)
else:
zerovec=np.zeros(update)
onevec=np.ones(summed.size-update)
summed+=np.append(zerovec,onevec)
#This part converts summed array to tables array which indicates the number
#of persons assigned to that table
tables=np.zeros(summed.size)
tables[0]=summed[0]
for i in range(1,summed.size):
tables[i]=summed[i]-summed[i-1]
return tables
a=CRP(0.9999,1000)
print a