import pymc3 as pm
import numpy as np
import theano.tensor as tt
from theano import shared
np.random.seed(42)
y = shared(np.concatenate([np.random.poisson(5, size=10), np.random.poisson(9, size=10)]))
x = shared(np.random.randn(20))
with pm.Model() as model:
μ = pm.Normal('μ', 0, 1, shape=2)
β = pm.Normal('β', 0, 1)
lam = pm.Deterministic('lam', tt.exp(tt.outer(μ, β*x).T))
components = pm.Poisson.dist(mu=lam, shape=(20, 2))
w = pm.Dirichlet('w', a=np.array([1, 1])) # two mixture component weights.
like = pm.Mixture('like', w=w, comp_dists=components, observed=y)
with model:
trace = pm.sample(1000)
Auto-assigning NUTS sampler... Initializing NUTS using jitter+adapt_diag... Multiprocess sampling (2 chains in 2 jobs) NUTS: [w, β, μ] 100%|██████████| 1500/1500 [00:07<00:00, 193.51it/s] There were 7 divergences after tuning. Increase `target_accept` or reparameterize. There were 1 divergences after tuning. Increase `target_accept` or reparameterize. The number of effective samples is smaller than 25% for some parameters.
with model:
pred = pm.sample_ppc(trace, samples=1000)
100%|██████████| 1000/1000 [00:00<00:00, 1408.03it/s]
pred['like'].shape
(1000,)