Seq and floor function

Hi,

Does Stan have the functions seq() and floor() as in R so far? I was trying to call them but it did not work in my stan codes.

Does anyone kindly share their Stan functions for those two? I tried to search the source codes in C++ and R and were not able to find either. I was trying to write in Stan by myself, but I encountered programming issues.

Thanks for your time and help in advance.

Yan

Hi,

I was trying to write a seq_fun(). I just realized Stan indeed have floor() function but it returns the real value not integer. I need return integer since it needs to be put in my index for the loop. If anyone have a good way to do the same thing, feel free to share your thoughts. Thanks.

    functions{
       vector seq_fun(real start, real end, real by_val {   
       real h;
       real int_h;
       vector[1+int_h]  out;
       vector[int_h] out_temp;
       h=(end-start) / by_val;
       int_h = floor(h);
       out[1]= start;
       for (i in 1:int_h)
       { out_temp[i]=start + i*by_val;
         out[i+1]=out_temp[i];
       }
      return(out);}

Look for the linspaced_* functions documented in 5.9 Container construction functions | Stan Functions Reference. To make them work with a by argument, you could for example use linspaced_vector(N, low, low + by*(N-1)).

1 Like

Hi Marco,

Thank you for your reply. This function linspaced_vector seems to be the right one. I tried it in my current Rstan. However, my current stan version is 2.21 in Rstan for R 3.6.3, thus this function is not available yet. I realized this function is available in Stan version 2.26.

Could you let me know how to upgrade my Stan version to 2.26 in R? I tried to reinstall my Rstan in R3.6.3. But the Stan version is still 2.21…

Thanks for your reply again.

Hi all,

I figured out how to write the equally spaced sequence by myself. Hopefully, everyone who cannot use the linspaced_vector due to the lower version of Stan can use it. Feel free to copy and past it as you wish :)

functions{

  vector seq_fun(real start, real end, int N_by)
 {  
       real h;
       vector[N_by]  out;
       h=(end-start) / (N_by-1);
       for (i in 1:N_by)
       { out[i]=start + (i-1)*h; }
      return(out);
 }

}
1 Like

If you are using Rstan, you are stuck at 2.21. If you could instead switch to using CmdStanR, it would be very easy to use the latest Stan release. But as you’ve found yourself a solution, perhaps it’s not necessary anymore. Good luck with your work!

2 Likes

Hi Marco,

Thank you for clarifying this installation issues. I will try CmdStanR later if needed.
Have a nice weekend.

Yan