1
votes

I'm trying to replicate the example given in documentations for PyMC 2.3:

@pm2.stochastic(dtype=int)
    def switchpoint(value=1900, t_l=1851, t_h=1962):
    """The switchpoint for the rate of disaster occurrence."""
    if value > t_h or value < t_l:
        # Invalid values
        return -np.inf
    else:
        # Uniform log-likelihood
        return -np.log(t_h - t_l + 1)

when trying to assign this:

test = switchpoint()

I get the error message(complete error message at the end of this post):

TypeError: 'numpy.ndarray' object is not callable

I'm guessing it's a compatibility problem; I'm using Enthought Canopy Python 2.7.6 (64-bit) distribution. Numpy version 1.8.0 and Scipy 0.13.3.

Can anyone help me find what the problem is?

Note: I found a small relatively old thread on google group with apparently the same problem. However the thread has no update on the status of the problem.

Here is the complete error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/arash/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pymc/CommonDeterministics.py", line 975, in __call__
    plot=False)
  File "/Users/arash/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pymc/PyMCObjects.py", line 435, in __init__
    verbose=verbose)
  File "/Users/arash/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pymc/Node.py", line 216, in __init__
    Node.__init__(self, doc, name, parents, cache_depth, verbose=verbose)
  File "/Users/arash/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pymc/Node.py", line 127, in __init__
    self.parents = parents
  File "/Users/arash/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pymc/Node.py", line 150, in _set_parents
    self.gen_lazy_function()
  File "/Users/arash/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pymc/PyMCObjects.py", line 446, in gen_lazy_function
    self._value.force_compute()
  File "LazyFunction.pyx", line 257, in pymc.LazyFunction.LazyFunction.force_compute (pymc/LazyFunction.c:2409)
  File "/Users/arash/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pymc/CommonDeterministics.py", line 967, in eval_fun
    return self(*args, **kwargs)
TypeError: 'numpy.ndarray' object is not callable
1

1 Answers

2
votes

PyMC actually turns switchpoint into a regular variable. So you just need to do

test = switchpoint

It looks strange because you're defining it as a decorated function, but that's not actually how you're supposed to use it. It makes more sense if you look at the other ways you can define stochastic variables, which are like this:

switchpoint = DiscreteUniform('switchpoint', lower=0, upper=110, doc='Switchpoint[year]')

And this:

def switchpoint_logp(value, t_l, t_h):
    if value > t_h or value < t_l:
        return -np.inf
    else:
        return -np.log(t_h - t_l + 1)

def switchpoint_rand(t_l, t_h):
    from numpy.random import random
    return np.round( (t_l - t_h) * random() ) + t_l

switchpoint = Stochastic( logp = switchpoint_logp,
                doc = 'The switchpoint for the rate of disaster occurrence.',
                name = 'switchpoint',
                parents = {'t_l': 1851, 't_h': 1962},
                random = switchpoint_rand,
                trace = True,
                value = 1900,
                dtype=int,
                rseed = 1.,
                observed = False,
                cache_depth = 2,
                plot=True,
                verbose = 0)