Use Stan easily from c++

c++api

#1

Hi,

I was wondering if there has been any progress on this question here.

The idea is being able to use Stan from C++ as easily as it is from R or Python. My use case is that I am doing data acquisition and pre-processing in C++ and writing files to pass the data to cmdstan is a real bottleneck.

Naively, I was thinking that, once the stan file is compiled to a C++ class, there would be a simple way to fit the model to data provided as through Eigen data structures.

Thanks!

Garjola


#2

If you have the C++ file generated from a Stan program, then it is not so hard. The C++ file will have a class with a method that starts like

   template <bool propto, bool jacobian, typename T_>
    T_ log_prob(Eigen::Matrix<T_,Eigen::Dynamic,1>& params_r,
               std::ostream* pstream = 0) const {

so it can be evaluated with a long vector of proposals for the real parameters. I think you pretty much need to just call the constructor, although that requires a stan::io::var_context, and then it is ready to go.


#3

Thanks for your quick answer. I think I’ll have to get familiar with the API before understanding how to use stan::io::var_context.

I have been looking at the code generated from the Bernouilli example and I don’t think I’ll be able to understand how to pass the data to fit the model.

Maybe one of the Stan developers could take a simple example and show how to do it?

Is there link to the Doxygen documentation for the API? I have found the description of the model concept which can be useful for starters, but links to other classes would be helpful.

Thanks again!


#4

There’s some code in the unit tests that might be helpful, specifically:

stan/src/test/unit/services/sample/standalone_gqs_big_test.cpp

also, you could look at some of the cmdstan code:


#5

C++ isn’t a very convenient language for optional arguments and heterogeneous data structures like dictionaries. The var_context type is a very low level interface meant to read data out of flat memory structures. What you’d want is a var_context you could add variables to one at a time in an easy way, which I don’t think we currently have. We’re going to need something like that soon to do round trips and restarts in a structured way.

One of the reasons we’ve been reluctant to dump a lot of time into documenting the C++ API is that it’s been pretty volatile compared to the Stan language and interface APIs.


#6

I understand the issue about C++ convenience (alhough heterogeneous containers and optional are coming to the language and already available in boost).

Wouldn’t it be useful to reuse what has been done for python and R ?

The bridge between python/R and C++ seems to be doing what is missing here (and it is already in C++). If such a C++ API was provided in Stan, maintaining other interfaces should be easier also, since it could hide the evolutions of the internal “volatile” API.

I know I am not helping much here, but I am reluctant to write code that somebody else can write better than me ;)


#7

What do you mean? I think the direction is going the wrong way… what was done in Stan (now CmdStan) was reused for Python and R.

As @mitzimorris has mentioned, there are actually a bunch of examples in the Stan repo’s unit tests of creating classes that implement the var_context interface that are based directly on C++ objects like std::vector. So… I think what you’re looking for is already there, but isn’t packaged in a way that’s really robust and general.

If you provide a lot more detail and a concrete use case, I can provide some more help.