pymc3多类别贝叶斯网络 - 如何样本?

时间:2017-04-21 10:58:48

标签: pymc pymc3

我已经建立了一个贝叶斯网络,每个节点有3个状态,如下所示,并且可以从中读取特定状态的logp(如代码中所示)。

接下来我想从中提取样品。在下面的代码中,采样运行,但我没有看到输出中三个状态的分布;相反,我看到一个均值和方差,好像它们是连续的节点。我如何得到这三个州的后验?

将numpy导入为np 将pymc3导入为mc 导入pylab,数学

model = mc.Model() 与模特:

rain = mc.Categorical('rain', p = np.array([0.5, 0. ,0.5]))

sprinkler = mc.Categorical('sprinkler', p=np.array([0.33,0.33,0.34]))

CPT = mc.math.constant(np.array([ [ [.1,.2,.7], [.2,.2,.6], [.3,.3,.4] ],\
                                  [ [.8,.1,.1], [.3,.4,.3], [.1,.1,.8] ],\
                                  [ [.6,.2,.2], [.4,.4,.2], [.2,.2,.6] ] ]))

p_wetgrass = CPT[rain, sprinkler]
wetgrass = mc.Categorical('wetgrass', p_wetgrass)

#brute force search (not working)
for val_rain in range(0,3):
    for val_sprinkler in range(0,3):
        for val_wetgrass in range(0,3):
            lik = model.logp(rain=val_rain, sprinkler=val_sprinkler, wetgrass=val_wetgrass )
            print([val_rain, val_sprinkler, val_wetgrass, lik])

#sampling (runs but don't understand output)
if 1:
    niter = 10000  # 10000
    tune = 5000  # 5000
    print("SAMPLING:")
    #trace = mc.sample(20000, step=[mc.BinaryGibbsMetropolis([rain, sprinkler])], tune=tune, random_seed=124)
    trace = mc.sample(20000, tune=tune, random_seed=124)

    print("trace summary")
    mc.summary(trace)

1 个答案:

答案 0 :(得分:1)

回答自己的问题:跟踪确实包含离散值,但mc.summary(trace)函数设置为计算连续均值和方差统计量。要制作离散状态的直方图,请使用h = hist(trace.get_values(sprinkler)):-)