4

I am new to C++ and extremely surprised by the lack of accessible, common probability manipulation tools (i.e. the lack of things in Boost and the standard library). I've done a lot of scientific programming in other languages, but the standard and/or ubiquitious third party add-ons always include a full range of probability tools. A friend billed up Boost to be the equivalent ubiquitous add-on for C++, but as I read the Boost documentation, even it seems to have a dearth of what I would consider extremely elementary built-ins.

I cannot find a built in that takes some sort of array of discrete probabilities and produces an index chosen according to those probabilities. I can of course write my own function for this, but I just wanted to check whether I am missing a standard way to do this.

Having to write my own functions at such a low-level is a bad thing, I feel, but I am writing a new simulation module for a larger project that is all in C++. My usual go-to tactic would be to write it in Python and link the Python to the C++, but because several other people are going to have to manage this code once I finish it, and none of them know Python, I think it would be more prudent to deliver it to them all in C++.

More generally, what do people do in C++ for things like sampling from standard distributions, in particular something as basic as a multi-variate normal distribution?

ely
  • 74,674
  • 34
  • 147
  • 228
  • There is also always http://www.gnu.org/software/gsl/manual/html_node/Random-Number-Distributions.html. And sampling from a histogram is really simple, if you do not want a larger package, just write the 10 lines yourself. – Benjamin Bannier Mar 30 '12 at 23:38

5 Answers5

5

Perhaps I'm misunderstanding your intention, but it seems to me what you want is simply std::discrete_distribution.

leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
  • Yes, this seems to be the most fluid solution to the problem. How did 30+ minutes of Googling things like "C++ standard discrete distribution sample" not hit on this? Oh well, I still won't try Bing anyway :) – ely Mar 30 '12 at 23:54
  • Yeah, it goes like this sometimes in C++, funny. I once spent hours (or what felt like hours) searching for `std::array`! I _knew_ there existed such a class, but it didn't come to my mind that it would have the most obvious possible name. – leftaroundabout Mar 31 '12 at 00:16
3

(Moved from comment.)

Did you look at Boost.Math.StatisticalDistributions? Specifically, its Discrete Probability Distributions?

Boost is not a library, it's a collection of libraries, so it can sometimes be difficult to find exactly what you're looking for – but that doesn't mean it isn't there. ;-]

ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • The links are dead: http://www.boost.org/doc/libs/1_57_0/libs/math/doc/html/dist.html http://www.boost.org/doc/libs/1_57_0/libs/math/doc/html/math_toolkit/stat_tut/dist_params.html – Tobias Madsen Dec 10 '14 at 10:45
2

As mentioned, you'll want to look at boost/math/distributions and friends to meet your needs.

Here's a very good, detailed tutorial on getting these working for you in Boost. You may also want to throw your weight behind stan as well, which looks quite promising within this space.

MrGomez
  • 23,788
  • 45
  • 72
0

You should do less C++ bashing, and more question asking - we try to be helpful and respectful on SO. Questions like yours are often tagged as inflammatory.

Boost::math seems to provide exactly what you're looking for: https://www.quantnet.com/cplusplus-statistical-distributions-boost/ - I'm not 100% sure about how well it handles multi-variate distributions though (nor am I an expert on statistics).

Get it here: http://www.boost.org/doc/libs/1_49_0/libs/math/doc/html/index.html

David Titarenco
  • 32,662
  • 13
  • 66
  • 111
0

Boost's math libraries are pretty good for working with different distributions, but if you are only interested in sampling (as in the problem you mentioned in your post), then looking at the boost Random libraries might be more germane to your task. This link shows how to simulate rolling a weighted die, for example.

Cameron
  • 1,675
  • 11
  • 12