Pergunta

I'm trying to estimate the rate of a Poisson process where the rate varies over time using the maximum a posteriori estimate. Here's a simplified example with a rate varying linearly (λ = ax+b) :

import numpy as np
import pymc

# Observation
a_actual = 1.3
b_actual = 2.0
t = np.arange(10)
obs = np.random.poisson(a_actual * t + b_actual)

# Model
a = pymc.Uniform(name='a', value=1., lower=0, upper=10)
b = pymc.Uniform(name='b', value=1., lower=0, upper=10)


@pymc.deterministic
def linear(a=a, b=b):
    return a * t + b

r = pymc.Poisson(mu=linear, name='r', value=obs, observed=True)

model = pymc.Model([a, b, r])
map = pymc.MAP(model)
map.fit()
map.revert_to_max()

print "a :", a._value
print "b :", b._value

This is working fine. But my actual Poisson process is capped by a deterministic value. As I can't associate my observed values to a Deterministic function, I'm adding a Normal Stochastic function with a small variance for my observations :

import numpy as np
import pymc

# Observation
a_actual = 1.3
b_actual = 2.0
t = np.arange(10)
obs = np.random.poisson(a_actual * t + b_actual).clip(0, 10)

# Model
a = pymc.Uniform(name='a', value=1., lower=0, upper=10)
b = pymc.Uniform(name='b', value=1., lower=0, upper=10)


@pymc.deterministic
def linear(a=a, b=b):
    return a * t + b

r = pymc.Poisson(mu=linear, name='r')


@pymc.deterministic
def clip(r=r):
    return r.clip(0, 10)

rc = pymc.Normal(mu=r, tau=0.001, name='rc', value=obs, observed=True)

model = pymc.Model([a, b, r, rc])
map = pymc.MAP(model)
map.fit()
map.revert_to_max()

print "a :", a._value
print "b :", b._value

This code is producing the following error :

Traceback (most recent call last):
  File "pymc-bug-2.py", line 59, in <module>
    map.revert_to_max()
  File "pymc/NormalApproximation.py", line 486, in revert_to_max
    self._set_stochastics([self.mu[s] for s in self.stochastics])
  File "pymc/NormalApproximation.py", line 58, in __getitem__
    tot_len += self.owner.stochastic_len[p]
KeyError: 0

Any idea on what am I doing wrong?

Foi útil?

Solução

By "Capped" do you mean that it is a truncated Poisson? It appears thats what you are saying. If it were a left truncation (which is more common), you could use the TruncatedPoisson distribution, but since you are doing a right truncation, you cannot (we should have made this more general!). What you are trying will not work -- the Poisson object has no clip() method. What you can do is use a factor potential. It would look like this:

@pymc.potential
def clip(r=r):
    if np.any(r>10):
        return -np.inf
    return 0

This will constrain the values of r to be less than 10. Refer to the pymc docs for information on the Potential class.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top