Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards


Distributions are Objects

Each kind of distribution in this library is a class type - an object, with member functions.

[Tip] Tip

If you are familiar with statistics libraries using functions, and 'Distributions as Objects' seem alien, see the comparison to other statistics libraries.

Policies provide optional fine-grained control of the behaviour of these classes, allowing the user to customise behaviour such as how errors are handled, or how the quantiles of discrete distributions behave.

Making distributions class types does two things:

Although the distribution classes in this library are templates, there are typedefs on type double that mostly take the usual name of the distribution (except where there is a clash with a function of the same name: beta and gamma, in which case using the default template arguments - RealType = double - is nearly as convenient). Probably 95% of uses are covered by these typedefs:

// using namespace boost::math; // Avoid potential ambiguity with names in std <random>
// Safer to declare specific functions with using statement(s):

using boost::math::beta_distribution;
using boost::math::binomial_distribution;
using boost::math::students_t;

// Construct a students_t distribution with 4 degrees of freedom:
students_t d1(4);

// Construct a double-precision beta distribution
// with parameters a = 10, b = 20
beta_distribution<> d2(10, 20); // Note: _distribution<> suffix !

If you need to use the distributions with a type other than double, then you can instantiate the template directly: the names of the templates are the same as the double typedef but with _distribution appended, for example: Students t Distribution or Binomial Distribution:

// Construct a students_t distribution, of float type,
// with 4 degrees of freedom:
students_t_distribution<float> d3(4);

// Construct a binomial distribution, of long double type,
// with probability of success 0.3
// and 20 trials in total:
binomial_distribution<long double> d4(20, 0.3);

The parameters passed to the distributions can be accessed via getter member functions:

d1.degrees_of_freedom();  // returns 4.0

This is all well and good, but not very useful so far. What we often want is to be able to calculate the cumulative distribution functions and quantiles etc for these distributions.