Search code examples
pythonnumpypymc3

Stochastic Indexing in Pymc3


I'm fairly new to pymc3, and I'm trying to understand how to work random variables into models in different ways. I would like to fit the following (contrived) model, but I cannot find any support for it in the documention.

equation

I tried the following, but numpy does not allow such indexing:

seq = numpy.arange(10,y_train.size)
basic_model = pymc3.Model()
with basic_model:
  alpha = pymc3.Normal('alpha',mu=0,sd=1)
  beta = pymc3.Normal('beta',mu=0,sd=1)
  gamma = pymc3.DiscreteUniform('gamma',lower=1,upper=10)
  mu = pymc3.Deterministic('mu',alpha+beta*y_train[seq-gamma])
  y = pymc3.Normal('y',mu=mu,sd=sigma,observed=y_train[11:])
  map_estimate = pymc3.find_MAP(model=basic_model)
  step = pymc3.Metropolis()
  trace = pymc3.sample(10000,step,start=map_estimate,progressbar=True)

Solution

  • You need to convert the numpy array to a theano const first:

    tt.as_tensor_variable(y_train)[seq-gamma]