laplace_distribution.hpp 5.6 KB

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