| 1 | /* boost random/laplace_distribution.hpp header file |
| 2 | * |
| 3 | * Copyright Steven Watanabe 2014 |
| 4 | * Distributed under the Boost Software License, Version 1.0. (See |
| 5 | * accompanying file LICENSE_1_0.txt or copy at |
| 6 | * http://www.boost.org/LICENSE_1_0.txt) |
| 7 | * |
| 8 | * See http://www.boost.org for most recent version including documentation. |
| 9 | * |
| 10 | * $Id$ |
| 11 | */ |
| 12 | |
| 13 | #ifndef BOOST_RANDOM_LAPLACE_DISTRIBUTION_HPP |
| 14 | #define BOOST_RANDOM_LAPLACE_DISTRIBUTION_HPP |
| 15 | |
| 16 | #include <cassert> |
| 17 | #include <istream> |
| 18 | #include <iosfwd> |
| 19 | #include <boost/random/detail/operators.hpp> |
| 20 | #include <boost/random/exponential_distribution.hpp> |
| 21 | |
| 22 | namespace boost { |
| 23 | namespace random { |
| 24 | |
| 25 | /** |
| 26 | * The laplace distribution is a real-valued distribution with |
| 27 | * two parameters, mean and beta. |
| 28 | * |
| 29 | * It has \f$\displaystyle p(x) = \frac{e^-{\frac{|x-\mu|}{\beta}}}{2\beta}\f$. |
| 30 | */ |
| 31 | template<class RealType = double> |
| 32 | class laplace_distribution { |
| 33 | public: |
| 34 | typedef RealType result_type; |
| 35 | typedef RealType input_type; |
| 36 | |
| 37 | class param_type { |
| 38 | public: |
| 39 | typedef laplace_distribution distribution_type; |
| 40 | |
| 41 | /** |
| 42 | * Constructs a @c param_type from the "mean" and "beta" parameters |
| 43 | * of the distribution. |
| 44 | */ |
| 45 | explicit param_type(RealType mean_arg = RealType(0.0), |
| 46 | RealType beta_arg = RealType(1.0)) |
| 47 | : _mean(mean_arg), _beta(beta_arg) |
| 48 | {} |
| 49 | |
| 50 | /** Returns the "mean" parameter of the distribtuion. */ |
| 51 | RealType mean() const { return _mean; } |
| 52 | /** Returns the "beta" parameter of the distribution. */ |
| 53 | RealType beta() const { return _beta; } |
| 54 | |
| 55 | /** Writes a @c param_type to a @c std::ostream. */ |
| 56 | BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm) |
| 57 | { os << parm._mean << ' ' << parm._beta; return os; } |
| 58 | |
| 59 | /** Reads a @c param_type from a @c std::istream. */ |
| 60 | BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm) |
| 61 | { is >> parm._mean >> std::ws >> parm._beta; return is; } |
| 62 | |
| 63 | /** Returns true if the two sets of parameters are the same. */ |
| 64 | BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs) |
| 65 | { return lhs._mean == rhs._mean && lhs._beta == rhs._beta; } |
| 66 | |
| 67 | /** Returns true if the two sets of parameters are the different. */ |
| 68 | BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type) |
| 69 | |
| 70 | private: |
| 71 | RealType _mean; |
| 72 | RealType _beta; |
| 73 | }; |
| 74 | |
| 75 | /** |
| 76 | * Constructs an @c laplace_distribution from its "mean" and "beta" parameters. |
| 77 | */ |
| 78 | explicit laplace_distribution(RealType mean_arg = RealType(0.0), |
| 79 | RealType beta_arg = RealType(1.0)) |
| 80 | : _mean(mean_arg), _beta(beta_arg) |
| 81 | {} |
| 82 | /** Constructs an @c laplace_distribution from its parameters. */ |
| 83 | explicit laplace_distribution(const param_type& parm) |
| 84 | : _mean(parm.mean()), _beta(parm.beta()) |
| 85 | {} |
| 86 | |
| 87 | /** |
| 88 | * Returns a random variate distributed according to the |
| 89 | * laplace distribution. |
| 90 | */ |
| 91 | template<class URNG> |
| 92 | RealType operator()(URNG& urng) const |
| 93 | { |
| 94 | RealType exponential = exponential_distribution<RealType>()(urng); |
| 95 | if(uniform_01<RealType>()(urng) < 0.5) |
| 96 | exponential = -exponential; |
| 97 | return _mean + _beta * exponential; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Returns a random variate distributed accordint to the laplace |
| 102 | * distribution with parameters specified by @c param. |
| 103 | */ |
| 104 | template<class URNG> |
| 105 | RealType operator()(URNG& urng, const param_type& parm) const |
| 106 | { |
| 107 | return laplace_distribution(parm)(urng); |
| 108 | } |
| 109 | |
| 110 | /** Returns the "mean" parameter of the distribution. */ |
| 111 | RealType mean() const { return _mean; } |
| 112 | /** Returns the "beta" parameter of the distribution. */ |
| 113 | RealType beta() const { return _beta; } |
| 114 | |
| 115 | /** Returns the smallest value that the distribution can produce. */ |
| 116 | RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const |
| 117 | { return RealType(-std::numeric_limits<RealType>::infinity()); } |
| 118 | /** Returns the largest value that the distribution can produce. */ |
| 119 | RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const |
| 120 | { return RealType(std::numeric_limits<RealType>::infinity()); } |
| 121 | |
| 122 | /** Returns the parameters of the distribution. */ |
| 123 | param_type param() const { return param_type(_mean, _beta); } |
| 124 | /** Sets the parameters of the distribution. */ |
| 125 | void param(const param_type& parm) |
| 126 | { |
| 127 | _mean = parm.mean(); |
| 128 | _beta = parm.beta(); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Effects: Subsequent uses of the distribution do not depend |
| 133 | * on values produced by any engine prior to invoking reset. |
| 134 | */ |
| 135 | void reset() { } |
| 136 | |
| 137 | /** Writes an @c laplace_distribution to a @c std::ostream. */ |
| 138 | BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, laplace_distribution, wd) |
| 139 | { |
| 140 | os << wd.param(); |
| 141 | return os; |
| 142 | } |
| 143 | |
| 144 | /** Reads an @c laplace_distribution from a @c std::istream. */ |
| 145 | BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, laplace_distribution, wd) |
| 146 | { |
| 147 | param_type parm; |
| 148 | if(is >> parm) { |
| 149 | wd.param(parm); |
| 150 | } |
| 151 | return is; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Returns true if the two instances of @c laplace_distribution will |
| 156 | * return identical sequences of values given equal generators. |
| 157 | */ |
| 158 | BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(laplace_distribution, lhs, rhs) |
| 159 | { return lhs._mean == rhs._mean && lhs._beta == rhs._beta; } |
| 160 | |
| 161 | /** |
| 162 | * Returns true if the two instances of @c laplace_distribution will |
| 163 | * return different sequences of values given equal generators. |
| 164 | */ |
| 165 | BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(laplace_distribution) |
| 166 | |
| 167 | private: |
| 168 | RealType _mean; |
| 169 | RealType _beta; |
| 170 | }; |
| 171 | |
| 172 | } // namespace random |
| 173 | } // namespace boost |
| 174 | |
| 175 | #endif // BOOST_RANDOM_LAPLACE_DISTRIBUTION_HPP |
| 176 | |