| Home | Libraries | People | FAQ | More

#### Distributions are Objects

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

| 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:

- It encapsulates the kind of distribution in the C++ type system; so, for example, Students-t distributions are always a different C++ type from Chi-Squared distributions.
- The distribution objects store any parameters associated with the distribution: for example, the Students-t distribution has a degrees of freedom parameter that controls the shape of the distribution. This degrees of freedom parameter has to be provided to the Students-t object when it is constructed.

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.