Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stan error casting parameter to int

Tags:

stan

rstan

I'm trying to do a switchpoint analysis with STAN. I've got a data vector y that has two different sequences of gaussian random variables. The goal is to find the posterior distribution of when a shift might have occured. I'm using RStan to run it, but the error lies within STAN.

This is the STAN code;

data {
  int N;
  vector[N] y;
}
parameters {
  real mu1;
  real sigma1;
  real mu2;
  real sigma2;
  real<lower=0, upper=N> shift;
}
model {
  int i_shift <- round(shift);
  for(n1 in 1:i_shift)
    y[n1] ~ normal(mu1, sigma1);
  for(n2 in i_shift:N)
    y[n2] ~ normal(mu2, sigma2);
}

The parser (which comes with Rstudio) gives the following error;

SYNTAX ERROR, MESSAGE(S) FROM PARSER:


ERROR at line 13

 11:    }
 12:    model {
 13:      int i_shift <- round(shift);
                      ^
 14:      for(n1 in 1:i_shift)

PARSER EXPECTED: ";"
Error in stanc(model_code = paste(program, collapse = "\n"), model_name = model_cppname,  : 
  failed to parse Stan model due to the above error.

Why can't it handle the variable assingment that does the casting? Does STAN require a different pattern for this sort of analysis. I've tried to create an integer variable in the parameters but STAN doesn't appear to have support for random integer variables, only continous ones.

like image 325
cantdutchthis Avatar asked Mar 07 '26 20:03

cantdutchthis


1 Answers

The root cause is that Stan doesn't allow assignment of real values to integers. In retrospect, we probably wouldn't have included round() at all as it introduces discontinuities, and thus defeats differentiability, whichis the basis of our HMC and optimization and approximate inference algorithms.

like image 100
Bob Carpenter Avatar answered Mar 09 '26 23:03

Bob Carpenter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!