pymc-devs / pymc2

THIS IS THE **OLD** PYMC PROJECT (VERSION 2). PLEASE USE PYMC INSTEAD:
http://pymc-devs.github.com/pymc/
Other
879 stars 229 forks source link

TypeError: 'numpy.ndarray' object is not callable #118

Closed rogermosher closed 8 years ago

rogermosher commented 8 years ago

Here is the code:

def bayesExtremeParams(data,oparams): """ bayesExtremeParams: use Bayesian techniques to estimate genExtremeParams from data :param data: an ndarray used to find the parameter estimates :param oparams: initial params :return: shape. location, and scale parameters and 95% quantiles """

# priors
shape = pm.Normal("shape", oparams[0], 0.1)  #shape
loc   = pm.Normal("loc", oparams[1], 0.1)
scale = pm.Uniform("scale", 0,1.0)

# create stochastic variable
@pm.stochastic(dtype=float)
def gev(value=oparams[1], shape=shape, loc=loc, scale=scale):
    return ss.genextreme.logpdf(value, shape, loc=loc, scale=scale)

# observations
obs = gev("obs", c=shape, loc=loc, scale=scale, value=data, observed=True)

# create model
mcmc = pm.MCMC([shape,loc,scale,obs])
mcmc.sample(20000,10000,5)

# generate estimates
shTrace = mcmc.trace("shape")[:,None]
shQ = ssm.mquantiles(shTrace, [0.025, 0.975])

locTrace = mcmc.trace("loc")[:,None]
locQ = ssm.mquantiles(locTrace, [0.025, 0.975])

sclTrace = mcmc.trace("scale")[:,None]
sclQ = ssm.mquantiles(sclTrace, [0.025, 0.975])

# return quantiles (shape, loc, scale)
return [shTrace.mean(),locTrace.mean(),sclTrace.mean()], [shQ[1],locQ[1],sclQ[1]], [shQ[2],locQ[2],sclQ[2]]

Here is the relevant part of the stack dump:

File "C:\Users\roger\AnacondaExamples\OWLReader\Stochastic.py", line 99, in bayesExtremeParams obs = gev("obs", c=shape, loc=loc, scale=scale, value=data, observed=True) File "C:\Anaconda3\lib\site-packages\pymc\CommonDeterministics.py", line 991, in call plot=False) File "C:\Anaconda3\lib\site-packages\pymc\PyMCObjects.py", line 443, in init verbose=verbose) File "C:\Anaconda3\lib\site-packages\pymc\Node.py", line 214, in init Node.init(self, doc, name, parents, cache_depth, verbose=verbose) File "C:\Anaconda3\lib\site-packages\pymc\Node.py", line 129, in init self.parents = parents File "C:\Anaconda3\lib\site-packages\pymc\Node.py", line 147, in _set_parents self.gen_lazy_function() File "C:\Anaconda3\lib\site-packages\pymc\PyMCObjects.py", line 454, in gen_lazy_function self._value.force_compute() File "pymc\LazyFunction.pyx", line 257, in pymc.LazyFunction.LazyFunction.force_compute (pymc\LazyFunction.c:2642) File "C:\Anaconda3\lib\site-packages\pymc\CommonDeterministics.py", line 983, in eval_fun return self(_args, *_kwargs) TypeError: 'numpy.ndarray' object is not callable

I assume it is trying to call the data array (value=data). Not sure what is going on.

fonnesbeck commented 8 years ago

You've implemented your likelihood incorrectly. There is no need to call the likelihood object. You should also use the @observed decorator not the @stochastic.

rogermosher commented 8 years ago

Thanks for your response Chris. I did not understand your answer, so I went back to an earlier attempt which wrapped ss.genextreme in a pymc stochastic class. I found an error in scipy that was preventing that approach from working and inserted a line of code. Once I got the priors right, it worked.

fonnesbeck commented 8 years ago

Sorry for the confusing answer. Glad you got it figured out.