fisher_f_distribution.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* boost random/fisher_f_distribution.hpp header file
  2. *
  3. * Copyright Steven Watanabe 2011
  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_FISHER_F_DISTRIBUTION_HPP
  13. #define BOOST_RANDOM_FISHER_F_DISTRIBUTION_HPP
  14. #include <iosfwd>
  15. #include <istream>
  16. #include <boost/config.hpp>
  17. #include <boost/limits.hpp>
  18. #include <boost/random/detail/operators.hpp>
  19. #include <boost/random/chi_squared_distribution.hpp>
  20. namespace boost {
  21. namespace random {
  22. /**
  23. * The Fisher F distribution is a real valued distribution with two
  24. * parameters m and n.
  25. *
  26. * It has \f$\displaystyle p(x) =
  27. * \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}
  28. * \left(\frac{m}{n}\right)^{m/2}
  29. * x^{(m/2)-1} \left(1+\frac{mx}{n}\right)^{-(m+n)/2}
  30. * \f$.
  31. */
  32. template<class RealType = double>
  33. class fisher_f_distribution {
  34. public:
  35. typedef RealType result_type;
  36. typedef RealType input_type;
  37. class param_type {
  38. public:
  39. typedef fisher_f_distribution distribution_type;
  40. /**
  41. * Constructs a @c param_type from the "m" and "n" parameters
  42. * of the distribution.
  43. *
  44. * Requires: m > 0 and n > 0
  45. */
  46. explicit param_type(RealType m_arg = RealType(1.0),
  47. RealType n_arg = RealType(1.0))
  48. : _m(m_arg), _n(n_arg)
  49. {}
  50. /** Returns the "m" parameter of the distribtuion. */
  51. RealType m() const { return _m; }
  52. /** Returns the "n" parameter of the distribution. */
  53. RealType n() const { return _n; }
  54. /** Writes a @c param_type to a @c std::ostream. */
  55. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
  56. { os << parm._m << ' ' << parm._n; return os; }
  57. /** Reads a @c param_type from a @c std::istream. */
  58. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
  59. { is >> parm._m >> std::ws >> parm._n; return is; }
  60. /** Returns true if the two sets of parameters are the same. */
  61. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
  62. { return lhs._m == rhs._m && lhs._n == rhs._n; }
  63. /** Returns true if the two sets of parameters are the different. */
  64. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
  65. private:
  66. RealType _m;
  67. RealType _n;
  68. };
  69. /**
  70. * Constructs a @c fisher_f_distribution from its "m" and "n" parameters.
  71. *
  72. * Requires: m > 0 and n > 0
  73. */
  74. explicit fisher_f_distribution(RealType m_arg = RealType(1.0),
  75. RealType n_arg = RealType(1.0))
  76. : _impl_m(m_arg), _impl_n(n_arg)
  77. {}
  78. /** Constructs an @c fisher_f_distribution from its parameters. */
  79. explicit fisher_f_distribution(const param_type& parm)
  80. : _impl_m(parm.m()), _impl_n(parm.n())
  81. {}
  82. /**
  83. * Returns a random variate distributed according to the
  84. * F distribution.
  85. */
  86. template<class URNG>
  87. RealType operator()(URNG& urng)
  88. {
  89. return (_impl_m(urng) * n()) / (_impl_n(urng) * m());
  90. }
  91. /**
  92. * Returns a random variate distributed according to the
  93. * F distribution with parameters specified by @c param.
  94. */
  95. template<class URNG>
  96. RealType operator()(URNG& urng, const param_type& parm) const
  97. {
  98. return fisher_f_distribution(parm)(urng);
  99. }
  100. /** Returns the "m" parameter of the distribution. */
  101. RealType m() const { return _impl_m.n(); }
  102. /** Returns the "n" parameter of the distribution. */
  103. RealType n() const { return _impl_n.n(); }
  104. /** Returns the smallest value that the distribution can produce. */
  105. RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; }
  106. /** Returns the largest value that the distribution can produce. */
  107. RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
  108. { return std::numeric_limits<RealType>::infinity(); }
  109. /** Returns the parameters of the distribution. */
  110. param_type param() const { return param_type(m(), n()); }
  111. /** Sets the parameters of the distribution. */
  112. void param(const param_type& parm)
  113. {
  114. typedef chi_squared_distribution<RealType> impl_type;
  115. typename impl_type::param_type m_param(parm.m());
  116. _impl_m.param(m_param);
  117. typename impl_type::param_type n_param(parm.n());
  118. _impl_n.param(n_param);
  119. }
  120. /**
  121. * Effects: Subsequent uses of the distribution do not depend
  122. * on values produced by any engine prior to invoking reset.
  123. */
  124. void reset() { }
  125. /** Writes an @c fisher_f_distribution to a @c std::ostream. */
  126. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, fisher_f_distribution, fd)
  127. {
  128. os << fd.param();
  129. return os;
  130. }
  131. /** Reads an @c fisher_f_distribution from a @c std::istream. */
  132. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, fisher_f_distribution, fd)
  133. {
  134. param_type parm;
  135. if(is >> parm) {
  136. fd.param(parm);
  137. }
  138. return is;
  139. }
  140. /**
  141. * Returns true if the two instances of @c fisher_f_distribution will
  142. * return identical sequences of values given equal generators.
  143. */
  144. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(fisher_f_distribution, lhs, rhs)
  145. { return lhs._impl_m == rhs._impl_m && lhs._impl_n == rhs._impl_n; }
  146. /**
  147. * Returns true if the two instances of @c fisher_f_distribution will
  148. * return different sequences of values given equal generators.
  149. */
  150. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(fisher_f_distribution)
  151. private:
  152. chi_squared_distribution<RealType> _impl_m;
  153. chi_squared_distribution<RealType> _impl_n;
  154. };
  155. } // namespace random
  156. } // namespace boost
  157. #endif // BOOST_RANDOM_EXTREME_VALUE_DISTRIBUTION_HPP