I’m trying to do weighted inference for say a hierarchical model, using weight as proposed
here.
There’s a similar question here but the implementation the guy came up with it’s wrong in my opinion (instead of multiplying weights with likelihoods he is summing them).
Using a simplified model with regular inference like:
import numpy as np
import pymc3 as pm
import theano.tensor as tt
N,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)
model = 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])
y_=pm.Poisson('Y_obs',mu=mu,observed=y)
trace=pm.sample(1000)
I’ve tried to insert weights doing:
def weighted_logp(w,RV):
def logp(x):
return w*RV.logp(x)
return logp
model = 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])
y_no_w=pm.Poisson('Y_obs',mu=mu,shape=N)
y_w=pm.DensityDist('Y_w', weighted_logp(w,y_no_w), observed=y)
trace=pm.sample(1000)
That is not working due to:
TypeError: can't turn [TensorConstant{[0. 0. 3. .. 2. 0. 3.]}] and {} into a dict. cannot convert dictionary update sequence element #0 to a sequence
With some tweaking, like:
def weighted_logp(w,RV):
def logp(x):
return w*RV.logp({RV.name:x})
return logp
Still I couldn’t make it work.
ValueError: ('setting an array element with a sequence.', 'Container name "Y_obs"')
Any help?
I’ would like to make this as general as possible (i.e. avoid rewriting poisson likelihood)
Thank you!