Sampling from non-analytical distribution in C++

Before spending a lot of time trying to sample in C++, I’d make very sure that just using pymc, stan or numpyro etc really isn’t an option. Other options come with quite a few challenges. Not just sampling itself and all the build and dependency issues that might come up, but you also need to figure out how to access sampler statistics, compute effective sample size etc. I’m sure that is all doable, but a lot of work. It of course depends a lot on the specifics, but this could quickly become a lot harder to write and maintain than rewriting a density.

But assuming you are sure that that’s what you need:

  • Again, not exactly what you seem to want, but I heard pretty good things about corrosion-rs and/or cxx to include rust dependencies into c++ cmake projects (I’ve never used it myself though). You could use that to directly call nuts-rs from the C++ code, and at least avoid python. That doesn’t solve any issues with convergence checking etc, but at least you could sample. Users would still need to have rustup installed though…
  • As Bob already mentioned, isn’t the stan library just what you are looking for? A C++ library that implements the sampler, given a function that computes the log density and its gradient. It does come with a couple of dependencies itself though (for instance boost), so including that into a larger code base might not be trivial.
1 Like