How to increase posterior sample count when fitting GP model with find_MAP()?

Hello,

I am using PYMC to build an fit some GP models to some datasets. I’ve been able to fit conditional GP models through sampling and find_MAP(); however, when drawing from the posterior, I can’t seem to control the number of posterior samples that are drawn. In the older PYMC GP tutorials a samples argument could be passed to sample_posterior_predictive() to control this value, but that has been removed in more recent versions it seems. If I use sampling to fit the covariance function parameters, the the posterior sample count is the same as the MCMC sample count. However, if I use find_MAP(), I only draw one sample. How can I directly control posterior sample counts?

I’ve included my mode construction below:

with pm.Model() as model:
    l = pm.Gamma("l", alpha=2, beta=1)
    eta = pm.HalfCauchy("eta", beta=5)

    M = pm.gp.mean.Constant(c=1)
    K = eta**2 * pm.gp.cov.Matern52(1, l)

    sigma = pm.HalfCauchy("sigma", beta=5)

    gp = pm.gp.Marginal(mean_func=M, cov_func=K)
    gp.marginal_likelihood('obs', X=X, y=y, sigma=sigma)

    # fit = pm.find_MAP()
    gp_trace = pm.sample(500, tune=500, chains=2)

    f_pred = gp.conditional("f_pred", X_pred)

    # gp_samples = pm.sample_posterior_predictive([fit], var_names=["f_pred"])
    gp_samples = pm.sample_posterior_predictive(gp_trace, var_names=["f_pred"])

Thanks in advance!

The docs need updating! I think what changed is now pm.sample_posterior_predictive will draw one sample for each element in the trace. What you can do now is repeat the result from find_MAP, and also subsample the results from NUTS.

Thanks for the clarification! I am still somewhat new to PYMC, is there a more elegant means of repeatedly calling sample_posterior_predictive() other than just wrapping it with a for loop to collect draws after using find_MAP()?

Yes, alter the InferenceData/trace/dict returned by find_MAP to make it look like it had N draws (all identical). Then call posterior predictive on that altered trace

2 Likes