Hello!
I am trying to re-write a model that I have using reduced_sum to parallelize chains but unfortunately I cannot get cmdstan to compile the model. The error, according to error messages, should be somewhere in types but I can’t figure what’s exactly wrong. I would appreciate if you could explain me what needs to be changed. I have seen another post on forum from 2 years ago, but I got a lot more categorical variables and transformed parameters which complicates learning from the suggestions given there.
Here is the model:
functions{
real partial_sum(vector[] pos_slice,
int start, int end,
int[] doculect,
vector[] phy, vector[] geo,
int[] role, int[] flag,
matrix[] role_flag, int[] text,
int[] textID,
real beta_w, real[] weight){
return bernoulli_logit_lpmf(pos_slice | phy[doculect[start:end]] +
geo[doculect[start:end]] +
role_flag[role[start:end], flag[start:end]] +
text[textID[start:end]] +
beta_w * weight[start:end]);
}
matrix cov_GPL2(matrix x, real sq_alpha, real sq_rho, real delta) {
int N = dims(x)[1];
matrix[N, N] K;
for (i in 1:(N-1)) {
K[i, i] = sq_alpha + delta;
for (j in (i + 1):N) {
K[i, j] = sq_alpha * exp(-sq_rho * square(x[i,j]));
K[j, i] = K[i, j];
}
}
K[N, N] = sq_alpha + delta;
return K;
}
matrix cov_GPL1(matrix x, real sq_alpha, real sq_rho, real delta) {
int N = dims(x)[1];
matrix[N, N] K;
for (i in 1:(N-1)) {
K[i, i] = sq_alpha + delta;
for (j in (i + 1):N) {
K[i, j] = sq_alpha * exp(-sq_rho * x[i,j] );
K[j, i] = K[i, j];
}
}
K[N, N] = sq_alpha + delta;
return K;
}
}
data{
int num_doc;
int n;
int pos[n];
real weight[n];
int textID[n];
int flag[n];
int role[n];
int doculect[n];
matrix[num_doc, num_doc] phylo;
matrix[num_doc, num_doc] geo_dist;
}
parameters{
matrix[2,5] z_rf;
vector<lower=0>[2] sigma_rf;
cholesky_factor_corr[2] L_Rho_rf;
vector[195] text;
real beta_w;
vector[num_doc] phy_z;
real<lower=0> etasq_phy;
real<lower=0> rhosq_phy;
real<lower=0> delta_phy;
vector[num_doc] geo_z;
real<lower=0> etasq_geo;
real<lower=0> rhosq_geo;
real<lower=0> delta_geo;
real mu_text;
real<lower=0> sigma_text;
}
transformed parameters{
matrix[5,2] role_flag;
vector[num_doc] phy;
matrix[num_doc,num_doc] phy_Sigma;
matrix[num_doc,num_doc] phy_SIGMA;
vector[num_doc] geo;
matrix[num_doc,num_doc] geo_Sigma;
matrix[num_doc,num_doc] geo_SIGMA;
geo_SIGMA = cov_GPL2(geo_dist, etasq_geo, rhosq_geo, delta_geo);
geo_Sigma = cholesky_decompose(geo_SIGMA);
geo = geo_Sigma * geo_z;
phy_SIGMA = cov_GPL1(phylo, etasq_phy, rhosq_phy, delta_phy);
phy_Sigma = cholesky_decompose(phy_SIGMA);
phy = phy_Sigma * phy_z;
role_flag = (diag_pre_multiply(sigma_rf, L_Rho_rf) * z_rf)';
}
model{
vector[n] p;
delta_geo ~ exponential( 2 ); // more relatex compared to delta_phy
rhosq_geo ~ exponential( 0.35 );
etasq_geo ~ exponential( 0.35 );
geo_z ~ normal( 0 , 1 );
delta_phy ~ exponential( 6 ); // very constrained
rhosq_phy ~ exponential( 0.35 );
etasq_phy ~ exponential( 0.35 );
phy_z ~ normal( 0 , 1 );
beta_w ~ normal( 0.5 , 1.5 );
// Multilevel textID variable
text ~ normal( mu_text, sigma_text );
mu_text ~ normal(0, 0.5);
sigma_text ~ exponential(1);
L_Rho_rf ~ lkj_corr_cholesky( 2 );
sigma_rf ~ exponential( 1 );
to_vector( z_rf ) ~ normal( 0 , 1 );
int grainsize = 1;
target += reduce_sum(partial_sum, pos,
grainsize,
doculect,
phy, geo,
role, flag,
role_flag, text,
textID,
beta_w, weight);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//for ( i in 1:n ) {
// p[i] = phy[doculect[i]] + geo[doculect[i]] + role_flag[role[i], flag[i]] + text[textID[i]] + beta_w * weight[i];
//p[i] = inv_logit(p[i]);
//}
//pos ~ binomial( 1 , p );
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
generated quantities{
matrix[2,2] Rho_rf;
Rho_rf = multiply_lower_tri_self_transpose(L_Rho_rf);
}
And here is the error message when I try to compile with cmdstan_model
function:
Semantic error in 'C:/Users/acraev/AppData/Local/Temp/RtmpklpNFr/model-5fc84d873a3f.stan', line 11, column 46 to line 12, column 58:
-------------------------------------------------
9: int[] textID,
10: real beta_w, real[] weight){
11: return bernoulli_logit_lpmf(pos_slice | phy[doculect[start:end]] +
^
12: geo[doculect[start:end]] +
13: role_flag[role[start:end], flag[start:end]] +
-------------------------------------------------
Ill-typed arguments supplied to infix operator +. Available signatures:
(int, int) => int
(real, real) => real
(real, vector) => vector
(vector, real) => vector
(vector, vector) => vector
(complex, complex) => complex
(real, row_vector) => row_vector
(row_vector, real) => row_vector
(row_vector, row_vector) => row_vector
(real, matrix) => matrix
(matrix, real) => matrix
(matrix, matrix) => matrix
(complex, complex_vector) => complex_vector
(complex_vector, complex) => complex_vector
(complex_vector, complex_vector) => complex_vector
(complex, complex_row_vector) => complex_row_vector
(complex_row_vector, complex) => complex_row_vector
(complex_row_vector, complex_row_vector) => complex_row_vector
(complex, complex_matrix) => complex_matrix
(complex_matrix, complex) => complex_matrix
(complex_matrix, complex_matrix) => complex_matrix
Instead supplied arguments of incompatible type: array[]
vector, array[] vector.
mingw32-make.exe: *** [make/program:50: C:\Users\acraev\AppData\Local\Temp\RtmpklpNFr\model-5fc84d873a3f.hpp] Error 1
I apologize if this is a fairly basic question, that’s my first time trying to parallelize the computation. Thank you very much in advance!