'Deterministic variable in posterior predictive samples
When generating posterior predictive samples using pm.sample_posterior_predictive
the result only shows the observed variable. How can I access deterministic variables after sampling?
Here is an example. After using pm.sample_posterior_predictive
I would like to access mu
, which is a pm.Deterministic
variable but the result only includes y
.
import pymc3 as pm
from numpy.random import default_rng
rng = default_rng(seed=0)
x1 = rng.standard_normal((1000, 1)) + 3
y = 10 + x1 * 2
with pm.Model() as model:
# Define priors
sigma = pm.HalfCauchy("sigma", beta=10, testval=1.0)
intercept = pm.Normal("Intercept", 0, sigma=20)
x_coeff = pm.Normal("x", 0, sigma=20)
# I would like this variable in the posterior predictive samples
mu = pm.Deterministic("mu", intercept + x_coeff * x1)
# Define likelihood
likelihood = pm.Normal("y", mu=mu, sigma=sigma, observed=y)
# Sample
trace = pm.sample(1000, return_inferencedata=True, cores=1)
ppc = pm.sample_posterior_predictive(trace, model=model)
print(ppc.keys()) # Only shows y
Solution 1:[1]
The deterministic variable can be accessed in the posterior predictive after explicitly naming it in var_names
.
ppc = pm.sample_posterior_predictive(trace, model=model, var_names=['y', 'mu'])
This shows both y
and mu
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ipa |