Trouble multiplying matrix with an array of vectors

Hello,

I am running a model of which this is the relevant part:

data{
int<lower=1> K;
int<lower=0> n;
vector[K] mu;
// ...
}

parameters{
vector[K] ThetaStar[n];
cholesky_factor_corr[K] L;
vector<lower=0>[K] sigma;
// ...
}

transformed parameters{
vector[K] Theta[n];
Theta = mu + sigma .* (L * ThetaStar);
}

model{
ThetaStar ~ std_normal;
sigma ~ cauchy(0,5);
L ~ lkj_corr_cholesky(1);
// ...
}

However, the model doesn’t work. I am getting an error in the following line: Theta = mu + sigma .* (L * ThetaStar);. The error says:

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
No matches for: 

  matrix * vector[ ]

Available argument signatures for operator*:

  real * real
  vector * real
  row_vector * real
  matrix * real
  row_vector * vector
  vector * row_vector
  matrix * vector
  row_vector * matrix
  matrix * matrix
  real * vector
  real * row_vector
  real * matrix

Expression is ill formed.

If I am interpreting this correctly, Stan has trouble multiplying a matrix by an array of vectors. What can I do to get around this issue?

Why not just represent ThetaStar and Theta as a K \times n matrix?

1 Like