1 /* boost random/laplace_distribution.hpp header file
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)
8 * See http://www.boost.org for most recent version including documentation.
13 #ifndef BOOST_RANDOM_LAPLACE_DISTRIBUTION_HPP
14 #define BOOST_RANDOM_LAPLACE_DISTRIBUTION_HPP
19 #include <boost/random/detail/operators.hpp>
20 #include <boost/random/exponential_distribution.hpp>
26 * The laplace distribution is a real-valued distribution with
27 * two parameters, mean and beta.
29 * It has \f$\displaystyle p(x) = \frac{e^-{\frac{|x-\mu|}{\beta}}}{2\beta}\f$.
31 template<class RealType = double>
32 class laplace_distribution {
34 typedef RealType result_type;
35 typedef RealType input_type;
39 typedef laplace_distribution distribution_type;
42 * Constructs a @c param_type from the "mean" and "beta" parameters
43 * of the distribution.
45 explicit param_type(RealType mean_arg = RealType(0.0),
46 RealType beta_arg = RealType(1.0))
47 : _mean(mean_arg), _beta(beta_arg)
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; }
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; }
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; }
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; }
67 /** Returns true if the two sets of parameters are the different. */
68 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
76 * Constructs an @c laplace_distribution from its "mean" and "beta" parameters.
78 explicit laplace_distribution(RealType mean_arg = RealType(0.0),
79 RealType beta_arg = RealType(1.0))
80 : _mean(mean_arg), _beta(beta_arg)
82 /** Constructs an @c laplace_distribution from its parameters. */
83 explicit laplace_distribution(const param_type& parm)
84 : _mean(parm.mean()), _beta(parm.beta())
88 * Returns a random variate distributed according to the
89 * laplace distribution.
92 RealType operator()(URNG& urng) const
94 RealType exponential = exponential_distribution<RealType>()(urng);
95 if(uniform_01<RealType>()(urng) < 0.5)
96 exponential = -exponential;
97 return _mean + _beta * exponential;
101 * Returns a random variate distributed accordint to the laplace
102 * distribution with parameters specified by @c param.
105 RealType operator()(URNG& urng, const param_type& parm) const
107 return laplace_distribution(parm)(urng);
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; }
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()); }
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)
132 * Effects: Subsequent uses of the distribution do not depend
133 * on values produced by any engine prior to invoking reset.
137 /** Writes an @c laplace_distribution to a @c std::ostream. */
138 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, laplace_distribution, wd)
144 /** Reads an @c laplace_distribution from a @c std::istream. */
145 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, laplace_distribution, wd)
155 * Returns true if the two instances of @c laplace_distribution will
156 * return identical sequences of values given equal generators.
158 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(laplace_distribution, lhs, rhs)
159 { return lhs._mean == rhs._mean && lhs._beta == rhs._beta; }
162 * Returns true if the two instances of @c laplace_distribution will
163 * return different sequences of values given equal generators.
165 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(laplace_distribution)
172 } // namespace random
175 #endif // BOOST_RANDOM_LAPLACE_DISTRIBUTION_HPP