# Computing Joint and Conditional Probabilities from a Pyro Model

I am not very much familiar with implementing probabilistic programming models. I have written a model in tensorflow probability and want to replicated similar behaviour using pyro. Following is the tensorflow probability model code

``````import tensorflow as tf
import tensorflow_probability as tfp

# DAG:  Weather -> Happiness
def model1(weather_prob, weather_to_happiness_probs):
weather = yield tfp.distributions.JointDistributionCoroutine.Root(
tfp.distributions.Bernoulli(probs=weather_prob, name='weather')
)
happiness = yield tfp.distributions.Bernoulli(
probs=weather_to_happiness_probs[weather],
name='happiness'
)

theta_weather = tf.constant(0.8) # prob of weather being good
theta_happiness = tf.constant([0.7, 0.95])

joint_model1 = tfp.distributions.JointDistributionCoroutineAutoBatched(
lambda: model1(theta_weather, theta_happiness)
)

# generate data
dataset = joint_model1.sample(10)

# compute joint probability of the sample in the dataset
joint_model1.prob(dataset)

# compute likelihood of dataset: likelihood of data is individual probability of
# samples multiplied together
tf.reduce_prod(joint_model1.prob(dataset))

``````

Here I have create a model weather to season probabilistic model and generated some sample from this model and trying to compute joint probability and likelihood of the dataset. Now, I want to repeat same exercise in pyro but I am not sure how to compute joint probability in pyro given a pryo model.

Primarily, Given a pyro model, I have two questions
(a) How to compute joint probability using model : is there any pyro methods available?
(b) How to compute individual/conditional probability given a pyro model?

Following is my attempt to create pyro model replicating the above model created in tensorflow probability

``````import torch
import pyro
import pyro.distributions as dist

def pyro_model1(weather_prob, weather_to_happiness_probs):
weather = pyro.sample("weather", dist.Bernoulli(probs=weather_prob))
happiness = pyro.sample("happiness", dist.Bernoulli(probs=weather_to_happiness_probs[weather.type(torch.int)]))
return weather, happiness

theta_weather = torch.tensor(0.8)
theta_happiness = torch.tensor([0.7, 0.95])

pyro.render_model(pyro_model1, model_args=(theta_weather, theta_happiness),
render_distributions=True, render_params=True,
render_deterministic=True)

# create a predictive model and sample data from it
predictive_model = pyro.infer.Predictive(pyro_model1, num_samples=10)
dataset = predictive_model(theta_weather, theta_happiness)

print(dataset)

# compute joint probability of the sample in the dataset
# TODO

# compute likelihood of dataset: likelihood of data is individual probability of
# TODO
``````

Also the `data type` weather sample was not `int/torch.int` shouldnâ€™t a sample from a `Bernoulli` distribution be of type `int` instead of `float`? I needed to typecase to `int` while writing the happiness sampling method (please see: `weather.type(torch.int)`)