Question

I'm having an issue running inference on a model in PyMC. I'm trying to run MCMC over a fairly complicated model, and I'm getting an error about

hasattr(): attribute name must be string

I get this on the final line of this block of code ( apologies, it's complicated, but I'm really not sure where the issue might be ).

import pymc
from matplotlib import pyplot as plt
import numpy as np

# a is a temp variable
# A is the data : a (2, 779)-shaped array of 0 and 1 only
a = np.loadtxt("PLOM3/data/stoch.csv")
A = np.zeros((2, len(a)-1))
A[0, :] = a[:-1]
A[1, :] = a[1:]

num_cities = 2



# Time
t = range(len(A) - 1)

# Noise term on p
epsilon = pymc.Uniform("epsilon", 0, 1)

# Exponential parameter
gamma = 1. / A.mean(axis = 1)

# Rate of imports
lambda_ = pymc.Exponential("lambda_", gamma, size=num_cities)

# Importations
Y = [pymc.Poisson("Y", lambda_[i], size = A.shape[1]) for i in range(num_cities)]

# Coefficients
alpha = [pymc.Uniform("alpha", 0, 1) for i in range(num_cities)]
beta = [pymc.Uniform("beta", 0, 1) for i in range(num_cities)]

# Refactory Period
delta = pymc.Exponential("delta", 0.2)

# Delay
d = pymc.Uniform("d", 0, 12, size = num_cities)

# Time since last epidemic
tau = np.zeros_like(A)
tmp = np.where(A[i, :] == 1)[0]
for i in range(2) :
    for j in range(len(tmp)-1) :
        tau[i, tmp[j]:tmp[j+1]] = tmp[j]
            tau[i, tmp[-1]:] = tmp[-1]

# Bernoulli probabilities
@pymc.deterministic
def p(delta = delta, tau = tau, alpha = alpha, Y = Y, beta = beta, epsilon = epsilon, t = t) :
    out = np.zeros((2, 1))
    for i in range(2) :
        if t > (tau[i, t] + delta) :
            out[i] = alpha[i] * Y[i] + beta[~1] * A[~1, t - d[i]] + epsilon

# Time Series
X = [pymc.Bernoulli("X", p, size = A.shape[1], value = A[i, :], observed = True) for i in range(num_cities)]



model = pymc.Model([X, p, delta, alpha, beta, Y, lambda_, gamma, epsilon])
mcmc = pymc.MCMC(model)

Any help would be really appreciated. There are few good tutorials out there on PyMC. I'm following Cameron Davidson-Pilon's excellent book, but so far, I can't find anything in there as to why this might give me this error.

Thanks again.


In response to Cam.Davidson.Pilon :

The model describes measles in small populations. The important factor is that the size of the populations are smaller than the carrying capacity for measles, and so, instead of being endemic at all times, measles goes extinct after every epidemic. It is then reintroduced through immigration, and through country-level migration between cities in the country when an epidemic occurs there.

The context is Iceland, 1900 - 1964 ( prevaccination ). We assume that measles can be imported through a Poisson process from overseas ( the Y variable ) with rates lambda, or from other cities in Iceland ( X ) with some delay ( d ). X is observed, sampled monthly, with a 1 if there was an epidemic that month and a 0 otherwise.

We have X_i and Y_i for every city i. We assume that the values of X_i are Bernoulli-distributed with a probability of an epidemic p_i that is a function of the immigration into that particular city, with an importance alpha_i, and also a function of migration from other cities, should they have epidemics ( X_j, j =/= i ), with coefficient beta_ij, as well a background level of noise epsilon which represents uncertainty from where a case of measles may have come from. We also impose a refractory period delta, to allow susceptible individuals in a city to be born again.

Things I'm not certain about :

  • epsilon may not be necessary and / or a good thing
  • I'd like to impose the constraint that alpha_i + sum_j beta_ij, i =/= j, be less than 1
  • I'd like to have delta distributed more in an exponential form, but obviously discrete, as we only have monthly data
  • Just spotted that d needs to be DiscreteUniform
  • When I pass X_j into determining p_i, I may smooth it to allow for temporal jitter, probably just using a convolution with a Hanning window

I think that about covers what's going on. Please let me know if anything is unclear ! Again, any input most welcome.

Thanks again,

Quentin

Was it helpful?

Solution

I can offer a few suggestions, and a fix to your problem.

Lines that contain an inline for loop should we wrapped in a pymc.Container class, which makes them pymc friendly.

Similarly, you should differentiate the names of each variable in those lists. For example:

 Y =  pymc.Container([pymc.Poisson("Y_%d"%i, lambda_[i], size = A.shape[1]) for i in range(num_cities)])

These fixes should work. Thanks for reading the text! This example looks nice and interesting, I would be interested in hearing the context of the example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top