...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
#include <boost/math/distributions/uniform.hpp>
namespace boost{ namespace math{ template <class RealType = double, class Policy = policies::policy<> > class uniform_distribution; typedef uniform_distribution<> uniform; template <class RealType, class Policy> class uniform_distribution { public: typedef RealType value_type; uniform_distribution(RealType lower = 0, RealType upper = 1); // Constructor. : m_lower(lower), m_upper(upper) // Default is standard uniform distribution. // Accessor functions. RealType lower()const; RealType upper()const; }; // class uniform_distribution }} // namespaces
The uniform distribution, also known as a rectangular distribution, is a probability distribution that has constant probability.
The continuous uniform distribution is a distribution with the probability density function:
f(x) =
and in this implementation:
The choice of x = lower or x = upper is made because statistical use of this distribution judged is most likely: the method of maximum likelihood uses this definition.
There is also a discrete uniform distribution.
Parameters lower and upper can be any finite value.
The random variate x must also be finite, and is supported lower <= x <= upper.
The lower parameter is also called the location parameter, that is where the origin of a plot will lie, and (upper - lower) is also called the scale parameter.
The following graph illustrates how the probability density function PDF varies with the shape parameter:
Likewise for the CDF:
uniform_distribution(RealType lower = 0, RealType upper = 1);
Constructs a uniform distribution with lower lower (a) and upper upper (b).
Requires that the lower and upper parameters are both finite; otherwise if infinity or NaN then calls domain_error.
RealType lower()const;
Returns the lower parameter of this distribution.
RealType upper()const;
Returns the upper parameter of this distribution.
All the usual non-member accessor functions that are generic to all distributions are supported: Cumulative Distribution Function, Probability Density Function, Quantile, Hazard Function, Cumulative Hazard Function, mean, median, mode, variance, standard deviation, skewness, kurtosis, kurtosis_excess, range and support.
The domain of the random variable is any finite value, but the supported range is only lower <= x <= upper.
The uniform distribution is implemented with simple arithmetic operators and so should have errors within an epsilon or two.
In the following table a is the lower parameter of the distribution, b is the upper parameter, x is the random variate, p is the probability and q = 1-p.
Function |
Implementation Notes |
---|---|
|
Using the relation: pdf = 0 for x < a, 1 / (b - a) for a <= x <= b, 0 for x > b |
cdf |
Using the relation: cdf = 0 for x < a, (x - a) / (b - a) for a <= x <= b, 1 for x > b |
cdf complement |
Using the relation: q = 1 - p, (b - x) / (b - a) |
quantile |
Using the relation: x = p * (b - a) + a; |
quantile from the complement |
x = -q * (b - a) + b |
mean |
(a + b) / 2 |
variance |
(b - a) 2 / 12 |
mode |
any value in [a, b] but a is chosen. (Would NaN be better?) |
skewness |
0 |
kurtosis excess |
-6/5 = -1.2 exactly. (kurtosis - 3) |
kurtosis |
9/5 |