Can a compiled program accept data arguments of varying length between runs?

Right now I am fitting multiple models that all use the same basic model structure, but the only thing that changes is the length of the data. Is it possible to compile a Stan program so that it can accept data where the length of inputs changes between each iteration.

So currently I have been re-compiling the Stan code for each fit. But it seems like it would save a lot of time if the model could be compiled only once. So is it possible to have a compiled Stan program accept data that are different lengths each time the program is run?

Yes, you can add an extra item to the top of the data block which is the size.

data {
  int<lower=0> N;
  vector[N] other_data;
}

You’ll then need to pass N as an extra item in your call to Stan at runtime

2 Likes

that’s a really simple answer that I didn’t even think of, thank you!