Suppose that I generate some sample data using pymc3 for a gamma distribution:
import pymc3 as pm
import arviz as az
# generate fake data:
with pm.Model() as model2:
g = pm.Gamma('g', alpha=1.7, beta=0.097)
syn = g.random(size=1000)
plt.hist(syn, bins=50);
Now, I will create a model to fit a gamma distribution on that data:
model = pm.Model()
with model:
# alpha
alpha = pm.Exponential('alpha', lam=2)
# beta
beta = pm.Exponential('beta', lam=0.1)
g = pm.Gamma('g', alpha=alpha, beta=beta, observed=syn)
trace = pm.sample(2000, return_inferencedata=True)
This will correctly get the values and distribution that created the original fake data. Now, I want to plot the pdf (but I don't know how to do that!). I saw an example that did this:
with model:
post_pred = pm.sample_posterior_predictive(trace.posterior)
# add posterior predictive to the InferenceData
az.concat(trace, az.from_pymc3(posterior_predictive=post_pred), inplace=True)
which creates a matrix that contains samples from the estimated pdfs. I plot the results with:
fig, ax = plt.subplots()
az.plot_ppc(trace, ax=ax)
ax.hist(syn, bins=100, alpha=.3, density=True, label='data')
ax.legend(fontsize=10);
plt.xlim([0,60])
which gives:
which is not what I'm looking for. Instead, I'd like to sample from the posterior of alpha and beta to draw many gamma pdfs. I can do that by sampling and plotting lines, but I thought this has to be something that is already implemented with pymc3 or arviz, but I just don't know it. Thanks in advance if you could tell me how to plot what I want.