I am trying to use write my own stochastic
and deterministic
variables with pymc3
, but old published recipe for pymc2.3
explained how we can parametrize our variables no longer works. For instance I tried to use this direct approach and it failed:
def x_logp(value, x_l, x_h):
if ((value>x_h) or (value<x_l)):
return -np.inf
else:
return -np.log(x_h-x_l+1)
def x_rand(x_l,x_h):
return np.round((x_h-x_l)*np.random.random_sample())+x_l
Xpos=pm.stochastic(logp=x_logp,
doc="X position of halo center ",
observed=False,
trace=True,
name='Xpos',
random=x_rand,
value=25.32,
parents={'x_l':0,'x_h'=500},
dtype=float64,
plot=True,
verbose=0)
I got the following error message:
ERROR: AttributeError: 'module' object has no attribute 'Stochastic' [unknown]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Stochastic'
I am wondering how could I define my own variables with pymc3
without using the available pymc
distributions?
Best Answer
Custom probability densities can be included using
pymc.DensityDist()
. For the gradient computation to work though, you have to supply your density as a theano function. For example, see https://github.com/pymc-devs/pymc3/blob/master/pymc3/examples/custom_dists.py:If you can't express you density as a theano compute graph, you have to create a blackbox theano expression using the new as_op decorator. For example: hhttps://github.com/pymc-devs/pymc3/blob/master/pymc3/examples/disaster_model_theano_op.py. Note that this requires Theano from current master branch:
We might make this as_op part a bit easier in the future.