简体   繁体   中英

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM