laplace.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // Copyright Thijs van den Berg, 2008.
  2. // Copyright John Maddock 2008.
  3. // Copyright Paul A. Bristow 2008.
  4. // Use, modification and distribution are subject to the
  5. // Boost Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. // This module implements the Laplace distribution.
  8. // Weisstein, Eric W. "Laplace Distribution." From MathWorld--A Wolfram Web Resource.
  9. // http://mathworld.wolfram.com/LaplaceDistribution.html
  10. // http://en.wikipedia.org/wiki/Laplace_distribution
  11. //
  12. // Abramowitz and Stegun 1972, p 930
  13. // http://www.math.sfu.ca/~cbm/aands/page_930.htm
  14. #ifndef BOOST_STATS_LAPLACE_HPP
  15. #define BOOST_STATS_LAPLACE_HPP
  16. #include <boost/math/distributions/detail/common_error_handling.hpp>
  17. #include <boost/math/distributions/complement.hpp>
  18. #include <boost/math/constants/constants.hpp>
  19. #include <limits>
  20. namespace boost{ namespace math{
  21. template <class RealType = double, class Policy = policies::policy<> >
  22. class laplace_distribution
  23. {
  24. public:
  25. // ----------------------------------
  26. // public Types
  27. // ----------------------------------
  28. typedef RealType value_type;
  29. typedef Policy policy_type;
  30. // ----------------------------------
  31. // Constructor(s)
  32. // ----------------------------------
  33. laplace_distribution(RealType l_location = 0, RealType l_scale = 1)
  34. : m_location(l_location), m_scale(l_scale)
  35. {
  36. RealType result;
  37. check_parameters("boost::math::laplace_distribution<%1%>::laplace_distribution()", &result);
  38. }
  39. // ----------------------------------
  40. // Public functions
  41. // ----------------------------------
  42. RealType location() const
  43. {
  44. return m_location;
  45. }
  46. RealType scale() const
  47. {
  48. return m_scale;
  49. }
  50. bool check_parameters(const char* function, RealType* result) const
  51. {
  52. if(false == detail::check_scale(function, m_scale, result, Policy())) return false;
  53. if(false == detail::check_location(function, m_location, result, Policy())) return false;
  54. return true;
  55. }
  56. private:
  57. RealType m_location;
  58. RealType m_scale;
  59. }; // class laplace_distribution
  60. //
  61. // Convenient type synonym for double
  62. typedef laplace_distribution<double> laplace;
  63. //
  64. // Non member functions
  65. template <class RealType, class Policy>
  66. inline const std::pair<RealType, RealType> range(const laplace_distribution<RealType, Policy>&)
  67. {
  68. using boost::math::tools::max_value;
  69. return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>());
  70. }
  71. template <class RealType, class Policy>
  72. inline const std::pair<RealType, RealType> support(const laplace_distribution<RealType, Policy>&)
  73. {
  74. using boost::math::tools::max_value;
  75. return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>());
  76. }
  77. template <class RealType, class Policy>
  78. inline RealType pdf(const laplace_distribution<RealType, Policy>& dist, const RealType& x)
  79. {
  80. BOOST_MATH_STD_USING // for ADL of std functions
  81. // Checking function argument
  82. RealType result = 0;
  83. const char* function = "boost::math::pdf(const laplace_distribution<%1%>&, %1%))";
  84. if (false == dist.check_parameters(function, &result)) return result;
  85. if (false == detail::check_x(function, x, &result, Policy())) return result;
  86. // Special pdf values
  87. if((boost::math::isinf)(x))
  88. return 0; // pdf + and - infinity is zero.
  89. // General case
  90. RealType scale( dist.scale() );
  91. RealType location( dist.location() );
  92. RealType exponent = x - location;
  93. if (exponent>0) exponent = -exponent;
  94. exponent /= scale;
  95. result = exp(exponent);
  96. result /= 2 * scale;
  97. return result;
  98. } // pdf
  99. template <class RealType, class Policy>
  100. inline RealType cdf(const laplace_distribution<RealType, Policy>& dist, const RealType& x)
  101. {
  102. BOOST_MATH_STD_USING // for ADL of std functions
  103. // Checking function argument
  104. RealType result = 0;
  105. const char* function = "boost::math::cdf(const laplace_distribution<%1%>&, %1%)";
  106. if (false == dist.check_parameters(function, &result)) return result;
  107. if (false == detail::check_x(function, x, &result, Policy())) return result;
  108. // Special cdf values:
  109. if((boost::math::isinf)(x))
  110. {
  111. if(x < 0) return 0; // -infinity
  112. return 1; // + infinity
  113. }
  114. // General cdf values
  115. RealType scale( dist.scale() );
  116. RealType location( dist.location() );
  117. if (x < location)
  118. {
  119. result = exp( (x-location)/scale )/2;
  120. }
  121. else
  122. {
  123. result = 1 - exp( (location-x)/scale )/2;
  124. }
  125. return result;
  126. } // cdf
  127. template <class RealType, class Policy>
  128. inline RealType quantile(const laplace_distribution<RealType, Policy>& dist, const RealType& p)
  129. {
  130. BOOST_MATH_STD_USING // for ADL of std functions.
  131. // Checking function argument
  132. RealType result = 0;
  133. const char* function = "boost::math::quantile(const laplace_distribution<%1%>&, %1%)";
  134. if (false == dist.check_parameters(function, &result)) return result;
  135. if(false == detail::check_probability(function, p, &result, Policy())) return result;
  136. // Extreme values of p:
  137. if(p == 0)
  138. {
  139. result = policies::raise_overflow_error<RealType>(function,
  140. "probability parameter is 0, but must be > 0!", Policy());
  141. return -result; // -std::numeric_limits<RealType>::infinity();
  142. }
  143. if(p == 1)
  144. {
  145. result = policies::raise_overflow_error<RealType>(function,
  146. "probability parameter is 1, but must be < 1!", Policy());
  147. return result; // std::numeric_limits<RealType>::infinity();
  148. }
  149. // Calculate Quantile
  150. RealType scale( dist.scale() );
  151. RealType location( dist.location() );
  152. if (p - 0.5 < 0.0)
  153. result = location + scale*log( static_cast<RealType>(p*2) );
  154. else
  155. result = location - scale*log( static_cast<RealType>(-p*2 + 2) );
  156. return result;
  157. } // quantile
  158. template <class RealType, class Policy>
  159. inline RealType cdf(const complemented2_type<laplace_distribution<RealType, Policy>, RealType>& c)
  160. {
  161. BOOST_MATH_STD_USING // for ADL of std functions
  162. RealType scale = c.dist.scale();
  163. RealType location = c.dist.location();
  164. RealType x = c.param;
  165. // Checking function argument
  166. RealType result = 0;
  167. const char* function = "boost::math::cdf(const complemented2_type<laplace_distribution<%1%>, %1%>&)";
  168. if(false == detail::check_x(function, x, &result, Policy()))return result;
  169. // Calculate complement of cdf.
  170. // Special cdf value
  171. if((boost::math::isinf)(x))
  172. {
  173. if(x < 0) return 1; // cdf complement -infinity is unity.
  174. return 0; // cdf complement +infinity is zero.
  175. }
  176. // Cdf interval value.
  177. if (-x < -location)
  178. {
  179. result = exp( (-x+location)/scale )/2;
  180. }
  181. else
  182. {
  183. result = 1 - exp( (-location+x)/scale )/2;
  184. }
  185. return result;
  186. } // cdf complement
  187. template <class RealType, class Policy>
  188. inline RealType quantile(const complemented2_type<laplace_distribution<RealType, Policy>, RealType>& c)
  189. {
  190. BOOST_MATH_STD_USING // for ADL of std functions.
  191. // Calculate quantile.
  192. RealType scale = c.dist.scale();
  193. RealType location = c.dist.location();
  194. RealType q = c.param;
  195. // Checking function argument.
  196. RealType result = 0;
  197. const char* function = "quantile(const complemented2_type<laplace_distribution<%1%>, %1%>&)";
  198. if(false == detail::check_probability(function, q, &result, Policy())) return result;
  199. // extreme values
  200. if(q == 0) return std::numeric_limits<RealType>::infinity();
  201. if(q == 1) return -std::numeric_limits<RealType>::infinity();
  202. if (0.5 - q < 0.0)
  203. result = location + scale*log( static_cast<RealType>(-q*2 + 2) );
  204. else
  205. result = location - scale*log( static_cast<RealType>(q*2) );
  206. return result;
  207. } // quantile
  208. template <class RealType, class Policy>
  209. inline RealType mean(const laplace_distribution<RealType, Policy>& dist)
  210. {
  211. return dist.location();
  212. }
  213. template <class RealType, class Policy>
  214. inline RealType standard_deviation(const laplace_distribution<RealType, Policy>& dist)
  215. {
  216. return constants::root_two<RealType>() * dist.scale();
  217. }
  218. template <class RealType, class Policy>
  219. inline RealType mode(const laplace_distribution<RealType, Policy>& dist)
  220. {
  221. return dist.location();
  222. }
  223. template <class RealType, class Policy>
  224. inline RealType median(const laplace_distribution<RealType, Policy>& dist)
  225. {
  226. return dist.location();
  227. }
  228. template <class RealType, class Policy>
  229. inline RealType skewness(const laplace_distribution<RealType, Policy>& /*dist*/)
  230. {
  231. return 0;
  232. }
  233. template <class RealType, class Policy>
  234. inline RealType kurtosis(const laplace_distribution<RealType, Policy>& /*dist*/)
  235. {
  236. return 6;
  237. }
  238. template <class RealType, class Policy>
  239. inline RealType kurtosis_excess(const laplace_distribution<RealType, Policy>& /*dist*/)
  240. {
  241. return 3;
  242. }
  243. } // namespace math
  244. } // namespace boost
  245. // This include must be at the end, *after* the accessors
  246. // for this distribution have been defined, in order to
  247. // keep compilers that support two-phase lookup happy.
  248. #include <boost/math/distributions/detail/derived_accessors.hpp>
  249. #endif // BOOST_STATS_LAPLACE_HPP