How to generate posterior predictive samples with size different than the observed variable?

I have a simple probabilistic model with Beta prior and Bernoulli likelihood:

with pm.Model() as model:
  mu = pm.Beta('mu', alpha=2.0, beta=2.0)
  x = pm.Bernoulli('x', p=mu, observed=x_obs)

  trace = pm.sample(1000)

my observed value x_obs is of shape (8,) , so when I sample from sample_posterior_predictive() , I always get samples with the same size:

samples = pm.sample_posterior_predictive(trace, samples=10000, model=model)
samples['x'].shape
>>> (10000, 8)

How can I sample n different Bernoulli draws other than 8? For example with shape= (10000, n)?

The easiest way to generate posterior samples in a case as simple as this is probably to just pull out sampled values of mu from your trace object. Here we generate a single (new) flip for each of the samples in your trace:

flips = scipy.stats.bernoulli.rvs(trace['mu'])

If you need 8 (or 10000) flips per mu, then you can go through each sampled value of trace['mu'] and grab 8 (or 10000) flips for each:

n_new_flips = 10000
for sample in trace:
    flips = scipy.stats.bernoulli.rvs(sample['mu'], size=n_new_flips)
    # do something with newly generated flips
1 Like