I am trying to fit a Negative binomial mixture with PyMC. It seems I do something wrong, because the predictive doesn't look at all similar to the input data. The problem is probably in the prior of the Negative binomial parameters. Any suggestions?
from sklearn.cluster import KMeans
import pymc as mc
n = 3 #Number of components of the mixture
ndata = len(data)
dd = mc.Dirichlet('dd', theta=(1,)*n)
category = mc.Categorical('category', p=dd, size=ndata)
kme = KMeans(n) # This is not needed but it is to help convergence
kme.fit(data[:,newaxis])
alphas = mc.TruncatedNormal('alphas', kme.cluster_centers_[:,0], 0.1, a=0. ,b=100000 ,size=n)
means = mc.TruncatedNormal('means', kme.cluster_centers_[:,0],0.1,a=0.0 ,b=100000, size=n)
@mc.deterministic
def mean(category=category, means=means):
return means[category]
@mc.deterministic
def alpha(category=category, alphas=alphas):
return alphas[category]
obs = mc.NegativeBinomial('obs', mean, alpha, value=data, observed = True)
predictive = mc.NegativeBinomial('predictive', mean, alpha)
model = mc.Model({'dd': dd,
'category': category,
'alphas': alphas,
'means': means,
'predictive':predictive,
'obs': obs})
mcmc = mc.MCMC( model )
mcmc.sample( iter=n_samples, burn=int(n_samples*0.7))