I am training a multilogit model where the beta ends up being structured as a bunch of stacked square matrices, where I have a prior for the diagonals and a separate prior for the off terms.
e.g. my prior on each element of beta would be
(0, uniform(-\infty, infty), uniform(-\infty, infty),
0, N(c_mu, c_sigma), N(0, 5),
0, N(0, 5), N(c_mu, c_sigma),
0, N(o_mu, o_sigma), N(0,4),
0, N(0, 4), N(o_mu, o_sigma)).
Let’s say these squares were K x K instead of 2x2 as they are here. Is there a better way to declare my priors (in terms of helping stan compute the gradient faster) in the model than to loop through beta and use if statements to set the appropriate prior?
data {
int N;
int K; //k categories
int D; //D predictors
int y[N];
matrix[N, D] x;
}
transformed data {
vector[D] zeros = rep_vector(0, D);
}
parameters {
real c_mu;
real<lower=0> c_sigma;
real o_mu;
real<lower=0> o_sigma;
matrix[D, K-1] beta_raw;
}
transformed parameters {
matrix[D, K] beta;
beta = append_col(zeros, beta_raw);
}
model {
matrix[N, K] x_beta = x * beta;
//for clarity i wrote out every element, but my current way is to loop through each one and write branching logic to assign the prior.
beta[2,2] ~ normal(c_mu, c_sigma);
beta[2,3] ~ normal(0, 5);
beta[3,2] ~ normal(0, 5);
beta[3,3] ~ normal(c_mu, c_sigma);
beta[4,2] ~ normal(o_mu, o_sigma);
beta[4,3] ~ normal(0, 4);
beta[5,2] ~ normal(0, 4);
beta[5,3] ~ normal(o_mu, o_sigma);
for (n in 1:N)
y[n] ~ categorical_logit(x_beta[n]’);
}
EDIT:
Relatedly, if I have a square block in my matrix with the same prior for each element, e.g.
(N(0,5),N(0,5),N(0,5),
N(0,5),N(0,5),N(0,5)
N(0,5),N(0,5),N(0,5))
is there a way to vectorize that declaration? Something like beta[10:13][1:3] ~ N(0,5)