How to do simple arithmetic in CmdStan

I know it seems like overkill, but I wanted to know if it is possible to make simple functions in cmdStan with no sampling. Say for example say I wanted to make a function that squared the a value.

The code below compiles, but when I try to run it I just get the standard help information asking for the data file. And the sampler I want to use. How can I just run a simple function like this?


functions{
  real foo(data real x) {
  return x^2;
  }
}

transformed data {
  real x=2;
  real a;
  a = foo(x);
  print(a);
}

Nah, there is no way to expose functions to the command line line this in cmdstan sorry.

expose_stan_functions does this in Rstan. There’ve been talks about doing various things like this for cmdstan but no big plans or anything.

Though if you just want to print, the simplest way of doing it is selecting sample and algorithm=fixed_param and setting the number of samples to 1.

If you want the result in the CSV output you could

functions{
   real foo(data real x) {
      return x^2;
   }
}
generated quantities {
       real a = foo(2);
}

and again setting the fixed_param algorithm with 1 sample.

1 Like