Chris@16: /* boost random/fisher_f_distribution.hpp header file Chris@16: * Chris@16: * Copyright Steven Watanabe 2011 Chris@16: * Distributed under the Boost Software License, Version 1.0. (See Chris@16: * accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Chris@16: * See http://www.boost.org for most recent version including documentation. Chris@16: * Chris@101: * $Id$ Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_RANDOM_FISHER_F_DISTRIBUTION_HPP Chris@16: #define BOOST_RANDOM_FISHER_F_DISTRIBUTION_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace random { Chris@16: Chris@16: /** Chris@16: * The Fisher F distribution is a real valued distribution with two Chris@16: * parameters m and n. Chris@16: * Chris@16: * It has \f$\displaystyle p(x) = Chris@16: * \frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)} Chris@16: * \left(\frac{m}{n}\right)^{m/2} Chris@16: * x^{(m/2)-1} \left(1+\frac{mx}{n}\right)^{-(m+n)/2} Chris@16: * \f$. Chris@16: */ Chris@16: template Chris@16: class fisher_f_distribution { Chris@16: public: Chris@16: typedef RealType result_type; Chris@16: typedef RealType input_type; Chris@16: Chris@16: class param_type { Chris@16: public: Chris@16: typedef fisher_f_distribution distribution_type; Chris@16: Chris@16: /** Chris@16: * Constructs a @c param_type from the "m" and "n" parameters Chris@16: * of the distribution. Chris@16: * Chris@16: * Requires: m > 0 and n > 0 Chris@16: */ Chris@16: explicit param_type(RealType m_arg = RealType(1.0), Chris@16: RealType n_arg = RealType(1.0)) Chris@16: : _m(m_arg), _n(n_arg) Chris@16: {} Chris@16: Chris@16: /** Returns the "m" parameter of the distribtuion. */ Chris@16: RealType m() const { return _m; } Chris@16: /** Returns the "n" parameter of the distribution. */ Chris@16: RealType n() const { return _n; } Chris@16: Chris@16: /** Writes a @c param_type to a @c std::ostream. */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm) Chris@16: { os << parm._m << ' ' << parm._n; return os; } Chris@16: Chris@16: /** Reads a @c param_type from a @c std::istream. */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm) Chris@16: { is >> parm._m >> std::ws >> parm._n; return is; } Chris@16: Chris@16: /** Returns true if the two sets of parameters are the same. */ Chris@16: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs) Chris@16: { return lhs._m == rhs._m && lhs._n == rhs._n; } Chris@16: Chris@16: /** Returns true if the two sets of parameters are the different. */ Chris@16: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type) Chris@16: Chris@16: private: Chris@16: RealType _m; Chris@16: RealType _n; Chris@16: }; Chris@16: Chris@16: /** Chris@16: * Constructs a @c fisher_f_distribution from its "m" and "n" parameters. Chris@16: * Chris@16: * Requires: m > 0 and n > 0 Chris@16: */ Chris@16: explicit fisher_f_distribution(RealType m_arg = RealType(1.0), Chris@16: RealType n_arg = RealType(1.0)) Chris@16: : _impl_m(m_arg), _impl_n(n_arg) Chris@16: {} Chris@16: /** Constructs an @c fisher_f_distribution from its parameters. */ Chris@16: explicit fisher_f_distribution(const param_type& parm) Chris@16: : _impl_m(parm.m()), _impl_n(parm.n()) Chris@16: {} Chris@16: Chris@16: /** Chris@16: * Returns a random variate distributed according to the Chris@16: * F distribution. Chris@16: */ Chris@16: template Chris@16: RealType operator()(URNG& urng) Chris@16: { Chris@16: return (_impl_m(urng) * n()) / (_impl_n(urng) * m()); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns a random variate distributed according to the Chris@16: * F distribution with parameters specified by @c param. Chris@16: */ Chris@16: template Chris@16: RealType operator()(URNG& urng, const param_type& parm) const Chris@16: { Chris@16: return fisher_f_distribution(parm)(urng); Chris@16: } Chris@16: Chris@16: /** Returns the "m" parameter of the distribution. */ Chris@16: RealType m() const { return _impl_m.n(); } Chris@16: /** Returns the "n" parameter of the distribution. */ Chris@16: RealType n() const { return _impl_n.n(); } Chris@16: Chris@16: /** Returns the smallest value that the distribution can produce. */ Chris@16: RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; } Chris@16: /** Returns the largest value that the distribution can produce. */ Chris@16: RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const Chris@16: { return std::numeric_limits::infinity(); } Chris@16: Chris@16: /** Returns the parameters of the distribution. */ Chris@16: param_type param() const { return param_type(m(), n()); } Chris@16: /** Sets the parameters of the distribution. */ Chris@16: void param(const param_type& parm) Chris@16: { Chris@16: typedef chi_squared_distribution impl_type; Chris@16: typename impl_type::param_type m_param(parm.m()); Chris@16: _impl_m.param(m_param); Chris@16: typename impl_type::param_type n_param(parm.n()); Chris@16: _impl_n.param(n_param); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Effects: Subsequent uses of the distribution do not depend Chris@16: * on values produced by any engine prior to invoking reset. Chris@16: */ Chris@16: void reset() { } Chris@16: Chris@16: /** Writes an @c fisher_f_distribution to a @c std::ostream. */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, fisher_f_distribution, fd) Chris@16: { Chris@16: os << fd.param(); Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Reads an @c fisher_f_distribution from a @c std::istream. */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, fisher_f_distribution, fd) Chris@16: { Chris@16: param_type parm; Chris@16: if(is >> parm) { Chris@16: fd.param(parm); Chris@16: } Chris@16: return is; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns true if the two instances of @c fisher_f_distribution will Chris@16: * return identical sequences of values given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(fisher_f_distribution, lhs, rhs) Chris@16: { return lhs._impl_m == rhs._impl_m && lhs._impl_n == rhs._impl_n; } Chris@16: Chris@16: /** Chris@16: * Returns true if the two instances of @c fisher_f_distribution will Chris@16: * return different sequences of values given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(fisher_f_distribution) Chris@16: Chris@16: private: Chris@16: chi_squared_distribution _impl_m; Chris@16: chi_squared_distribution _impl_n; Chris@16: }; Chris@16: Chris@16: } // namespace random Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_RANDOM_EXTREME_VALUE_DISTRIBUTION_HPP