beta_distribution.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* boost random/beta_distribution.hpp header file
  2. *
  3. * Copyright Steven Watanabe 2014
  4. * Distributed under the Boost Software License, Version 1.0. (See
  5. * accompanying file LICENSE_1_0.txt or copy at
  6. * http://www.boost.org/LICENSE_1_0.txt)
  7. *
  8. * See http://www.boost.org for most recent version including documentation.
  9. *
  10. * $Id$
  11. */
  12. #ifndef BOOST_RANDOM_BETA_DISTRIBUTION_HPP
  13. #define BOOST_RANDOM_BETA_DISTRIBUTION_HPP
  14. #include <cassert>
  15. #include <istream>
  16. #include <iosfwd>
  17. #include <boost/random/detail/operators.hpp>
  18. #include <boost/random/gamma_distribution.hpp>
  19. namespace boost {
  20. namespace random {
  21. /**
  22. * The beta distribution is a real-valued distribution which produces
  23. * values in the range [0, 1]. It has two parameters, alpha and beta.
  24. *
  25. * It has \f$\displaystyle p(x) = \frac{x^{\alpha-1}(1-x)^{\beta-1}}{B(\alpha, \beta)}\f$.
  26. */
  27. template<class RealType = double>
  28. class beta_distribution {
  29. public:
  30. typedef RealType result_type;
  31. typedef RealType input_type;
  32. class param_type {
  33. public:
  34. typedef beta_distribution distribution_type;
  35. /**
  36. * Constructs a @c param_type from the "alpha" and "beta" parameters
  37. * of the distribution.
  38. *
  39. * Requires: alpha > 0, beta > 0
  40. */
  41. explicit param_type(RealType alpha_arg = RealType(1.0),
  42. RealType beta_arg = RealType(1.0))
  43. : _alpha(alpha_arg), _beta(beta_arg)
  44. {
  45. assert(alpha_arg > 0);
  46. assert(beta_arg > 0);
  47. }
  48. /** Returns the "alpha" parameter of the distribtuion. */
  49. RealType alpha() const { return _alpha; }
  50. /** Returns the "beta" parameter of the distribution. */
  51. RealType beta() const { return _beta; }
  52. /** Writes a @c param_type to a @c std::ostream. */
  53. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
  54. { os << parm._alpha << ' ' << parm._beta; return os; }
  55. /** Reads a @c param_type from a @c std::istream. */
  56. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
  57. { is >> parm._alpha >> std::ws >> parm._beta; return is; }
  58. /** Returns true if the two sets of parameters are the same. */
  59. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
  60. { return lhs._alpha == rhs._alpha && lhs._beta == rhs._beta; }
  61. /** Returns true if the two sets of parameters are the different. */
  62. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
  63. private:
  64. RealType _alpha;
  65. RealType _beta;
  66. };
  67. /**
  68. * Constructs an @c beta_distribution from its "alpha" and "beta" parameters.
  69. *
  70. * Requires: alpha > 0, beta > 0
  71. */
  72. explicit beta_distribution(RealType alpha_arg = RealType(1.0),
  73. RealType beta_arg = RealType(1.0))
  74. : _alpha(alpha_arg), _beta(beta_arg)
  75. {
  76. assert(alpha_arg > 0);
  77. assert(beta_arg > 0);
  78. }
  79. /** Constructs an @c beta_distribution from its parameters. */
  80. explicit beta_distribution(const param_type& parm)
  81. : _alpha(parm.alpha()), _beta(parm.beta())
  82. {}
  83. /**
  84. * Returns a random variate distributed according to the
  85. * beta distribution.
  86. */
  87. template<class URNG>
  88. RealType operator()(URNG& urng) const
  89. {
  90. RealType a = gamma_distribution<RealType>(_alpha, RealType(1.0))(urng);
  91. RealType b = gamma_distribution<RealType>(_beta, RealType(1.0))(urng);
  92. return a / (a + b);
  93. }
  94. /**
  95. * Returns a random variate distributed accordint to the beta
  96. * distribution with parameters specified by @c param.
  97. */
  98. template<class URNG>
  99. RealType operator()(URNG& urng, const param_type& parm) const
  100. {
  101. return beta_distribution(parm)(urng);
  102. }
  103. /** Returns the "alpha" parameter of the distribution. */
  104. RealType alpha() const { return _alpha; }
  105. /** Returns the "beta" parameter of the distribution. */
  106. RealType beta() const { return _beta; }
  107. /** Returns the smallest value that the distribution can produce. */
  108. RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
  109. { return RealType(0.0); }
  110. /** Returns the largest value that the distribution can produce. */
  111. RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
  112. { return RealType(1.0); }
  113. /** Returns the parameters of the distribution. */
  114. param_type param() const { return param_type(_alpha, _beta); }
  115. /** Sets the parameters of the distribution. */
  116. void param(const param_type& parm)
  117. {
  118. _alpha = parm.alpha();
  119. _beta = parm.beta();
  120. }
  121. /**
  122. * Effects: Subsequent uses of the distribution do not depend
  123. * on values produced by any engine prior to invoking reset.
  124. */
  125. void reset() { }
  126. /** Writes an @c beta_distribution to a @c std::ostream. */
  127. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, beta_distribution, wd)
  128. {
  129. os << wd.param();
  130. return os;
  131. }
  132. /** Reads an @c beta_distribution from a @c std::istream. */
  133. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, beta_distribution, wd)
  134. {
  135. param_type parm;
  136. if(is >> parm) {
  137. wd.param(parm);
  138. }
  139. return is;
  140. }
  141. /**
  142. * Returns true if the two instances of @c beta_distribution will
  143. * return identical sequences of values given equal generators.
  144. */
  145. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(beta_distribution, lhs, rhs)
  146. { return lhs._alpha == rhs._alpha && lhs._beta == rhs._beta; }
  147. /**
  148. * Returns true if the two instances of @c beta_distribution will
  149. * return different sequences of values given equal generators.
  150. */
  151. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(beta_distribution)
  152. private:
  153. RealType _alpha;
  154. RealType _beta;
  155. };
  156. } // namespace random
  157. } // namespace boost
  158. #endif // BOOST_RANDOM_BETA_DISTRIBUTION_HPP