I need to create a mixture model with Exponential distribution positioned at different places.
I tried several approaches: using Deterministic distribution, using DensityDist distribution.
But all they failed for some pymc3 issues. Finally I have written a custom distribution.
It does not fail with exception but does not sample.
class ShExponential(pm.Exponential):
def __init__(self, mu, lam, *args, **kwargs):
super(ShExponential, self).__init__(lam, *args, **kwargs)
self.mu = tt.as_tensor_variable(mu)
self.mean += self.mu
self.mode += self.mu
self.median += self.mu
def logp(self, value):
return super(ShExponential, self).logp(value - self.mu)
def random(self, point=None, size=None):
mu = pm.distributions.draw_values([self.mu], point=point, size=size)[0]
return super(ShExponential, self).random(point=point, size=size) + mu
def stick_breaking(beta):
portion_remaining = tt.concatenate([[1], tt.extra_ops.cumprod(1 - beta)[:-1]])
return beta * portion_remaining
with pm.Model() as model:
alpha = pm.Gamma('alpha', 1., 1.)
beta = pm.Beta('beta', 1, alpha, shape=K)
w = pm.Deterministic('w', stick_breaking(beta))
lam = pm.Uniform('lam', 1., 2., shape=K)
exp = pm.Exponential('exp', lam, shape=K)
mu = pm.Uniform('mu', 1., 100., shape=K)
obs = pm.Mixture('obs', w, ShExponential.dist(mu, lam), observed=x)
step = pm.Metropolis()
trace = pm.sample(1000, step=step, random_seed=SEED)
All samples in “trace” are identical.
What is wrong with my class?