Hello!
It’s my first time doing inference in Stan and after solving many bugs, this is where I am stuck at. I am trying to simulate a 3-state Markov chain and the transition probabilities come from a Gaussian process with spatio-temporal covariance matrix and some other variables. The error refers to the part that I want to simulate y (the states), which comes from a categorical distribution.
functions {
array[] matrix cor_sep(real nu, real c, real a, real alpha,
data array[] matrix u_ar, data array[] matrix h_ar, data int n_var, data int n_block_row) {
array[n_block_row] matrix[n_var, n_var] corr;
for (n in 1:n_block_row) {
corr[n] = (a * u_ar[n] ^ (2 * alpha) + 1) ^ (-1) .* ((1 - nu) * exp(-c * h_ar[n]));
for (j in 1:n_var) {
for (i in 1:n_var) {
if (i == j)
corr[n][i, j] = (a * (u_ar[n][i, j]) ^ (2 * alpha) + 1) ^ (-1);
}
}
}
return corr;
}
matrix cor_joint(real nu, real c, real a, real alpha, array[] matrix corr_ar,
data int n_var, data int n_block_row) {
int ind_i_start;
int ind_i_end;
int ind_j_start;
int ind_j_end;
matrix[n_var * n_block_row, n_var * n_block_row] corr;
for (i in 1:n_block_row) {
ind_i_start = ((i - 1) * n_var + 1);
ind_i_end = (n_var * i);
for (j in 1:n_block_row) {
ind_j_start = ((j - 1) * n_var + 1);
ind_j_end = (n_var * j);
if (j >= i)
corr[ind_i_start:ind_i_end, ind_j_start:ind_j_end] = corr_ar[j - i + 1];
else
corr[ind_i_start:ind_i_end, ind_j_start:ind_j_end] = corr_ar[i - j + 1];
}
}
return corr;
}
}
data {
int<lower=1> N;
int<lower=1> N_mu;
int<lower=1> lag_u;
int<lower=1> n_ahead;
//number of wind farms
int<lower=1> n_var;
int<lower=1> lag_max;
int<lower=1> n_block_row;
matrix[n_var, n_var] dist;
vector[N_mu] X[N];
array[n_block_row] matrix[n_var, n_var] h_ar;
array[n_block_row] matrix[n_var, n_var] u_ar;
//the final y
int<lower = -1, upper = 1> y[N,n_var];
}
parameters {
// model parameters
real<lower=0, upper=1> nu;
real<lower=0> c;
real<lower=0> a;
real<lower=0, upper=1> alpha;
//new_parameters
//array[N] matrix[3,3] beta;
//array[n_var] matrix[3,3] b;
//array[N,n_var] simplex[3] theta[3];
vector[N]beta[3,3];
matrix[N,n_var]b[3,3];
// mean
vector[N_mu] mu;
}
transformed parameters {
array[n_block_row] matrix[n_var, n_var] c_sep;
matrix[n_var * n_block_row, n_var * n_block_row] c_joint;
array[N, n_var, 3, 3] simplex[3] theta;
c_sep = cor_sep(nu, c, a, alpha, u_ar, h_ar, n_var, n_block_row);
c_joint = cor_joint(nu, c, a, alpha, c_sep, n_var, n_block_row);
for(i in 2:N){
for(j in 1:n_var){
for(t in 1:3){
for(k in 1:3){
theta[i,j,t,k] = softmax(beta[i,t,k]*X[i] + b[i,j,t,k]);
}
}
}
}
}
model {
// flat priors
nu ~ uniform(0, 1);
c ~ uniform(0.0001, 5);
a ~ uniform(0.0001, 5);
alpha ~ uniform(0, 1);
mu ~ uniform(-10,10);
for(i in 2:N){
X[i]~ multi_normal(mu, c_joint);
for(j in 1:n_var){
y[i,j] ~ categorical(theta[y[i-1,j]]);
}
}
}
"
And this is the error:
> fit_sep <- stan(model_code = fit_sep_stan_code, iter = 10000, verbose = FALSE,
+ chain = 4, data = stan_data, init = stan_init, seed = 123)
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
0
Semantic error in 'string', line 116, column 8 to column 46:
Ill-typed arguments to '~' statement. No distribution 'categorical' was found with the correct signature.
Any help is appreciated!