简体   繁体   English

如何在pymc3中声明具有更改参数的二项分布变量

[英]How to declare a binomial distribution variable with changing parameters in pymc3

I would like to declare a binomial variable that is intermediate and not observed, used to compute another deterministic variable that is observed.我想声明一个中间且未观察到的二项式变量,用于计算观察到的另一个确定性变量。 If I had the observations, I could declare the following and get the model pictured below:如果我有观察结果,我可以声明以下内容并获得如下图所示的 model:

import pymc3
with pymc3.Model() as model:
    rate = pymc3.Beta('rate', alpha=2, beta=2)
    input_var = pymc3.Data('input_var', [1, 2, 3])
    intermediate_var = pymc3.Binomial('intermediate_var', n=input_var, p=rate, observed=[1, 1, 1])
    output_var = pymc3.Deterministic('output_var', intermediate_var)
pymc3.model_to_graphviz(model)

示例模型

But, I don't have observations of the intermediate variable, only of the output variable, and this does not work, giving wrong number of dimensions error:但是,我没有观察到中间变量,只有 output 变量,这不起作用,给出错误的维数错误:

import pymc3
with pymc3.Model() as model:
    rate = pymc3.Beta('rate', alpha=2, beta=2)
    input_var = pymc3.Data('input_var', [1, 2, 3])
    intermediate_var = pymc3.Binomial('intermediate_var', n=input_var, p=rate)
    output_var = pymc3.Deterministic('output_var', intermediate_var)

Obviously, this is an innocent example, and someone could say that how is that I have observations of the output variable and not the intermediate one in this case, but in my model, the output variable depends on several intermediate variables that depend in other input variables as well.显然,这是一个无辜的例子,有人可能会说,我如何观察 output 变量而不是在这种情况下的中间变量,但在我的 model 中,Z78E6221F6393D1356681DB398DZF1 依赖于其他输入变量中的几个中间变量变量也是如此。 This is a minimal example trying to explain what I want to do.这是一个试图解释我想要做什么的最小示例。

The same happens if I change the Deterministic by a Normal with observed data.如果我用观察到的数据通过Normal更改Deterministic ,也会发生同样的情况。

If the plate diagram is to be taken literally, the dimensionality of the intermediate_var needs to be declared, since it is really three random variables each with a distinct n parameterized by the entries in input_var .如果要从字面上理解车牌图,则需要声明intermediate_var的维度,因为它实际上是三个随机变量,每个变量都有一个不同的n ,由input_var中的条目参数化。 This is done with the shape argument:这是通过shape参数完成的:

import pymc3

with pymc3.Model() as model:
    rate = pymc3.Beta('rate', alpha=2, beta=2)
    input_var = pymc3.Data('input_var', [1, 2, 3])
    intermediate_var = pymc3.Binomial('intermediate_var', n=input_var, p=rate, shape=3)
    output_var = pymc3.Deterministic('output_var', intermediate_var)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM