In [1]:
from collections import OrderedDict
import numpy as np
import pymc3 as pm
import seaborn as sns
sns.set(font_scale=1.5)
import scipy.stats
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
data = scipy.stats.nbinom.rvs(50, 0.04, size=20)
In [3]:
with pm.Model() as modelp:
# fit a poisson and negbin
p_mean = pm.Uniform('p_mean', 1, 10000)
p = pm.Poisson('p', mu=p_mean, observed=data)
tracep = pm.sample(10000, tune=4000)
with pm.Model() as modelnb:
# fit a poisson and negbin
nb_mean = pm.Uniform('nb_mean', 1, 10000)
nb_alpha = pm.Uniform('nb_alpha', 1, 1000)
nb = pm.NegativeBinomial('nb', mu=nb_mean, alpha=nb_alpha, observed=data)
tracenb = pm.sample(10000, tune=4000)
In [4]:
pm.traceplot(tracep, combined=True)
Out[4]:
In [5]:
pm.traceplot(tracenb, combined=True)
Out[5]:
In [6]:
ppcp = pm.sample_ppc(tracep, samples=1000, model=modelp)
ppcnb = pm.sample_ppc(tracenb, samples=1000, model=modelnb)
In [7]:
sns.distplot(ppcp['p'], label='p')
sns.distplot(ppcnb['nb'], label='nb')
sns.distplot(data, label='data')
plt.legend()
Out[7]:
In [8]:
pm.stats.waic(tracep, model=modelp, progressbar=True)
Out[8]:
In [9]:
pm.stats.waic(tracenb, model=modelnb, progressbar=True)
Out[9]:
In [10]:
pm.stats.loo(tracep, model=modelp, progressbar=True)
Out[10]:
In [11]:
pm.stats.loo(tracenb, model=modelnb, progressbar=True)
Out[11]:
In [12]:
modelp.name = 'Poisson'
modelnb.name = 'NegBin'
mods = OrderedDict()
mods[modelp]= tracep
mods[modelnb]= tracenb
comp = pm.stats.compare(mods)
comp
Out[12]:
In [13]:
pm.plots.compareplot(comp)
Out[13]:
In [14]:
data = scipy.stats.poisson.rvs(50, size=20)
In [15]:
with pm.Model() as modelp:
# fit a poisson and negbin
p_mean = pm.Uniform('p_mean', 1, 10000)
p = pm.Poisson('p', mu=p_mean, observed=data)
tracep = pm.sample(10000, tune=4000)
with pm.Model() as modelnb:
# fit a poisson and negbin
nb_mean = pm.Uniform('nb_mean', 1, 10000)
nb_alpha = pm.Uniform('nb_alpha', 1, 1000)
nb = pm.NegativeBinomial('nb', mu=nb_mean, alpha=nb_alpha, observed=data)
tracenb = pm.sample(10000, tune=4000)
In [16]:
ppcp = pm.sample_ppc(tracep, samples=1000, model=modelp)
ppcnb = pm.sample_ppc(tracenb, samples=1000, model=modelnb)
sns.distplot(ppcp['p'], label='p')
sns.distplot(ppcnb['nb'], label='nb')
sns.distplot(data, label='data')
plt.legend()
Out[16]:
In [17]:
modelp.name = 'Poisson'
modelnb.name = 'NegBin'
mods = OrderedDict()
mods[modelp]= tracep
mods[modelnb]= tracenb
comp = pm.stats.compare(mods)
comp
Out[17]:
In [18]:
pm.plots.compareplot(comp)
Out[18]:
In [ ]: