Is it possible to construct a Bayesian network with Pyro?

I’m wondering whether it’s possible to construct a directed acyclic graph (DAG), i.e., a Bayesian network and perform posterior inference with Pyro package? For example, a graph describing the following generative process:

x ~ N(\mu, \theta)
e ~ N(f(x), g(x))
y ~ N(h(x,e), m(x,e))

i.e., y < x > e > y, where f(.), g(.), h(.), m(.) are some link functions.

My goal to perform posterior inference p(y|x).

I couldn’t find any related materials from Pyro online tutorial or in this forum. Any guidance would be appreciated!


Yes. Bayes nets can be viewed as probabilistic programs with only assignment statements, no control flow. For example your model would be written:

def model(observed_y):
    x = pyro.sample("x", dist.Normal(mu, theta))
    e = pyro.sample("e", dist.Normal(f(x), g(x)))
    pyro.sample("y", dist.Normal(h(x, e), m(x, e)), obs=observed_y)

Then you could use Pyro’s HMC or SVI (with guide = AutoDiagonalNormal(model)) to perform posterior inference of p(x|y) (I assume you mean p(x|y) rather than p(y|x)).

1 Like

Hey, following up; How would categorical-variable bayesian network be handled? For e.g. a node with 3 states, which has two parents each having 3 states. This would require a conditional probability table… how would that be initialized/generated? I suspect Dirichlet distributions to be involved…


1 Like

Thanks, I was just confused about DAG representation and the conditional distributions of child given parents, but those could just be regular functions with inputs as samples of parents and outputs as the child’s distribution parameter!!

@Volatile Would it be possible for you to share some examples with the BN work? I am also looking for some guidance and tutorial on how to set it up with mixed node types.