0

I'm trying to sample from the posterior of this model: enter image description here

x is a 10x1 vector, mu is a 10x1 vector, sigma is a 10x10 matrix, psi_0 is a 10x10 matrix, 1 in bold is a 5x1 unity vector, and the rest are scalars. F and E are subscripts for the two group of stocks in our analysis (we're doing portfolio optimization). I try to run the following code:

stancode <- "data {
  int<lower=1> T;
  matrix[T,10] data_total; 
  real<lower=0> sigma_squared;
  real k;
  real<lower=0> r_squared;
  matrix[10,10] psi;
}

parameters {
  vector[10] mu_var;
  matrix[10,10] Sigma_var;
  real alpha_energy_var;
  real alpha_fin_var;
  real beta;
}

transformed parameters {
  vector[10] mu_mean;
  mu_mean = [alpha_fin_var,alpha_fin_var,alpha_fin_var,alpha_fin_var,alpha_fin_var,alpha_energy_var,alpha_energy_var,alpha_energy_var,alpha_energy_var,alpha_energy_var]';
}

model {
  target += normal_lpdf(beta | k, sqrt(r_squared));
  target += inv_wishart_lpdf(Sigma_var | 12, psi);
  target += normal_lpdf(alpha_energy_var | beta, sqrt(sigma_squared));
  target += normal_lpdf(alpha_fin_var | beta, sqrt(sigma_squared));
  target += multi_normal_lupdf(mu_var | mu_mean, Sigma_var);
  target += multi_normal_lupdf(data_total | mu_var,Sigma_var);
}"

stanmodel <- stan_model(model_code = stancode, model_name="stanmodel")

(data_total is the data matrix, and the "_var" suffixes are added just for consistency with the rest of the code)

The last line gives me an error: PARSER EXPECTED: "(", with respect to the penultimate line of the model code. I've seen on Stack that this kind of error happened to puzzle also more expert developers.

Can anyone help me? I don't really understand what went wrong.

  • 1
    Do you mean to have the single quote at the end of your `mu_mean = ...` in `transformed parameters`? – SmokeyShakers Apr 12 '21 at 15:36
  • It's a transpose sign... Rstan weirdly did not recognize the c() function for building vectors, so I had to assemble that vector manually. – Federico Bindi Apr 12 '21 at 15:40
  • Oh, I see. Maybe try a stan file, instead of a character vector, as suggested in the 'Getting Started' for the package? "Be sure that your Stan programs ends in a blank line without any characters including spaces and comments." – SmokeyShakers Apr 12 '21 at 15:45
  • I tried, but to no avail! The error still shows up with respect to this line: target += multi_normal_lupdf(mu_var | mu_mean, Sigma_var); – Federico Bindi Apr 12 '21 at 16:22
  • Ok, I should've solved the issue (now I'm having a whole lot of other problems, but anyway...) – Federico Bindi Apr 12 '21 at 16:55

0 Answers0