In [1]:
import itertools
import matplotlib.pyplot as plt
import matplotlib as mpl
import pymc3 as pm
from theano import as_op
import theano.tensor as tt
import numpy as np
from scipy import stats
import tqdm
import pandas as pd
from scipy import stats
import seaborn as sns
import DeadTime
sns.set(font_scale=1.6)
%matplotlib inline
%load_ext version_information
%version_information pymc3, scipy
Out[1]:
In [2]:
DT = DeadTime.TimeStamps(size=1000000)
DT.apply_nonpar_deadtime(1e-1)
In [3]:
TS = DeadTime.TimeSeries(DT)
In [8]:
fig, ax = plt.subplots(1,1, figsize=(8,4))
sns.distplot(TS.timeseries, 15, label=('Deadtime 1e-1'));
XX = np.arange(60, 151)
plt.plot(XX, stats.poisson(100).pmf(XX), 'r', label='Poisson 100')
plt.legend()
plt.xlabel('Reported rate')
kde = stats.gaussian_kde(TS.timeseries)
# plt.plot(XX, kde.evaluate(XX), 'g')
In [18]:
def dist_from_hist(param, kde, xlim=(50,150), N=100):
width = xlim[1] - xlim[0]
x = np.linspace(xlim[0], xlim[1], N)
y = kde.evaluate(x)
# what was never sampled should have a small probability but not 0,
# so we'll extend the domain and use linear approximation of density on it
x = np.concatenate([[x[0] - 3 * width], x, [x[-1] + 3 * width]])
y = np.concatenate([[0], y, [0]])
return pm.distributions.Interpolated(param, x, y)
In [19]:
with pm.Model() as model:
# Priors are posteriors from previous iteration
dat = dist_from_hist('dat', kde)
# draw 10000 posterior samples
trace = pm.sample(10000)
In [20]:
pm.traceplot(trace)
Out[20]:
In [21]:
pm.traceplot(trace, combined=True)
Out[21]:
In [23]:
fig, ax = plt.subplots(1,1, figsize=(8,4))
sns.distplot(TS.timeseries, 15, label=('Deadtime 1e-1'));
XX = np.arange(60, 151)
plt.plot(XX, stats.poisson(100).pmf(XX), 'r', label='Poisson 100')
plt.xlabel('Reported rate')
kde = stats.gaussian_kde(TS.timeseries)
sns.distplot(trace['dat'], 15, label='mcmc')
plt.legend()
Out[23]:
In [ ]: