pymc3 多分类贝叶斯网络——如何采样?
pymc3 Multi-category Bayesian network - how to sample?
我已经建立了一个贝叶斯网络,每个节点有 3 个状态,如下所示,并且可以从中读取特定状态的 logp(如代码中所示)。
接下来我想从中取样。在下面的代码中,采样运行但我没有看到输出中三个状态的分布;相反,我看到的是均值和方差,就好像它们是连续的节点一样。如何获得三个状态的后验值?
将 numpy 导入为 np
将 pymc3 导入为 mc
导入 pylab,数学
模型=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)
回答自己的问题:轨迹确实包含离散值,但 mc.summary(trace) 函数设置为计算连续的均值和方差统计数据。要制作离散状态的直方图,请使用 h = hist(trace.get_values(sprinkler)) :-)
我已经建立了一个贝叶斯网络,每个节点有 3 个状态,如下所示,并且可以从中读取特定状态的 logp(如代码中所示)。
接下来我想从中取样。在下面的代码中,采样运行但我没有看到输出中三个状态的分布;相反,我看到的是均值和方差,就好像它们是连续的节点一样。如何获得三个状态的后验值?
将 numpy 导入为 np 将 pymc3 导入为 mc 导入 pylab,数学
模型=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)
回答自己的问题:轨迹确实包含离散值,但 mc.summary(trace) 函数设置为计算连续的均值和方差统计数据。要制作离散状态的直方图,请使用 h = hist(trace.get_values(sprinkler)) :-)