Chris@16: /* boost random/chi_squared_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_CHI_SQUARED_DISTRIBUTION_HPP_INCLUDED Chris@16: #define BOOST_RANDOM_CHI_SQUARED_DISTRIBUTION_HPP_INCLUDED Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace random { Chris@16: Chris@16: /** Chris@16: * The chi squared distribution is a real valued distribution with Chris@16: * one parameter, @c n. The distribution produces values > 0. Chris@16: * Chris@16: * The distribution function is Chris@16: * \f$\displaystyle P(x) = \frac{x^{(n/2)-1}e^{-x/2}}{\Gamma(n/2)2^{n/2}}\f$. Chris@16: */ Chris@16: template Chris@16: class chi_squared_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 chi_squared_distribution distribution_type; Chris@16: /** Chris@16: * Construct a param_type object. @c n Chris@16: * is the parameter of the distribution. Chris@16: * Chris@16: * Requires: t >=0 && 0 <= p <= 1 Chris@16: */ Chris@16: explicit param_type(RealType n_arg = RealType(1)) Chris@16: : _n(n_arg) Chris@16: {} Chris@16: /** Returns the @c n parameter of the distribution. */ Chris@16: RealType n() const { return _n; } Chris@16: #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS Chris@16: /** Writes the parameters of the distribution to a @c std::ostream. */ Chris@16: template Chris@16: friend std::basic_ostream& Chris@16: operator<<(std::basic_ostream& os, Chris@16: const param_type& parm) Chris@16: { Chris@16: os << parm._n; Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Reads the parameters of the distribution from a @c std::istream. */ Chris@16: template Chris@16: friend std::basic_istream& Chris@16: operator>>(std::basic_istream& is, param_type& parm) Chris@16: { Chris@16: is >> parm._n; Chris@16: return is; Chris@16: } Chris@16: #endif Chris@16: /** Returns true if the parameters have the same values. */ Chris@16: friend bool operator==(const param_type& lhs, const param_type& rhs) Chris@16: { Chris@16: return lhs._n == rhs._n; Chris@16: } Chris@16: /** Returns true if the parameters have different values. */ Chris@16: friend bool operator!=(const param_type& lhs, const param_type& rhs) Chris@16: { Chris@16: return !(lhs == rhs); Chris@16: } Chris@16: private: Chris@16: RealType _n; Chris@16: }; Chris@16: Chris@16: /** Chris@16: * Construct a @c chi_squared_distribution object. @c n Chris@16: * is the parameter of the distribution. Chris@16: * Chris@16: * Requires: t >=0 && 0 <= p <= 1 Chris@16: */ Chris@16: explicit chi_squared_distribution(RealType n_arg = RealType(1)) Chris@16: : _impl(n_arg / 2) Chris@16: { Chris@16: } Chris@16: Chris@16: /** Chris@16: * Construct an @c chi_squared_distribution object from the Chris@16: * parameters. Chris@16: */ Chris@16: explicit chi_squared_distribution(const param_type& parm) Chris@16: : _impl(parm.n() / 2) Chris@16: { Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns a random variate distributed according to the Chris@16: * chi squared distribution. Chris@16: */ Chris@16: template Chris@16: RealType operator()(URNG& urng) Chris@16: { Chris@16: return 2 * _impl(urng); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns a random variate distributed according to the Chris@16: * chi squared 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 chi_squared_distribution(parm)(urng); Chris@16: } Chris@16: Chris@16: /** Returns the @c n parameter of the distribution. */ Chris@16: RealType n() const { return 2 * _impl.alpha(); } 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(n()); } Chris@16: /** Sets parameters of the distribution. */ Chris@16: void param(const param_type& parm) Chris@16: { Chris@16: typedef gamma_distribution impl_type; Chris@16: typename impl_type::param_type impl_parm(parm.n() / 2); Chris@16: _impl.param(impl_parm); 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() { _impl.reset(); } Chris@16: Chris@16: #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS Chris@16: /** Writes the parameters of the distribution to a @c std::ostream. */ Chris@16: template Chris@16: friend std::basic_ostream& Chris@16: operator<<(std::basic_ostream& os, Chris@16: const chi_squared_distribution& c2d) Chris@16: { Chris@16: os << c2d.param(); Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Reads the parameters of the distribution from a @c std::istream. */ Chris@16: template Chris@16: friend std::basic_istream& Chris@16: operator>>(std::basic_istream& is, Chris@16: chi_squared_distribution& c2d) Chris@16: { Chris@16: c2d.read(is); Chris@16: return is; Chris@16: } Chris@16: #endif Chris@16: Chris@16: /** Returns true if the two distributions will produce the same Chris@16: sequence of values, given equal generators. */ Chris@16: friend bool operator==(const chi_squared_distribution& lhs, Chris@16: const chi_squared_distribution& rhs) Chris@16: { Chris@16: return lhs._impl == rhs._impl; Chris@16: } Chris@16: /** Returns true if the two distributions could produce different Chris@16: sequences of values, given equal generators. */ Chris@16: friend bool operator!=(const chi_squared_distribution& lhs, Chris@16: const chi_squared_distribution& rhs) Chris@16: { Chris@16: return !(lhs == rhs); Chris@16: } Chris@16: Chris@16: private: Chris@16: Chris@16: /// @cond show_private Chris@16: Chris@16: template Chris@16: void read(std::basic_istream& is) { Chris@16: param_type parm; Chris@16: if(is >> parm) { Chris@16: param(parm); Chris@16: } Chris@16: } Chris@16: Chris@16: gamma_distribution _impl; Chris@16: Chris@16: /// @endcond Chris@16: }; Chris@16: Chris@16: } Chris@16: Chris@16: } Chris@16: Chris@16: #endif