Python 使用dirichlet先验从pyMC3中绘制分类向量

Python 使用dirichlet先验从pyMC3中绘制分类向量,python,categorical-data,pymc3,dirichlet,probabilistic-programming,Python,Categorical Data,Pymc3,Dirichlet,Probabilistic Programming,我想画一个分类向量,它的先验是Dirichlet分布的乘积。类别是固定的,类别向量中的每个元素对应一个不同的Dirichlet先验。这是一个长度为33的分类向量,包含4个类别,使用Previor和Dirichlet设置 将pymc3导入为pm 将pm.Model()作为model3: θ=pm.Dirichlet(name='theta',a=np.one((33,4)),shape=(33,4)) seq=[pm.category(name='seq_{}'。格式(str(i)),p=thet

我想画一个分类向量,它的先验是Dirichlet分布的乘积。类别是固定的,类别向量中的每个元素对应一个不同的Dirichlet先验。这是一个长度为33的分类向量,包含4个类别,使用Previor和Dirichlet设置

将pymc3导入为pm
将pm.Model()作为model3:
θ=pm.Dirichlet(name='theta',a=np.one((33,4)),shape=(33,4))
seq=[pm.category(name='seq_{}'。格式(str(i)),p=theta[i,:],shape=(1,))表示范围(33)]
step1=pm.Metropolis(vars=[theta])
步骤2=[pm.CategoricalGibbsMetropolis(vars=[i])表示序列中的i]
trace=pm.sample(50,步骤=[step1]+[step2中i代表i])

然而,这种方法很麻烦,因为我必须做一些数组索引才能得到分类向量。有更好的方法吗?

您不需要指定形状。请注意,您设置它的方式有33个不同的分类变量;我想这是你的本意。以下是更简单的方法:

with pm.Model() as model: 
    theta = pm.Dirichlet(name='theta',a=np.ones(4)) 
    children = [pm.Categorical(f"seq_{i}", p=theta) for i in range(33)] 

您不需要指定形状。请注意,您设置它的方式有33个不同的分类变量;我想这是你的本意。以下是更简单的方法:

with pm.Model() as model: 
    theta = pm.Dirichlet(name='theta',a=np.ones(4)) 
    children = [pm.Categorical(f"seq_{i}", p=theta) for i in range(33)] 

这可能也值得发布。虽然有些开发人员(比如@colcarroll)经常这样做,但更多的开发人员遵循这一论述。这可能也值得发布。虽然有些开发人员(比如@colcarroll)经常这样做,但更多的开发人员遵循这篇文章。我意识到这篇文章是很久以前写的,但这个问题是我用来尝试解决它的资源之一;我找不到很多这样的例子,我意识到这篇文章是很久以前写的,但这个问题是我用来尝试解决它的资源之一;我找不到很多这样的例子