pytorchpyro.ai

pyro: how to specify conditional distribution


I am trying to use pyro for specifying a Bayesian network. I have a child node D which is continuous and it has three discrete nodes are parents, each with 10 possible states:

So, I first define my discrete nodes as:

import torch
import pyro
import pyro.distributions as dist

def model(data):
    A = pyro.sample("A", dist.Dirichlet(torch.ones(10)))
    B = pyro.sample("B", dist.Dirichlet(torch.ones(10)))
    C = pyro.sample("C", dist.Dirichlet(torch.ones(10)))
    

Now, as I understand I need to define P(D|A, B, C). I want to model this as a normal distribution but not sure how to do this conditioning. My plan is to put priors on this distribution parameters and then use MCMC or HMC to estimate the posterior distribution and learn the model parameters.

However, not sure how to proceed with the model definition.


Solution

  • Good thing about pyro is that the model definition is very pythonic. The underlying PyTorch machinery can track dependencies for you.

    All you need is use the samples A, B and C and compute the parameters of the conditional p(D|A,B,C)

    def cond_mean(a, b, c):
        return # use a,b,c to compute mean
    
    def cond_scale(a, b, c):
        return # use a,b,c to compute scale
    
    def model(data):
        A = pyro.sample("A", dist.Dirichlet(torch.ones(10)))
        B = pyro.sample("B", dist.Dirichlet(torch.ones(10)))
        C = pyro.sample("C", dist.Dirichlet(torch.ones(10)))
    
        D = pyro.sample("D", dist.Normal(loc=cond_mean(A, B, C), scale=cond_scale(A, B, C)
        ...