Back to home page

EIC code displayed by LXR

 
 

    


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

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