In the code below (simplification of actual model), I am attempting to select a matrix (array) from a list using indices generated by a Categorical prior distribution. I do not understand why this generates an index out of bounds error. The theano code appears to work in isolation, but not in the PyMC3 model.
from pymc3 import *
from pymc3.distributions.multivariate import Dirichlet, Multinomial
from pymc3.distributions.discrete import Categorical
import numpy as np
import warnings
import theano
import theano.tensor as tt
from theano import config, scalar
def run_MCMC3(sfs, prior, tree_matrices, tree_probabilities, draws):
config.warn.round = False
config.exception_verbosity='high'
warnings.simplefilter("ignore", FutureWarning)
tree_matrices = theano.shared(tree_matrices)
with Model() as categorical_model:
tree_index = Categorical('tree_index', tree_probabilities, testval=0) #deleted testval=0
mx1 = tree_matrices[tree_index]
probs = Dirichlet('probs', a=prior)
conditional_probs = tt.dot(mx1, probs.T)
sfs_obs = Multinomial('sfs_obs', n=np.sum(sfs), p=conditional_probs, observed=sfs)
with categorical_model:
step = Metropolis()
tune = int(draws / 5)
trace = sample(draws, tune=tune, njobs=1, step=step)
plt.show(forestplot(trace, varnames=['probs']))
traceplot(trace, varnames=['probs'])
print(summary(trace))
return trace
sfs = np.array([36, 24, 3, 9, 0])
tree_matrices = [np.array([[1., 2., 3., 4., 6.],
[0., 0., 0., 1., 0.],
[0., 0., 1., 0., 0.],
[0., 1., 0., 0., 0.],
[1., 0., 0., 0., 0.]]), np.array([[0., 2., 3., 4., 6.],
[1., 0., 0., 1., 0.],
[0., 0., 1., 0., 0.],
[1., 1., 0., 0., 0.],
[0., 0., 0., 0., 0.]]), np.array([[0., 1., 3., 4., 6.],
[1., 1., 0., 1., 0.],
[0., 1., 1., 0., 0.],
[1., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]]), np.array([[0., 1., 2., 4., 6.],
[1., 1., 2., 1., 0.],
[0., 1., 0., 0., 0.],
[1., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])]
tree_matrices = np.array(tree_matrices)
print(tree_matrices)
tree_probabilities = np.asarray([0.3, 0.2, 0.2, 0.3])
print('tree_probabilities = ', tree_probabilities)
draws = 50000
uninf_prior = np.ones(5) / 5
uninf_trace = run_MCMC3(sfs, uninf_prior, tree_matrices, tree_probabilities, draws=draws)