Dear community,
I’m trying to implement a model described in this paper http://onlinelibrary.wiley.com/doi/10.1002/1097-0258(20000915/30)19:17/18<2421::AID-SIM579>3.0.CO;2-C/full#references.
I’m using this model to fit 26 discrete variables (y), with an offset, and a spatial effect component. Refer to equation (1) of the paper and equation (7) and (8). The relative code is:
class CAR3(distribution.Continuous):
"""
Conditional Autoregressive (CAR) with 2nd order neighbors
"""
def __init__(self, adjacency, tau, rho, adjacency2, rho2, *args, **kwargs):
super(CAR3, self).__init__(*args, **kwargs)
n, m = adjacency.shape
self.n = n
adjacency_sparse = scipy.sparse.csr_matrix(adjacency)
self.adjacency = theano.sparse.as_sparse_variable(adjacency_sparse)
adjacency_sparse2 = scipy.sparse.csr_matrix(adjacency2)
self.adjacency2 = theano.sparse.as_sparse_variable(adjacency_sparse2)
self.n_neighbors = tt.as_tensor_variable(adjacency.sum(1))
self.n_neighbors2 = tt.as_tensor_variable(adjacency2.sum(1))
self.mean = tt.zeros(n)
self.median = self.mean
self.tau = tt.as_tensor_variable(tau)
self.rho = tt.as_tensor_variable(rho)
self.rho2 = tt.as_tensor_variable(rho2)
with open('dataset.pkl', "rb") as input_file:
y = pickle.load(input_file)
offset = pickle.load(input_file)
adjacency = pickle.load(input_file)
adjacency2 = pickle.load(input_file)
with pm.Model() as pooled_model:
b0 = pm.Normal('intercept', mu=0, tau=0.001)
rho = pm.Uniform('rho', 0, 1)
rho2 = pm.Uniform('rho2', 0, 1)
tau = pm.Gamma('tau', alpha=0.001, beta=0.001)
mu_phi = CAR3('mu_phi', adjacency=adjacency, rho=rho, adjacency2=adjacency2, rho2=rho2, tau=tau, shape=len(y))
phi2 = pm.Deterministic('phi', mu_phi - tt.mean(mu_phi))
mu = tt.exp(b0 + phi2 + tt.log(offset))
ncrimes = pm.Poisson('ncrimes', mu=mu, observed=y)
pooled_trace = pm.sample(3000, njobs=3, tune=5000) # , , tune=1000
plot_traces(pooled_trace)
My traces are very weird and I have these warnings
UserWarning: The acceptance probability in chain 1 does not match the target. It is 0.0537608781563, but should be close to 0.8. Try to increase the number of tuning steps.
UserWarning: Chain 0 reached the maximum tree depth. Increase max_treedepth, increase target_accept or reparameterize.
Dataset: https://www.dropbox.com/s/a3h1iz02ehqbze0/dataset.pkl?dl=0
Am I doing something very wrong? I am not an expert of reparametrization: is there a way to do it? Do you have any suggestions?
I’m using advi+adapt_diag and the latest version of pymc3