【问题标题】:Is it possible to create a hierarchical model in PyMC3 using Categorical random variables?是否可以使用分类随机变量在 PyMC3 中创建分层模型?
【发布时间】: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


    【解决方案1】:

    我对此进行了快速尝试。然而,这种方法需要进行相当大的改变,因为它通常更方便地对模型进行矢量化。这也揭示了我修复的一个错误 (https://github.com/pymc-devs/pymc3/commit/c784c478aa035b5113e175145df8751b0dea0df3),因此您需要从当前的 master 进行更新才能使其正常工作。

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

    它似乎还没有完全奏效,因为结果并不相同,但这是朝着正确方向迈出的一步。

    【讨论】:

    • 嘿,谢谢!我喜欢你的解决方案,模型规格比我的更清晰。正如您可能知道的那样,我刚刚开始使用 PyMC3。因此,据我所知,您可以像在当前 MCMC 步骤中引用它们的当前值一样引用 RV 对象,但只能在另一个 RV 的上下文中。换句话说,您的coeffs[choice, :(choice+1)] sn-p 是合法的,因为coeffs 本身就是一个(集合)RV,而我的indmodel[choice] sn-p 是非法的,因为indmodel 是一个常规的ol' 列表。对吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 2023-03-09
    • 1970-01-01
    • 2014-05-28
    • 2013-08-25
    相关资源
    最近更新 更多