Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:51:10

0001 /* boost random/chi_squared_distribution.hpp header file
0002  *
0003  * Copyright Steven Watanabe 2011
0004  * Distributed under the Boost Software License, Version 1.0. (See
0005  * accompanying file LICENSE_1_0.txt or copy at
0006  * http://www.boost.org/LICENSE_1_0.txt)
0007  *
0008  * See http://www.boost.org for most recent version including documentation.
0009  *
0010  * $Id$
0011  */
0012 
0013 #ifndef BOOST_RANDOM_CHI_SQUARED_DISTRIBUTION_HPP_INCLUDED
0014 #define BOOST_RANDOM_CHI_SQUARED_DISTRIBUTION_HPP_INCLUDED
0015 
0016 #include <iosfwd>
0017 #include <boost/limits.hpp>
0018 
0019 #include <boost/random/detail/config.hpp>
0020 #include <boost/random/gamma_distribution.hpp>
0021 
0022 namespace boost {
0023 namespace random {
0024 
0025 /**
0026  * The chi squared distribution is a real valued distribution with
0027  * one parameter, @c n.  The distribution produces values > 0.
0028  *
0029  * The distribution function is
0030  * \f$\displaystyle P(x) = \frac{x^{(n/2)-1}e^{-x/2}}{\Gamma(n/2)2^{n/2}}\f$.
0031  */
0032 template<class RealType = double>
0033 class chi_squared_distribution {
0034 public:
0035     typedef RealType result_type;
0036     typedef RealType input_type;
0037 
0038     class param_type {
0039     public:
0040         typedef chi_squared_distribution distribution_type;
0041         /**
0042          * Construct a param_type object.  @c n
0043          * is the parameter of the distribution.
0044          *
0045          * Requires: t >=0 && 0 <= p <= 1
0046          */
0047         explicit param_type(RealType n_arg = RealType(1))
0048           : _n(n_arg)
0049         {}
0050         /** Returns the @c n parameter of the distribution. */
0051         RealType n() const { return _n; }
0052 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
0053         /** Writes the parameters of the distribution to a @c std::ostream. */
0054         template<class CharT, class Traits>
0055         friend std::basic_ostream<CharT,Traits>&
0056         operator<<(std::basic_ostream<CharT,Traits>& os,
0057                    const param_type& parm)
0058         {
0059             os << parm._n;
0060             return os;
0061         }
0062     
0063         /** Reads the parameters of the distribution from a @c std::istream. */
0064         template<class CharT, class Traits>
0065         friend std::basic_istream<CharT,Traits>&
0066         operator>>(std::basic_istream<CharT,Traits>& is, param_type& parm)
0067         {
0068             is >> parm._n;
0069             return is;
0070         }
0071 #endif
0072         /** Returns true if the parameters have the same values. */
0073         friend bool operator==(const param_type& lhs, const param_type& rhs)
0074         {
0075             return lhs._n == rhs._n;
0076         }
0077         /** Returns true if the parameters have different values. */
0078         friend bool operator!=(const param_type& lhs, const param_type& rhs)
0079         {
0080             return !(lhs == rhs);
0081         }
0082     private:
0083         RealType _n;
0084     };
0085     
0086     /**
0087      * Construct a @c chi_squared_distribution object. @c n
0088      * is the parameter of the distribution.
0089      *
0090      * Requires: t >=0 && 0 <= p <= 1
0091      */
0092     explicit chi_squared_distribution(RealType n_arg = RealType(1))
0093       : _impl(static_cast<RealType>(n_arg / 2))
0094     {
0095     }
0096     
0097     /**
0098      * Construct an @c chi_squared_distribution object from the
0099      * parameters.
0100      */
0101     explicit chi_squared_distribution(const param_type& parm)
0102       : _impl(static_cast<RealType>(parm.n() / 2))
0103     {
0104     }
0105     
0106     /**
0107      * Returns a random variate distributed according to the
0108      * chi squared distribution.
0109      */
0110     template<class URNG>
0111     RealType operator()(URNG& urng)
0112     {
0113         return 2 * _impl(urng);
0114     }
0115     
0116     /**
0117      * Returns a random variate distributed according to the
0118      * chi squared distribution with parameters specified by @c param.
0119      */
0120     template<class URNG>
0121     RealType operator()(URNG& urng, const param_type& parm) const
0122     {
0123         return chi_squared_distribution(parm)(urng);
0124     }
0125 
0126     /** Returns the @c n parameter of the distribution. */
0127     RealType n() const { return 2 * _impl.alpha(); }
0128 
0129     /** Returns the smallest value that the distribution can produce. */
0130     RealType min BOOST_PREVENT_MACRO_SUBSTITUTION() const { return 0; }
0131     /** Returns the largest value that the distribution can produce. */
0132     RealType max BOOST_PREVENT_MACRO_SUBSTITUTION() const
0133     { return (std::numeric_limits<RealType>::infinity)(); }
0134 
0135     /** Returns the parameters of the distribution. */
0136     param_type param() const { return param_type(n()); }
0137     /** Sets parameters of the distribution. */
0138     void param(const param_type& parm)
0139     {
0140         typedef gamma_distribution<RealType> impl_type;
0141         typename impl_type::param_type impl_parm(static_cast<RealType>(parm.n() / 2));
0142         _impl.param(impl_parm);
0143     }
0144 
0145     /**
0146      * Effects: Subsequent uses of the distribution do not depend
0147      * on values produced by any engine prior to invoking reset.
0148      */
0149     void reset() { _impl.reset(); }
0150 
0151 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
0152     /** Writes the parameters of the distribution to a @c std::ostream. */
0153     template<class CharT, class Traits>
0154     friend std::basic_ostream<CharT,Traits>&
0155     operator<<(std::basic_ostream<CharT,Traits>& os,
0156                const chi_squared_distribution& c2d)
0157     {
0158         os << c2d.param();
0159         return os;
0160     }
0161     
0162     /** Reads the parameters of the distribution from a @c std::istream. */
0163     template<class CharT, class Traits>
0164     friend std::basic_istream<CharT,Traits>&
0165     operator>>(std::basic_istream<CharT,Traits>& is,
0166                chi_squared_distribution& c2d)
0167     {
0168         c2d.read(is);
0169         return is;
0170     }
0171 #endif
0172 
0173     /** Returns true if the two distributions will produce the same
0174         sequence of values, given equal generators. */
0175     friend bool operator==(const chi_squared_distribution& lhs,
0176                            const chi_squared_distribution& rhs)
0177     {
0178         return lhs._impl == rhs._impl;
0179     }
0180     /** Returns true if the two distributions could produce different
0181         sequences of values, given equal generators. */
0182     friend bool operator!=(const chi_squared_distribution& lhs,
0183                            const chi_squared_distribution& rhs)
0184     {
0185         return !(lhs == rhs);
0186     }
0187 
0188 private:
0189 
0190     /// @cond show_private
0191 
0192     template<class CharT, class Traits>
0193     void read(std::basic_istream<CharT, Traits>& is) {
0194         param_type parm;
0195         if(is >> parm) {
0196             param(parm);
0197         }
0198     }
0199 
0200     gamma_distribution<RealType> _impl;
0201 
0202     /// @endcond
0203 };
0204 
0205 }
0206 
0207 }
0208 
0209 #endif