Automated tests for Stan functions

Is there a recommended way to automate testing for hand-written Stan functions?

For example, say I define a new Stan function:

functions {
  real my_square(real x) {
    return square(x);

I would like to verify that the output of this function is correct for a set of inputs via some sort of automated procedure.

So far, the two options I can think of are:

  • Use the rstan interface and expose_stan_functions to be able to directly call the function in R, at which point the output can be compared to some reference output.
  • Write a dummy model in Stan that serves only to execute the function on a set of inputs, and then outputs the results via print. You could perhaps implement this in a generated data block, and have parameters and model be empty. The output could then be read externally and checked against some reference.

Are there any other options I’ve missed?


  • Herb
1 Like

Two additional options using cmdstan:

A cmdstanr solution due to @rok_cesnovar

And a CLI solution to return log probabilties from cmdstan due to @syclik and @dmuck


Thanks for pointing that out.

I have however stopped updating the gist, the latest version of that function is available here, together with a vignette.