Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /* boost random/extreme_value_distribution.hpp header file
0002  *
0003  * Copyright Steven Watanabe 2010
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_EXTREME_VALUE_DISTRIBUTION_HPP
0014 #define BOOST_RANDOM_EXTREME_VALUE_DISTRIBUTION_HPP
0015 
0016 #include <boost/config/no_tr1/cmath.hpp>
0017 #include <iosfwd>
0018 #include <istream>
0019 #include <boost/config.hpp>
0020 #include <boost/limits.hpp>
0021 #include <boost/random/detail/operators.hpp>
0022 #include <boost/random/uniform_01.hpp>
0023 
0024 namespace boost {
0025 namespace random {
0026 
0027 /**
0028  * The extreme value distribution is a real valued distribution with two
0029  * parameters a and b.
0030  *
0031  * It has \f$\displaystyle p(x) = \frac{1}{b}e^{\frac{a-x}{b} - e^\frac{a-x}{b}}\f$.
0032  */
0033 template<class RealType = double>
0034 class extreme_value_distribution {
0035 public:
0036     typedef RealType result_type;
0037     typedef RealType input_type;
0038 
0039     class param_type {
0040     public:
0041         typedef extreme_value_distribution distribution_type;
0042 
0043         /**
0044          * Constructs a @c param_type from the "a" and "b" parameters
0045          * of the distribution.
0046          *
0047          * Requires: b > 0
0048          */
0049         explicit param_type(RealType a_arg = 1.0, RealType b_arg = 1.0)
0050           : _a(a_arg), _b(b_arg)
0051         {}
0052 
0053         /** Returns the "a" parameter of the distribtuion. */
0054         RealType a() const { return _a; }
0055         /** Returns the "b" parameter of the distribution. */
0056         RealType b() const { return _b; }
0057 
0058         /** Writes a @c param_type to a @c std::ostream. */
0059         BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
0060         { os << parm._a << ' ' << parm._b; return os; }
0061 
0062         /** Reads a @c param_type from a @c std::istream. */
0063         BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
0064         { is >> parm._a >> std::ws >> parm._b; return is; }
0065 
0066         /** Returns true if the two sets of parameters are the same. */
0067         BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
0068         { return lhs._a == rhs._a && lhs._b == rhs._b; }
0069         
0070         /** Returns true if the two sets of parameters are the different. */
0071         BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
0072 
0073     private:
0074         RealType _a;
0075         RealType _b;
0076     };
0077 
0078     /**
0079      * Constructs an @c extreme_value_distribution from its "a" and "b" parameters.
0080      *
0081      * Requires: b > 0
0082      */
0083     explicit extreme_value_distribution(RealType a_arg = 1.0, RealType b_arg = 1.0)
0084       : _a(a_arg), _b(b_arg)
0085     {}
0086     /** Constructs an @c extreme_value_distribution from its parameters. */
0087     explicit extreme_value_distribution(const param_type& parm)
0088       : _a(parm.a()), _b(parm.b())
0089     {}
0090 
0091     /**
0092      * Returns a random variate distributed according to the
0093      * @c extreme_value_distribution.
0094      */
0095     template<class URNG>
0096     RealType operator()(URNG& urng) const
0097     {
0098         using std::log;
0099         return _a - log(-log(uniform_01<RealType>()(urng))) * _b;
0100     }
0101 
0102     /**
0103      * Returns a random variate distributed accordint to the extreme
0104      * value distribution with parameters specified by @c param.
0105      */
0106     template<class URNG>
0107     RealType operator()(URNG& urng, const param_type& parm) const
0108     {
0109         return extreme_value_distribution(parm)(urng);
0110     }
0111 
0112     /** Returns the "a" parameter of the distribution. */
0113     RealType a() const { return _a; }
0114     /** Returns the "b" parameter of the distribution. */
0115     RealType b() const { return _b; }
0116 
0117     /** Returns the smallest value that the distribution can produce. */
0118     RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
0119     { return -std::numeric_limits<RealType>::infinity(); }
0120     /** Returns the largest value that the distribution can produce. */
0121     RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
0122     { return std::numeric_limits<RealType>::infinity(); }
0123 
0124     /** Returns the parameters of the distribution. */
0125     param_type param() const { return param_type(_a, _b); }
0126     /** Sets the parameters of the distribution. */
0127     void param(const param_type& parm)
0128     {
0129         _a = parm.a();
0130         _b = parm.b();
0131     }
0132 
0133     /**
0134      * Effects: Subsequent uses of the distribution do not depend
0135      * on values produced by any engine prior to invoking reset.
0136      */
0137     void reset() { }
0138 
0139     /** Writes an @c extreme_value_distribution to a @c std::ostream. */
0140     BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, extreme_value_distribution, wd)
0141     {
0142         os << wd.param();
0143         return os;
0144     }
0145 
0146     /** Reads an @c extreme_value_distribution from a @c std::istream. */
0147     BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, extreme_value_distribution, wd)
0148     {
0149         param_type parm;
0150         if(is >> parm) {
0151             wd.param(parm);
0152         }
0153         return is;
0154     }
0155 
0156     /**
0157      * Returns true if the two instances of @c extreme_value_distribution will
0158      * return identical sequences of values given equal generators.
0159      */
0160     BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(extreme_value_distribution, lhs, rhs)
0161     { return lhs._a == rhs._a && lhs._b == rhs._b; }
0162     
0163     /**
0164      * Returns true if the two instances of @c extreme_value_distribution will
0165      * return different sequences of values given equal generators.
0166      */
0167     BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(extreme_value_distribution)
0168 
0169 private:
0170     RealType _a;
0171     RealType _b;
0172 };
0173 
0174 } // namespace random
0175 } // namespace boost
0176 
0177 #endif // BOOST_RANDOM_EXTREME_VALUE_DISTRIBUTION_HPP