Hello,
I’m not sure why sampling from the posterior after running MCMC, seems to be doing nothing. A toy example:
key = random.PRNGKey(42)
my_data = dist.Normal(loc=1.3, scale=0.7).sample(key, sample_shape=(100,))
def model(data):
mu = numpyro.sample('mu', dist.Uniform(low=-2, high=2))
std = numpyro.sample('std', dist.Uniform(low=0, high=1.5))
with numpyro.plate("data", data.shape[0]):
numpyro.sample("obs", dist.Normal(loc=mu, scale=std), obs=data)
# Inference
kernel = NUTS(model)
key, subkey = random.split(key)
posterior = MCMC(kernel, num_samples=1100, num_warmup=100)
posterior.run(subkey, my_data)
# Prior Sampling
key, subkey = random.split(key)
prior_samples = Predictive(model, {}, num_samples=10)(subkey, my_data)
x = jnp.linspace(-3, 3, 201)
for mu, s in zip(prior_samples["mu"], prior_samples["std"]):
y = dist.Normal(loc=mu, scale=s).log_prob(x)
plt.plot(x, jnp.exp(y), c="k", alpha=0.4)
Sampling from the prior works as expected, but sampling from the posterior seems to be doing nothing
# Posterior Sampling
key, subkey = random.split(key)
posterior_pred = Predictive(model, posterior.get_samples(), return_sites=['mu', 'std'])
post = posterior_pred(subkey, jnp.array([1]))
posterior.get_samples()['mu'] == post['mu']
the above code returns all Trues. On the other hand, the following code
key, subkey = random.split(key)
posterior_pred = Predictive(model, posterior.get_samples())
posterior_pred(subkey, jnp.array([1]))
Returns all ones.
Any guidance on how to properly use Preictive, will be highly appreciated
Thanks,
Fouad