Use Stan easily from c++

c++api

#21

I had seen some documentation on contributing new functions to Stan, but they seemed more on the development than the modeling side. This appears to be more modeling but still require a little more than just writing the model, but it may be what I am looking for after all. Is there a full example of this in the CmdStan documentation? That would be really helpful for the general audience (in which I include myself). Also, if it is implemented as a Stan/Math header and called via CmdStan it means it should be available to any of the interfaces, is that right?
If I manage maybe I can try to write a tutorial for it myself. Thanks.


#22

Section 4.4

No, there is an additional step where you have to modify the Stan library so that the new function is exposed in the Stan language.


#23

For benefit of @twistedmersenne: this step is near-trivial.


#24

Thanks for the pointers. I’ll give it a go. I think this can be a powerful way of extending Stan, but I think between the CmdStan reference and online wiki the documentation is probably lacking for non-developers, so at this point I still find it easier to implement a simpler version of the model and sampler from scratch than implementing a new Stan function.

The thread mentioned above is helpful, too. I understand the basic usage of templates, but I normally don’t use them and I don’t understand why they are needed here, so all in all I’m still at a loss here. I’ll write an example function and maybe open a new thread for that (or maybe extend that one mentioned). Thanks.


#25

They are needed because if you provide them then the Stan math library automatically calculates the gradient of the function for you, which is required for HMC, ADVI, and L/BFGS.


#26

That’s an understatement. We’re working on developer-facing doc. It’s always the last to get attention. The cobbler’s children have no shoes and all that.

One way to see how to write a templated function for Stan in full generality is to look at the C++ generated by Stan for a user-defined function. Usually, you won’t need to template that generally for ordinary use because you’ll know the types.