Chris@16: /* boost random/student_t_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_STUDENT_T_DISTRIBUTION_HPP Chris@16: #define BOOST_RANDOM_STUDENT_T_DISTRIBUTION_HPP Chris@16: Chris@16: #include 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 Student t distribution is a real valued distribution with one Chris@16: * parameter n, the number of degrees of freedom. Chris@16: * Chris@16: * It has \f$\displaystyle p(x) = Chris@16: * \frac{1}{\sqrt{n\pi}} Chris@16: * \frac{\Gamma((n+1)/2)}{\Gamma(n/2)} Chris@16: * \left(1+\frac{x^2}{n}\right)^{-(n+1)/2} Chris@16: * \f$. Chris@16: */ Chris@16: template Chris@16: class student_t_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 student_t_distribution distribution_type; Chris@16: Chris@16: /** Chris@16: * Constructs a @c param_type with "n" degrees of freedom. Chris@16: * Chris@16: * Requires: n > 0 Chris@16: */ Chris@16: explicit param_type(RealType n_arg = RealType(1.0)) Chris@16: : _n(n_arg) Chris@16: {} Chris@16: Chris@16: /** Returns the number of degrees of freedom 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._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._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._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 _n; Chris@16: }; Chris@16: Chris@16: /** Chris@16: * Constructs an @c student_t_distribution with "n" degrees of freedom. Chris@16: * Chris@16: * Requires: n > 0 Chris@16: */ Chris@16: explicit student_t_distribution(RealType n_arg = RealType(1.0)) Chris@16: : _normal(), _chi_squared(n_arg) Chris@16: {} Chris@16: /** Constructs an @c student_t_distribution from its parameters. */ Chris@16: explicit student_t_distribution(const param_type& parm) Chris@16: : _normal(), _chi_squared(parm.n()) Chris@16: {} Chris@16: Chris@16: /** Chris@16: * Returns a random variate distributed according to the Chris@16: * Student t distribution. Chris@16: */ Chris@16: template Chris@16: RealType operator()(URNG& urng) Chris@16: { Chris@16: using std::sqrt; Chris@16: return _normal(urng) / sqrt(_chi_squared(urng) / n()); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns a random variate distributed accordint to the Student Chris@16: * t 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 student_t_distribution(parm)(urng); Chris@16: } Chris@16: Chris@16: /** Returns the number of degrees of freedom. */ Chris@16: RealType n() const { return _chi_squared.n(); } Chris@16: Chris@16: /** Returns the smallest value that the distribution can produce. */ Chris@16: RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const Chris@16: { return -std::numeric_limits::infinity(); } 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(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 chi_squared_type; Chris@16: typename chi_squared_type::param_type chi_squared_param(parm.n()); Chris@16: _chi_squared.param(chi_squared_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: _normal.reset(); Chris@16: _chi_squared.reset(); Chris@16: } Chris@16: Chris@16: /** Writes a @c student_t_distribution to a @c std::ostream. */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, student_t_distribution, td) Chris@16: { Chris@16: os << td.param(); Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Reads a @c student_t_distribution from a @c std::istream. */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, student_t_distribution, td) Chris@16: { Chris@16: param_type parm; Chris@16: if(is >> parm) { Chris@16: td.param(parm); Chris@16: } Chris@16: return is; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns true if the two instances of @c student_t_distribution will Chris@16: * return identical sequences of values given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(student_t_distribution, lhs, rhs) Chris@16: { return lhs._normal == rhs._normal && lhs._chi_squared == rhs._chi_squared; } Chris@16: Chris@16: /** Chris@16: * Returns true if the two instances of @c student_t_distribution will Chris@16: * return different sequences of values given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(student_t_distribution) Chris@16: Chris@16: private: Chris@16: normal_distribution _normal; Chris@16: chi_squared_distribution _chi_squared; Chris@16: }; Chris@16: Chris@16: } // namespace random Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_RANDOM_STUDENT_T_DISTRIBUTION_HPP