Yup, I think that’s the right and easiest approach as junpenglao suggested.
On the other hand using DensityDist may allow you to then sample from the posterior distribution, unfortunately if you create wrapped functions and use multicores then some pickle issues may araise, like:
import numpy as np
import pymc3 as pm
import theano.tensor as tt
from functools import partialN,p=5000,2
X=np.random.normal(0,1,(N,p))
b=np.random.uniform(0,0.1,p)
y=np.random.poisson(np.exp(X.dot(b)),N)w=np.random.uniform(0,1,N)
def wlogp(x,w,distributuion,**kwargs):
return w*distributuion.dist(**kwargs).logp(x)
def weighted_distribution(w,distributuion,**kwargs):
return partial(wlogp,w=w,distributuion=distributuion,**kwargs),distributuion.dist(**kwargs).randommodel = pm.Model()
with model:b0=pm.Normal('b0',mu=0,sd=.1) b1=pm.Normal('b1',mu=0,sd=.1) mu=pm.math.exp(b0*X[:,0]+b1*X[:,1]) wlogp,wrandom=weighted_distribution(w,pm.Poisson,mu=mu) y_w=pm.DensityDist('Y_w',wlogp, observed=y,random=wrandom) trace=pm.sample(1000)
yields:
Auto-assigning NUTS sampler…
Initializing NUTS using jitter+adapt_diag…
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [b1, b0]
Could not pickle model, sampling singlethreaded.
But if I save the two helper functions in a separate package then multicore works. I wonder if makes sense to open an issue for that…