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