Problem Concatenating two tensor generated with scan (a time series example)


I am currently attempting to model a generative process for an AR1 timeseries model but I am encountering difficulties in aligning the shapes of ar_inint and ar_innov in order to concatenate them for the model. Also, I am aware about pymc’s builtin AR class but i am explicitly writing it down for better understanding…

import pymc as pm
import pytensor as ae
import pytensor.tensor as at
from pytensor.tensor.random.utils import RandomStream

srng = RandomStream(seed=234)

# Program
lags = 1

with pm.Model() as model_ar:
  ar_init = pm.Normal("ar_init", shape=(lags,))

  def ar_function(x_tm1, rho, sigma):
    return srng.normal(x_tm1*rho,sigma)

  ar_innov, updates = ae.scan(
      output_info = [dict(initial = ar_init, taps = [-lags] ) ],
      non_sequences=[rho, sigma],

  ar = pm.Deterministic("ar", at.concatenate([ar_init, ar_innov]))

Which is getting me the error below

TypeError: Only tensors with the same number of dimensions can be joined. Input ndims were: [1, 2]


When I hit a shape error, the first thing I do is comment out the offending line and check shapes of everything in the model using the .eval() method. In your case, I check:

>>> [100, 1]
>>> [1]

Here we see the problem – ar_init is a 1d array. According to the error, the concatenate function only wants objects of the same dimensions (1d-1d, 2d-2d, etc). Here we have 1d and 2d.

To fix it, we need to make ar_init into a 2d array. Lots of choices, but I usually go with None indexing:

    ar = pm.Deterministic("ar", at.concatenate([ar_init[None], ar_innov]))

If that’s not transparent enough for your tastes, you can go with the very clear at.atleast_2d:

    ar = pm.Deterministic("ar", at.concatenate([at.atleast_2d(ar_init), ar_innov]))

ThanK you very much!!