# Syntax errors for a user defined function

**URL:** <https://discourse.mc-stan.org/t/syntax-errors-for-a-user-defined-function/21601>\
**Category:** Modeling\
**Created:** [March 30, 2021, 4:56pm UTC](https://discourse.mc-stan.org/t/syntax-errors-for-a-user-defined-function/21601 "2021-03-30T16:56:03Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![zcai](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/zcai/32/10242_2.png) [@zcai](https://discourse.mc-stan.org/u/zcai)\
**Post date:** [March 30, 2021, 4:56pm UTC](https://discourse.mc-stan.org/t/syntax-errors-for-a-user-defined-function/21601/1 "2021-03-30T16:56:03Z")

</div>

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))'`

* * *

```stan
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.

---

<div class="post-metadata">

**Author:** ![zcai](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/zcai/32/10242_2.png) [@zcai](https://discourse.mc-stan.org/u/zcai)\
**Post date:** [April 1, 2021, 5:45pm UTC](https://discourse.mc-stan.org/t/syntax-errors-for-a-user-defined-function/21601/2 "2021-04-01T17:45:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![martinmodrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/martinmodrak/32/133_2.png) [@martinmodrak](https://discourse.mc-stan.org/u/martinmodrak)\
**Post date:** [April 6, 2021, 8:04pm UTC](https://discourse.mc-stan.org/t/syntax-errors-for-a-user-defined-function/21601/3 "2021-04-06T20:04:48Z")

</div>

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](https://mc-stan.org/docs/2_26/reference-manual/data-types-chapter.html) for more info on data types and [7 Mixed Operations | Stan Functions Reference](https://mc-stan.org/docs/2_26/functions-reference/mixed-operations.html) for conversion operations.

Best of luck with your model!

---

<div class="post-metadata">

**Author:** ![zcai](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/zcai/32/10242_2.png) [@zcai](https://discourse.mc-stan.org/u/zcai)\
**Post date:** [April 6, 2021, 8:19pm UTC](https://discourse.mc-stan.org/t/syntax-errors-for-a-user-defined-function/21601/4 "2021-04-06T20:19:14Z")

</div>

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](https://discourse.mc-stan.org/t/dot-products-of-vectors-to-perform-1d-convolution/9053/8) 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.

---

<div class="post-metadata">

**Author:** ![martinmodrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/martinmodrak/32/133_2.png) [@martinmodrak](https://discourse.mc-stan.org/u/martinmodrak)\
**Post date:** [April 6, 2021, 8:46pm UTC](https://discourse.mc-stan.org/t/syntax-errors-for-a-user-defined-function/21601/5 "2021-04-06T20:46:50Z")

</div>

Two things: you can install a recent version of rstan via [Repository for distributing (some) stan-dev R packages | r-packages](https://mc-stan.org/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 :-)

---

<div class="post-metadata">

**Author:** ![zcai](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/zcai/32/10242_2.png) [@zcai](https://discourse.mc-stan.org/u/zcai)\
**Post date:** [April 6, 2021, 8:52pm UTC](https://discourse.mc-stan.org/t/syntax-errors-for-a-user-defined-function/21601/6 "2021-04-06T20:52:25Z")

</div>

Thanks, It’s a syntax error like this  
 ![image](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/0/05c357f1a9bee566117ed3cc6857b33c4fa98462.png)

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.

---

<div class="post-metadata">

**Author:** ![martinmodrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/martinmodrak/32/133_2.png) [@martinmodrak](https://discourse.mc-stan.org/u/martinmodrak)\
**Post date:** [April 6, 2021, 8:55pm UTC](https://discourse.mc-stan.org/t/syntax-errors-for-a-user-defined-function/21601/7 "2021-04-06T20:55:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zcai](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/zcai/32/10242_2.png) [@zcai](https://discourse.mc-stan.org/u/zcai)\
**Post date:** [April 6, 2021, 9:06pm UTC](https://discourse.mc-stan.org/t/syntax-errors-for-a-user-defined-function/21601/8 "2021-04-06T21:06:12Z")

</div>

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.

```stan
  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 :)

---

<div class="post-metadata">

**Author:** ![martinmodrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/martinmodrak/32/133_2.png) [@martinmodrak](https://discourse.mc-stan.org/u/martinmodrak)\
**Post date:** [April 6, 2021, 9:10pm UTC](https://discourse.mc-stan.org/t/syntax-errors-for-a-user-defined-function/21601/9 "2021-04-06T21:10:56Z")

</div>

> [@zcai](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![zcai](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/zcai/32/10242_2.png) [@zcai](https://discourse.mc-stan.org/u/zcai)\
**Post date:** [April 6, 2021, 9:13pm UTC](https://discourse.mc-stan.org/t/syntax-errors-for-a-user-defined-function/21601/10 "2021-04-06T21:13:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zcai](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/zcai/32/10242_2.png) [@zcai](https://discourse.mc-stan.org/u/zcai)\
**Post date:** [April 6, 2021, 9:24pm UTC](https://discourse.mc-stan.org/t/syntax-errors-for-a-user-defined-function/21601/11 "2021-04-06T21:24:09Z")

</div>

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