Can I call a pymc-function directly?

Robert rxjwg98 at gmail.com
Sun Dec 20 20:33:01 EST 2015


Hi,
I am new to pymc package. I come across the following tutorial:
The example is from: www.map.ox.ac.uk/media/PDF/Patil_et_al_2010.pdf
.........
import pymc 
import numpy as np 
n = 5*np.ones(4,dtype=int) 
x = np.array([-.86,-.3,-.05,.73]) 
alpha = pymc.Normal('alpha',mu=0,tau=.01) 
beta = pymc.Normal('beta',mu=0,tau=.01) 
@pymc.deterministic 
def theta(a=alpha, b=beta): 
 """theta = logit^{-1}(a+b)""" 
 return pymc.invlogit(a+b*x) 
d = pymc.Binomial('d', n=n, p=theta, value=np.array([0.,1.,3.,5.]),\ 
 observed=True) 
....... 
import pymc 
import pymc.Matplot 
import mymodel 
S = pymc.MCMC(mymodel, db='pickle') 
S.sample(iter=10000, burn=5000, thin=2) 
pymc.Matplot.plot(S) 
import matplotlib.pyplot as plt 
plt.show() 
///////
Through the above code runs and plots, I would like to know more detail
about function theta. I have tried the following modified code:
import pymc 
import numpy as np 
n = 5*np.ones(4,dtype=int) 
x = np.array([-.86,-.3,-.05,.73]) 
alpha = pymc.Normal('alpha',mu=0,tau=.01) 
beta = pymc.Normal('beta',mu=0,tau=.01) 
@pymc.deterministic 
def theta(a=alpha, b=beta): 
 """theta = logit^{-1}(a+b)""" 
 return pymc.invlogit(a+b*x)
 
p=theta(alpha, beta)
print 'p=', p
//////////////
It has this error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
C:\Users\rj\pyprj\pymc_mymodel0.py in <module>()
 13 return pymc.invlogit(a+b*x)
 14 
---> 15 p=theta(alpha, beta)
 16 print 'p=', p
C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\pymc\CommonDeterministics.pyc in __call__(self, *args, **kwargs)
 989 {'self': self, 'args': args, 'kwargs': kwargs},
 990 trace=False,
--> 991 plot=False)
 992 Variable.__call__ = UnboundMethodType(__call__, None, Variable)
 993 
C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\pymc\PyMCObjects.pyc in __init__(self, eval, doc, name, parents, dtype, trace, cache_depth, plot, verbose, jacobians, jacobian_formats)
 441 trace=trace,
 442 plot=plot,
--> 443 verbose=verbose)
 444 
 445 # self._value.force_compute()
C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\pymc\Node.pyc in __init__(self, doc, name, parents, cache_depth, trace, dtype, plot, verbose)
 212 self.extended_children = set()
 213 
--> 214 Node.__init__(self, doc, name, parents, cache_depth, verbose=verbose)
 215 
 216 if self.dtype is None:
C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\pymc\Node.pyc in __init__(self, doc, name, parents, cache_depth, verbose)
 127 
 128 # Initialize
--> 129 self.parents = parents
 130 
 131 def _get_parents(self):
C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\pymc\Node.pyc in _set_parents(self, new_parents)
 145 
 146 # Get new lazy function
--> 147 self.gen_lazy_function()
 148 
 149 parents = property(
C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\pymc\PyMCObjects.pyc in gen_lazy_function(self)
 452 cache_depth=self._cache_depth)
 453 
--> 454 self._value.force_compute()
 455 
 456 self._jacobians = {}
C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\pymc\LazyFunction.pyd in pymc.LazyFunction.LazyFunction.force_compute (pymc\LazyFunction.c:2409)()
C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\pymc\CommonDeterministics.pyc in eval_fun(self, args, kwargs)
 981 
 982 def eval_fun(self, args=args, kwargs=kwargs):
--> 983 return self(*args, **kwargs)
 984 return pm.Deterministic(eval_fun,
 985 'A Deterministic returning the value of %s(*%s, **%s)' % (
TypeError: 'numpy.ndarray' object is not callable 
///////////////
>From function syntactically, it seems OK for this function calling(random
variables alpha and beta as parameters of theta. 
p=theta(alpha, beta)
Do you know what is wrong 
with my code and idea (call theta by my code, not a random model like:
d = pymc.Binomial('d', n=n, p=theta, value=np.array([0.,1.,3.,5.]),\
 observed=True)
Thanks,


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /