2

I have the following random intercept pymc3 (3.11.12) model, where X is (NxTxK) and y is (TxN). N is the number of groups, T the number of observations within each group and K is the number of predictors.

with pm.Model() as model:
    gamma= pm.Normal("gamma", mu=0, sigma=100, shape=())
    beta= pm.Normal("beta", mu=0, sigma=100, shape=(X.shape[2], 1))
    u= pm.Normal("u", mu=0, sigma=1, shape=(1, X.shape[0]))
    r = pm.Gamma("r", alpha=9, beta=4, shape=())

    y_hat = gamma + np.einsum("jik,kl->ij", X, beta) + u

    y_like = pm.Normal("y_like", mu=y_hat, sigma=r, observed=y)

However, when I run the model, I get the following error

ValueError: einstein sum subscripts string contains too many subscripts for operand 1

If I do the calculation np.einsum("jik,kl->ij", X, beta) outside the pm.Model() environment, it works as expected so it seems that np.einsum does not work in the wrapper function.

Michael
  • 357
  • 1
  • 13
  • Beneath it all, PyMC3 uses Theano's compute graph, so use [Theano's tensor maths](https://theano-pymc.readthedocs.io/en/latest/library/tensor/basic.html#linear-algebra). There are other Q's that cover how to translate np.einsum to Theano. – merv Nov 11 '21 at 00:41

0 Answers0