I have been using PyMC for a while, and recently started using PyMC3.
I am currently using a model where I my parameter is a normal distribution whose centre is given by an array. With PyMC, I define it as :
xp=pymc.Normal('xp',mu=x,tau=0.1)
However, I get a few errors when I try doing this in PyMC3. Thus, I considered the error term to be a parameter instead :
e=pymc3.Normal('e',mu=0,tau=0.1)
and used (x+e)
in my calculations.
In my model, the likelihood function has an integral whose limit is (x+e).
In PyMC, I defined the centre (mu) of the likelihood as :
@pymc.deterministic
def mu(om=om,xp=xp):
f=lambda y:(1+om*((1+y)**float(3)-1))**float(-0.5)
numpy.vectorize(f)
def f1(xp):
return integrate.quad(f,0,xp)
f1=numpy.vectorize(f1)
f2=f1(xp)
return (f2[0]*(1+xp))
However, this didn’t really work with PyMC3. I also tried using a Custom theano Op (from https://stackoverflow.com/questions/42678490/custom-theano-op-to-do-numerical-integration/43154053). My code currently looks like this:
basic_model = pymc3.Model()
with basic_model:
om=pymc3.Uniform('omega_m',0,1)
a=pymc3.Uniform('a',0.01,0.1)
b=pymc3.Uniform('b',0.001,0.01)
e_r=pymc3.Normal("e_r",mu=0,sd=a+b*x)
t = T.dscalar('t')
z= (1+om*((1+t)**float(3)-1))**float(-0.5)
numpy.vectorize(z)
def f1(xp):
intZ = integrateOut(z,t,0,xp)()
funcIntZ = theano.function([],intZ)
return funcIntZ()
f1=numpy.vectorize(f1)
f2=f1(x+e)
mu=f2[0]*(1+x+e)
y=pymc3.Normal('y',mu=mu,sd=0.5,observed=y_obs)
This is full traceback of the error:
Traceback (most recent call last):
File "<ipython-input-9-4264bc40650f>", line 12, in <module>
z= (1+om*((1+t)**float(3)-1))**float(-0.5)
File "/anaconda3/lib/python3.6/site-packages/theano/tensor/var.py", line 227, in __radd__
return theano.tensor.basic.add(other, self)
File "/anaconda3/lib/python3.6/site-packages/theano/gof/op.py", line 639, in __call__
(i, ins, node, detailed_err_msg))
ValueError: Cannot compute test value: input 1 (t) of Op Elemwise{add,no_inplace}(TensorConstant{1}, t) missing default value.
Backtrace when that variable is created:
File "/anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell
handler(stream, idents, msg)
File "/anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "/anaconda3/lib/python3.6/site-packages/ipykernel/ipkernel.py", line 208, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/anaconda3/lib/python3.6/site-packages/ipykernel/zmqshell.py", line 537, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2728, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2850, in run_ast_nodes
if self.run_code(code, result):
File "/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2910, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-9-4264bc40650f>", line 11, in <module>
t = T.dscalar('t')
Is there any other way of achieving this? I have tried various methods, but they don’t seem to work when I include a parameter as a limit of integration.