Whoever has time to read this,
I am getting an error when tying to parse a multiple-output Gaussian process model with predictive inference. I am a beginner in using Stan and my code is mainly based on the Stan’s User Guide. My model is as follows:
data {
int<lower=1> N1;
int<lower=1> D;
int<lower=1> q;
int<lower=1> Np;
matrix[N1, q] x1;
matrix[N1, D] y;
matrix[Np, q] xp;
}
transformed data {
real delta = 1e-9;
int<lower=1> N = N1 + Np;
matrix[N,q] X;
for (i in 1:N1) X[i,] = x1[i,];
for (i in 1:Np) X[N1 + i,] = xp[i,];
}
parameters {
vector<lower=0>[q-1] theta;
vector[q] beta;
vector<lower=0>[D] alpha;
cholesky_factor_corr[D] L_Omega;
matrix[N, D] eta;
}
transformed parameters{
real<lower=0> sigma;
matrix[N, D] f;
matrix[N,q-1] z;
vector[N] mu = X * beta;
matrix[N,D] MEAN = append_col(mu,mu);
for(i in 2:q)
z[,i-1] = X[, i]/theta[i-1];
{
matrix[N, N] L_K;
matrix[N, N] K;
for (i in 1:(N - 1)) {
K[i, i] = 1 + delta;
for (j in (i + 1):N) {
K[i, j] = exp(-sum(square(z[i, ] - z[j, ])));
K[j, i] = K[i, j];
}
}
K[N, N] = 1 + delta;
// matrix[N, N] K = cov_exp_quad(x, 1.0, rho);
// diagonal elements
// for (n in 1:N)
// K[n, n] = K[n, n] + delta;
L_K = cholesky_decompose(K);
f = MEAN + L_K * eta
* diag_pre_multiply(alpha, L_Omega)';
}
}
model {
for(i in 1:(q-1))
theta[i] ~ exponential(0.2);
beta[1] ~ normal(0,10);
for(i in 2:q)
beta[i] ~ normal(0,4);
alpha ~ std_normal();
sigma ~ std_normal();
L_Omega ~ lkj_corr_cholesky(3);
to_vector(eta) ~ std_normal();
to_vector(y) ~ normal(to_vector(f[1:N1,]), sigma);
}
generated quantities {
matrix[D, D] Omega;
matrix[Np, D] yp;
Omega = L_Omega * L_Omega';
to_vector(yp) = normal_rng(to_vector(f[(N1+1):N]), sigma);// something's wrong here
}
The errors I get are:
Left-hand side of sampling statement (~) may contain a non-linear transform of a parameter or local variable.
If it does, you need to include a target += statement with the log absolute determinant of the Jacobian of the transform.
Left-hand-side of sampling statement:
sigma ~ std_normal(…)
Illegal statement beginning with non-void expression parsed as
to_vector(yp)
Not a legal assignment, sampling, or function statement. Note that
- Assignment statements only allow variables (with optional indexes) on the left;
- Sampling statements allow arbitrary value-denoting expressions on the left.
- Functions used as statements must be declared to have void returns
error in ‘model151022a7486e_MVARgpe’ at line 79, column 2
77: Omega = L_Omega * L_Omega';
78: //to_vector(yp) = normal_rng(to_vector(f[((N1+1):N),]), sigma);
79: to_vector(yp) = normal_rng(to_vector(f[(N1+1):N]), sigma);
^
80: //for (i in 1:Np)
PARSER EXPECTED: <one of the following:
a variable declaration, beginning with type
(int, real, vector, row_vector, matrix, unit_vector,
simplex, ordered, positive_ordered,
corr_matrix, cov_matrix,
cholesky_corr, cholesky_cov
or a
or ‘}’ to close variable declarations and definitions>
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model ‘MVARgpe’ due to the above error.
Thanks!