Hi all,
I’m a new Pyro user and I’m trying to handle a simple (in theory) problem:
I have a tensor 500x300 where each row is an independent sample from a Poisson distribution where the lambda is generated by a gamma distribution (Gamma-Poisson model).
import torch
import pyro
import math
import matplotlib.pyplot as plt
import numpy as np
import pyro.optim
from pyro.infer import SVI, Trace_ELBO, MCMC, NUTS
import pyro.distributions as dist
from torch.distributions import constraints
np.random.seed(90)
back_dist = np.random.gamma(5,1.2,500)
dt = np.zeros((500,300))
for i in range(500):
dt[i] = np.random.poisson(back_dist[i], 300)
X = torch.tensor(dt, dtype=torch.float32)
I would estimate the parameters of the gamma distribution (“latent rate”, common for each row) and the lambda specific for each row. Here what I’m trying to do, I’m sure that I’m making mistakes with the plates
def gamma_poisson_model(data):
#N = 250
N, D = data.shape
# define the hyperparameters that control the Gamma prior
alpha0 = torch.tensor(2.0)
beta0 = torch.tensor(2.0)
# sample f from the Beta prior
f = pyro.sample("latent_rate", dist.Gamma(alpha0,beta0))
# loop over the observed data
with pyro.plate('x_data', N):
with pyro.plate('y_data', D):
pyro.sample('obs', dist.Poisson(f), obs=data)
def guide(data):
# register the two variational parameters with Pyro.
alpha_q = pyro.param("alpha_q", torch.tensor(2.0),
constraint=constraints.positive)
beta_q = pyro.param("beta_q", torch.tensor(2.0),
constraint=constraints.positive)
# sample latent rate from the distribution Gamma(alpha_q, beta_q)
pyro.sample("latent_rate", dist.Gamma(alpha_q, beta_q))
Do you have any advice to solve the problem? I read that I should use “to_event” to declare each independent row, but it’s not clear how to use it.
Thank you for your help
Nicolo’