I have been working on PyMc3 to build a probabilistic model that fits my data. As a part of it I’ve tried implementing updating priors on my data in which posteriors are being used as priors and will run for multiple times
observed_cell_delay = list_cell_delay[:5000]
def stick_breaking(beta):
portion_remaining = tt.concatenate([[1], tt.extra_ops.cumprod(1 - beta)[:-1]])
return beta * portion_remaining
K=30
with pm.Model() as cell_del_model:
alpha = pm.Gamma('alpha', 1., 1.)
beta = pm.Beta('beta', 1., alpha, shape=K)
w = pm.Deterministic('w', stick_breaking(beta))
tau = pm.Gamma('tau', 1., 1., shape=K)
mu = pm.Normal('mu',0.,10.,shape=K)
est_cell_del = pm.NormalMixture('est_cell_del', w, mu, tau=tau, observed=observed_cell_delay)
with cell_del_model:
step = pm.Metropolis()
#db = pm.backends.Text('samples')
trace = pm.sample(2000,step=step,tune=1000,cores=8)#,discard_tuned_samples=True,n_init=1000,trace=db)
pm.traceplot(trace)
def from_posterior(param, samples):
smin, smax = np.min(samples), np.max(samples)
width = smax - smin
x = np.linspace(smin, smax, 100)
samples = samples[~np.isnan(samples)]
y = stats.gaussian_kde(samples)(x)
x = np.concatenate([[x[0] - 3 * width], x, [x[-1] + 3 * width]])
y = np.concatenate([[0], y, [0]])
return Interpolated(param, x, y)
new_cell_delay = list_cell_delay[10000:]
with pm.Model() as cell_delay_model:
alpha = from_posterior('alpha',trace['alpha'])
beta = from_posterior('beta',trace['beta'])
w = from_posterior('w',trace['w'])
tau = from_posterior('tau',trace['tau'])
mu = from_posterior('mu',trace['mu'])
est_cell_del = pm.NormalMixture('est_cell_del', w, mu, tau=tau, observed=new_cell_delay)
step = pm.Metropolis()
trace1 = pm.sample(1000,step=step,tune=1000,cores=8)
I am getting the value error "ValueError: Not enough dimensions on Elemwise{mul,no_inplace}.0 to reduce on axis -1"
at pm.NormalMixture while updating priors. Thanks in advance.