是否可以使用分类随机变量在PyMC3中创建分层模型?

时间:2015-08-26 02:09:20

标签: python bayesian categorical-data pymc3

我尝试使用PyMC3比较两个模型(来自Jake Vanderplas&blog;博客的example),但我无法使用我修改过的代码(函数{{1} }和best_theta()在Jake的博文中有解释,该文章以IPython Notebook形式提供):

logL()

引发异常,因为变量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)) 是RV对象,而不是整数(与PyMC2不同),如this question中所述。但是,在我的代码中,choice的值对于使其有效非常重要。

我的问题是,有没有办法访问RV choice的值,或者更一般地使用分类随机变量建立分层模型(即使用分类RV的值来计算对数似然另一辆房车?)

1 个答案:

答案 0 :(得分:2)

我快速刺了一下。但是,这种方法需要进行相当多的改动,因为它通常更方便地对模型进行矢量化。这也揭示了我修复了一个错误(https://github.com/pymc-devs/pymc3/commit/c784c478aa035b5113e175145df8751b0dea0df3),所以你需要从当前的主人更新才能使用。

这是完整的NB: https://gist.github.com/anonymous/c1ada3388a40ae767a8d

它似乎还没有完成,因为结果并不相同,但它是迈向正确方向的一步。