2

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.

2 Answers 2

4

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.

0

There is currently no compound declaration and definition within the Stan program (as of v2.9.0).

Fixing the syntax issue won't fix the statistical model, however. I believe there's a continuous change point model in the manual that does what you're attempting, so please check there for a solution.

1
  • 1
    There's only a discrete change-point model in the chapter on marginalizing out latent discrete parameters. Mar 11, 2016 at 17:56

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.