Search code examples
pythonbayesianpymc3mcmc

Pymc3 Conditional Posterior Sampling


I want to sample from the posterior distributions in pymc3 but conditional on specific values of certain variables. I see how to sample the posteriors with sample_ppc, but it is unclear how to do this conditionally. Is there a simple way?

For example, let's say I have this model:

with pymc3.Model() as model:
    mu = pymc3.Uniform('mu', -3., 3.)
    std = pymc3.Uniform('std', 0., 2.)
    N = pymc3.Normal('N', mu=mu, sd=std, observed=obs)
    start = pymc3.find_MAP() 
    step = pymc3.NUTS(scaling=start)
    trace = pymc3.sample(2000, step, start=start)

How can I sample from the posterior distribution of N conditional on mu having the value of 1.5?


Solution

  • AFAIK, you can only use sample_ppc to get posterior predictive values that depend on the input values. So if you need to samples at different points or fix the value of a parameter you have to rewrite the model and take from the trace the values for the parameters. Following your example, you need to do something like:

    ppd = np.random.normal(1.5, trace['std'])