OCaml interface for Stan

I’ve been developing a program analysis tool in OCaml, and would like to run Stan from the OCaml code. What is the best way to do so?

As of now, what I do is to first use OCaml’s binding for Python to run Python from OCaml. The particular binding I use is pyml. I then run Stan from Python. However, now that the Stan3 compiler is implemented in OCaml, I wonder if I can directly run Stan from OCaml. Also, is there any plan in the near future for developing an opam package for Stan? Thanks a lot!

1 Like

There is not really any way to do this. While the compiler is in OCaml, the output of the compiler is C++ and the Stan algorithms and math libraries are both implemented in C++.

If you’re fine using a separate process to run the Stan program, you can do something similar to CmdStanPy/CmdStanR and run the stan model as a separate process and read in the output CSV it produces, then do whatever you want in OCaml from there

1 Like

I see. Thanks a lot!

If you really wanted to use Stan models through OCaml, it’d make sense to do something like a BridgeStan interface. See:

If you want the whole inference system with sampling, optimization, VI etc., then something like CmdStanPy or CmdStanR would work as @WardBrian suggested.

1 Like