Hi, I have been able to correctly transfer the Pymc programme I am interested in to an equivalent Stan programme. Equivalent results are generated. I am just wondering if there are any obvious inefficiencies in the code that anyone could advise me on and make my code better.
data{
int N;
int D;
int C;
array[N] int<lower = 1, upper = D> Date;
array[N] int<lower = 1, upper = C> Country;
row_vector[D] t_sq;
vector[N] y;
int<lower= 0, upper = 1> onlyprior;
}
parameters{
vector<lower = 0>[C] sd;
real<lower = 0> sigma;
vector[C] alpha;
matrix[C,D] w;
real<lower=0> e;
cholesky_factor_corr[C] L;
}
transformed parameters{
matrix[C, D] beta;
matrix[C, D] beta_int;
beta_int = w * sigma;
for (c in 1:C){
beta[c, ] = beta_int[c, ] .* t_sq;
}
matrix[C,C] L_int = diag_pre_multiply(sd, L);
matrix[C,C] Sigma = L_int * L_int’;
matrix[C, D] B;
for (d in 1:D){
B[ ,d] = Sigma * beta[, d];
}
vector[N] mu;
for (n in 1:N){
mu[n] = alpha[Country[n]] + B[Country[n], Date[n]];
}
}
model{
// Priors
sd ~ normal(0, 1);
sigma ~ normal(0, 1);
e ~ normal(0, 1);
alpha ~ normal(0, 1);
to_vector(w) ~ normal(0, 1);
L ~ lkj_corr_cholesky(2);
if(onlyprior){
// Likelihood
y ~ normal(mu, e);
}
}
generated quantities{
array[N] real yrep;
yrep = normal_rng(mu, e);
}