简体   繁体   English

pyMC3 - 使用变量的值

[英]pyMC3 - Using the value of a variable

I am simulating a very basic Bayesian Network using pyMC3.我正在使用 pyMC3 模拟一个非常基本的贝叶斯网络。 In this simulation, I have only categorical variables.在这个模拟中,我只有分类变量。 Given the value of a variable, I would like to set the distribution of another variable based on output from a Pandas Dataframe that I have used to store conditional probabilities.给定一个变量的值,我想根据我用来存储条件概率的 Pandas Dataframe 中的 output 设置另一个变量的分布。 For example, if x is a pyMC3 random variable, and x=1 in an instance of the simulation, then I would like to access p_y_cond_x.loc[x] , which in this instance is just p_y_cond_x.loc[1] , with here p_y_cond_x is a pre-computed (using data) conditional probability table stored as a pandas series.例如,如果x是 pyMC3 随机变量,并且在模拟实例中x=1 ,那么我想访问p_y_cond_x.loc[x] ,在这种情况下只是p_y_cond_x.loc[1] ,这里p_y_cond_x是一个预先计算(使用数据)的条件概率表,存储为 pandas 系列。

Is there any easy way to do this?有什么简单的方法可以做到这一点? Unfortunately x is not an integer when instantiating the model (say, using a with block), so I'm not sure how I could access its value and do the above when the simulation is running.不幸的是x在实例化 model 时(例如,使用with块)不是 integer ,所以我不确定如何在模拟运行时访问它的值并执行上述操作。

I have seen solutions using pm.math.switch , but unfortunately my variables are ternary so I will need to use two switches for each conditional.我见过使用pm.math.switch的解决方案,但不幸的是我的变量是三元的,所以我需要为每个条件使用两个开关。 Moreover, if I need to condition on multiple variables I imagine this will be painful.此外,如果我需要以多个变量为条件,我想这会很痛苦。

Using pyAgrum, you could use the notation bn.cpt("Y")[{"X":1}] .使用 pyAgrum,您可以使用符号bn.cpt("Y")[{"X":1}]

在此处输入图像描述

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

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