Syntax errors for a user defined function

Hi there,

I got some syntax errors for a user defined function in Stan, but couldn’t figure out the reason. Sorry I am not familiar with Stan’s syntax especially for defining the function. Here is what I am trying,

There is a vector parameter vector[N_evt] evt_amp in which each element follows evt_amp ~ gamma(2,2); I’d like to generate a matrix in function generate_evt() to

  1. assign vector[N_evt] evt_amp to a longer vector vector[N_obs] event_temp; (initiated as an all 0s vector) according to a known index int evt_idx[N_evt];.
  2. then toeplitz event_temp and an all zeros vector append_row(event_temp[1], rep_vector(0, N_f-1))'

functions {
  matrix generate_evt(int evt_idx, vector evt_amp, int N_f, int N_obs) {
  // Assign evt_amp to event_temp according to index evt_idx
  matrix[N_obs, N_f] evt;
  vector[N_obs] event_temp = rep_vector(0, N_obs);
  event_temp[evt_idx] = evt_amp;
  // Toeplitz event_temp
  evt[1, ] = append_row(event_temp[1], rep_vector(0, N_f-1))';
  evt[, 1] = event_temp;
  for (i in 2:N_obs) {
      evt[i, 2:N_f] = evt[i - 1, 1:(N_f)];
  }
  return evt;
  }  
}
} 

data{
  ...
  int<lower=1> N_f;
  int<lower=1> N_obs;
  int evt_idx[N_evt];
}

parameters {  ...
  vector[N_evt] evt_amp;
}

transformed parameters {  ...
  evt = generate_evt(evt_idx, evt_amp, N_f, N_obs);  
}

model { ...
 evt_amp ~ gamma(2,2);  
}

I got error for this line event_temp[evt_idx] = evt_amp; , saying left hand type = real, right hand type = vector. So I changed the type of evt_amp to real like this matrix generate_evt(int evt_idx, real evt_amp, int N_f, int N_obs) , then got the following error for this line real event_empt[N_obs] = rep_vector(0, N_obs); saying

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Variable definition base type mismatch, variable declared as base type real variable definition has base type vector

Could you please help to fix this? I am basically circulating on how to assign values to a vector :) Thanks.

I guess the reason is the declare in function argument, I should have used int[] instead of int, since it is an int array not a single integer.

Hi,
did you manage to resolve this or are you still stuck? Types in Stan can be a bit tricky as it distinguishes between arrays of reals, vectors and row vectors, which are not interchangeable, but have to be converted to each other - see 5 Data Types and Declarations | Stan Reference Manual for more info on data types and 7 Mixed Operations | Stan Functions Reference for conversion operations.

Best of luck with your model!

Hi @martinmodrak, thanks a lot for asking. It was my mistake. I solved it by replacing int evt_idx with int[] evt_idx , since evt_idx is a vector with integers in it.

May I ask another question regarding how to reverse a vector in a user-defined function? I found a similar post Dot products of vectors to perform 1D convolution - #8 by zcai and tried the solutions but they did not work. There is indeed a reverse() function but not yet available in rstan. Let’s say we have a for loop and inside it we just do a dot_product between two vectors a and b, but we reverse the index of b from i to 1 instead of 1 to i. I tried the suggestion in the above post dot_product(a[1:i], b[sort_desc(1:i)]);, but I got syntax error. Do you think I can only do it with a user-defined reverse funciton? or I just missed something here. Sorry I am not familiar with stan lib for user defined functions.

Two things: you can install a recent version of rstan via Repository for distributing (some) stan-dev R packages | r-packages (the version on CRAN is quite old for stupid reasons).

I’ve never used reverse or sort_desc myself, what sort of error do you get? In any case, when I doubt that I have my linear algebra right, I just write it explicitly on a for loop - that always works :-)

Thanks, It’s a syntax error like this
image

My rstan version is rstan (Version 2.21.2, GitRev: 2e1f913d3ca3), not sure if it is the latest version but I installed it from Github repo.

Looks a bit weird, but hard to tell without seeing the whole code, could you share the whole code (note that you can use triple backtics (```) to format code neatly.

The latest rstan is 2.26.

Thanks. Here it is, it’s just a 1D convolution function. In the for loop I got this PARSER EXPECTED : “(” from Rstudio. Maybe I have some silly mistake somewhere. If you see any weird way of coding could you please feel free to let me know? it will be a good opportunity for me to learn.

  vector stan_convolve_1D(int[] evt_idx, vector evt_amp, int ly, int lx, int rec, vector y) {
  vector[lx] x;
  vector[lx + ly] x_temp;
  vector[lx + ly] y_temp;
  vector[lx + ly] cvxy;
  x = rep_vector(0, lx);
  x[evt_idx] = evt_amp;

  x_temp = append_row(x,rep_vector(0,ly));
  y_temp = append_row(y,rep_vector(0,lx));
  cvxy = rep_vector(0,lx + ly);
  for(i in 2:(lx + ly)){
    cvxy[i] = dot_product(x_temp[sort_desc(1:(i-1))], y_temp[1:(i-1)]);
  }
  return cvxy[2:(lx+1)];
  }

Do you think 2.26 has reverse() function in it? If so I could just update my version, since I’ve tried cmdstan and reverse() function worked :)

Cmdstan should also be on 2.26, so it should work.

The code looks good at first glance, so maybe it is just older version of rstan not recognizing sort_desc. However, it looks like you throw away some values for cvxy that you compute in the loop - i.e. cvxy[lx + 2] and above is never used, so if you don’t use it you may as well not compute it all and save some performance.

Thanks. Those are the boundary of the cvxy since we did padding at the beginning. Then I will go for the latest rstan version.

Hi @martinmodrak, just to update, after installing 2.26, the previous function works without syntax error and the reverse() function also works. Thanks again.