I’m very new to Stan, so I may be missing something simple here. I’m trying to work with the example from the subsection “Estimating Parameters of a Mixture” (currently pg 193). I’ve tried copying the text by hand and copy/pasting from the PDF (my copy/paste version below).
data {
int<lower=1> K; // number of mixture components
int<lower=1> N; // number of data points
real y[N]; // observations
}
parameters {
simplex[K] theta; // mixing proportions
ordered mu[K]; // locations of mixture components
vector<lower=0>[K] sigma; // scales of mixture components
}
model {
real log_theta[K] = log(theta); // cache log calculation
sigma ~ lognormal(0, 2);
mu ~ normal(0, 10);
for (n in 1:N) {
real lps[K] = log_theta;
for (k in 1:K)
lps[k] += normal_lpdf(y[n] | mu[k], sigma[k]);
target += log_sum_exp(lps);
}
}
In both cases I get a syntax error from the parser.
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
error in 'model_foo' at line 8, column 10
-------------------------------------------------
6: parameters {
7: simplex[K] theta; // mixing proportions
8: ordered mu[K]; // locations of mixture components
^
9: vector<lower=0>[K] sigma; // scales of mixture components
-------------------------------------------------
PARSER EXPECTED: <size declaration: integer (data-only) in square brackets>
Error in stanc(model_code = paste(program, collapse = "\n"), model_name = model_cppname, :
failed to parse Stan model 'foo' due to the above error.
I fought with it a bit and finally got the parser to pass by changing the reals for log_theta and lps to vectors, and moving the size declaration square brackets to the type specification for those two and for mu. Was this the right thing to do? Did I miss something? If this was the right thing to do for the program, what’s the best way to share a revision of the code upstream?