rubikscube09
rubikscube09

Reputation: 159

pyMC3 - Using the value of a variable

I am simulating a very basic Bayesian Network using 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. 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.

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.

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. Moreover, if I need to condition on multiple variables I imagine this will be painful.

Upvotes: 2

Views: 362

Answers (1)

Pierre-Henri Wuillemin
Pierre-Henri Wuillemin

Reputation: 541

Using pyAgrum, you could use the notation bn.cpt("Y")[{"X":1}].

enter image description here

Upvotes: 1

Related Questions