Chris@102: /* boost random/laplace_distribution.hpp header file Chris@102: * Chris@102: * Copyright Steven Watanabe 2014 Chris@102: * Distributed under the Boost Software License, Version 1.0. (See Chris@102: * accompanying file LICENSE_1_0.txt or copy at Chris@102: * http://www.boost.org/LICENSE_1_0.txt) Chris@102: * Chris@102: * See http://www.boost.org for most recent version including documentation. Chris@102: * Chris@102: * $Id$ Chris@102: */ Chris@102: Chris@102: #ifndef BOOST_RANDOM_LAPLACE_DISTRIBUTION_HPP Chris@102: #define BOOST_RANDOM_LAPLACE_DISTRIBUTION_HPP Chris@102: Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: Chris@102: namespace boost { Chris@102: namespace random { Chris@102: Chris@102: /** Chris@102: * The laplace distribution is a real-valued distribution with Chris@102: * two parameters, mean and beta. Chris@102: * Chris@102: * It has \f$\displaystyle p(x) = \frac{e^-{\frac{|x-\mu|}{\beta}}}{2\beta}\f$. Chris@102: */ Chris@102: template Chris@102: class laplace_distribution { Chris@102: public: Chris@102: typedef RealType result_type; Chris@102: typedef RealType input_type; Chris@102: Chris@102: class param_type { Chris@102: public: Chris@102: typedef laplace_distribution distribution_type; Chris@102: Chris@102: /** Chris@102: * Constructs a @c param_type from the "mean" and "beta" parameters Chris@102: * of the distribution. Chris@102: */ Chris@102: explicit param_type(RealType mean_arg = RealType(0.0), Chris@102: RealType beta_arg = RealType(1.0)) Chris@102: : _mean(mean_arg), _beta(beta_arg) Chris@102: {} Chris@102: Chris@102: /** Returns the "mean" parameter of the distribtuion. */ Chris@102: RealType mean() const { return _mean; } Chris@102: /** Returns the "beta" parameter of the distribution. */ Chris@102: RealType beta() const { return _beta; } Chris@102: Chris@102: /** Writes a @c param_type to a @c std::ostream. */ Chris@102: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm) Chris@102: { os << parm._mean << ' ' << parm._beta; return os; } Chris@102: Chris@102: /** Reads a @c param_type from a @c std::istream. */ Chris@102: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm) Chris@102: { is >> parm._mean >> std::ws >> parm._beta; return is; } Chris@102: Chris@102: /** Returns true if the two sets of parameters are the same. */ Chris@102: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs) Chris@102: { return lhs._mean == rhs._mean && lhs._beta == rhs._beta; } Chris@102: Chris@102: /** Returns true if the two sets of parameters are the different. */ Chris@102: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type) Chris@102: Chris@102: private: Chris@102: RealType _mean; Chris@102: RealType _beta; Chris@102: }; Chris@102: Chris@102: /** Chris@102: * Constructs an @c laplace_distribution from its "mean" and "beta" parameters. Chris@102: */ Chris@102: explicit laplace_distribution(RealType mean_arg = RealType(0.0), Chris@102: RealType beta_arg = RealType(1.0)) Chris@102: : _mean(mean_arg), _beta(beta_arg) Chris@102: {} Chris@102: /** Constructs an @c laplace_distribution from its parameters. */ Chris@102: explicit laplace_distribution(const param_type& parm) Chris@102: : _mean(parm.mean()), _beta(parm.beta()) Chris@102: {} Chris@102: Chris@102: /** Chris@102: * Returns a random variate distributed according to the Chris@102: * laplace distribution. Chris@102: */ Chris@102: template Chris@102: RealType operator()(URNG& urng) const Chris@102: { Chris@102: RealType exponential = exponential_distribution()(urng); Chris@102: if(uniform_01()(urng) < 0.5) Chris@102: exponential = -exponential; Chris@102: return _mean + _beta * exponential; Chris@102: } Chris@102: Chris@102: /** Chris@102: * Returns a random variate distributed accordint to the laplace Chris@102: * distribution with parameters specified by @c param. Chris@102: */ Chris@102: template Chris@102: RealType operator()(URNG& urng, const param_type& parm) const Chris@102: { Chris@102: return laplace_distribution(parm)(urng); Chris@102: } Chris@102: Chris@102: /** Returns the "mean" parameter of the distribution. */ Chris@102: RealType mean() const { return _mean; } Chris@102: /** Returns the "beta" parameter of the distribution. */ Chris@102: RealType beta() const { return _beta; } Chris@102: Chris@102: /** Returns the smallest value that the distribution can produce. */ Chris@102: RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const Chris@102: { return RealType(-std::numeric_limits::infinity()); } Chris@102: /** Returns the largest value that the distribution can produce. */ Chris@102: RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const Chris@102: { return RealType(std::numeric_limits::infinity()); } Chris@102: Chris@102: /** Returns the parameters of the distribution. */ Chris@102: param_type param() const { return param_type(_mean, _beta); } Chris@102: /** Sets the parameters of the distribution. */ Chris@102: void param(const param_type& parm) Chris@102: { Chris@102: _mean = parm.mean(); Chris@102: _beta = parm.beta(); Chris@102: } Chris@102: Chris@102: /** Chris@102: * Effects: Subsequent uses of the distribution do not depend Chris@102: * on values produced by any engine prior to invoking reset. Chris@102: */ Chris@102: void reset() { } Chris@102: Chris@102: /** Writes an @c laplace_distribution to a @c std::ostream. */ Chris@102: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, laplace_distribution, wd) Chris@102: { Chris@102: os << wd.param(); Chris@102: return os; Chris@102: } Chris@102: Chris@102: /** Reads an @c laplace_distribution from a @c std::istream. */ Chris@102: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, laplace_distribution, wd) Chris@102: { Chris@102: param_type parm; Chris@102: if(is >> parm) { Chris@102: wd.param(parm); Chris@102: } Chris@102: return is; Chris@102: } Chris@102: Chris@102: /** Chris@102: * Returns true if the two instances of @c laplace_distribution will Chris@102: * return identical sequences of values given equal generators. Chris@102: */ Chris@102: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(laplace_distribution, lhs, rhs) Chris@102: { return lhs._mean == rhs._mean && lhs._beta == rhs._beta; } Chris@102: Chris@102: /** Chris@102: * Returns true if the two instances of @c laplace_distribution will Chris@102: * return different sequences of values given equal generators. Chris@102: */ Chris@102: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(laplace_distribution) Chris@102: Chris@102: private: Chris@102: RealType _mean; Chris@102: RealType _beta; Chris@102: }; Chris@102: Chris@102: } // namespace random Chris@102: } // namespace boost Chris@102: Chris@102: #endif // BOOST_RANDOM_LAPLACE_DISTRIBUTION_HPP