【发布时间】:2015-08-26 02:09:20
【问题描述】:
我正在尝试使用 PyMC3 比较两个模型(来自 Jake Vanderplas 博客的example),但我无法让我修改后的代码工作(函数 best_theta() 和 logL() 在 Jake 的博客中进行了解释帖子,以IPython Notebook 形式提供):
degrees = [1, 2, 3]
# best_theta() finds the best set of parameters for a given model
thetas = [best_theta(d) for d in degrees]
n = len(degrees)
prob = np.array([ 1 for _ in degrees ])
# model specs
from pymc3 import Model, Dirichlet, Categorical, DensityDist
with Model() as bfactor:
choices = Dirichlet('choices', prob, shape=prob.shape[0])
choice = Categorical('choice', choices)
indmodel = [0] * len(degrees)
for i, d in enumerate(degrees):
# logL() calculates the log-likelihood for a given model
indmodel[i] = DensityDist('indmodel', lambda value: logL(thetas[i]))
fullmodel = DensityDist('fullmodel', lambda value: indmodel[choice].logp(value))
这引发了一个异常,因为变量 choice 是一个 RV 对象,而不是一个整数(与 PyMC2 中不同),正如 this question 中所讨论的。然而,在我的代码中,choice 的值对于让它工作很重要。
我的问题是,有没有办法访问 RV choice 的值,或者更一般地使用分类随机变量设置分层模型(即使用分类 RV 的值来计算另一个的对数似然房车)?
【问题讨论】:
标签: python bayesian categorical-data pymc3