laplace.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // Copyright Thijs van den Berg, 2008.
  2. // Copyright John Maddock 2008.
  3. // Copyright Paul A. Bristow 2008, 2014.
  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. #ifdef BOOST_MSVC
  22. # pragma warning(push)
  23. # pragma warning(disable:4127) // conditional expression is constant
  24. #endif
  25. template <class RealType = double, class Policy = policies::policy<> >
  26. class laplace_distribution
  27. {
  28. public:
  29. // ----------------------------------
  30. // public Types
  31. // ----------------------------------
  32. typedef RealType value_type;
  33. typedef Policy policy_type;
  34. // ----------------------------------
  35. // Constructor(s)
  36. // ----------------------------------
  37. laplace_distribution(RealType l_location = 0, RealType l_scale = 1)
  38. : m_location(l_location), m_scale(l_scale)
  39. {
  40. RealType result;
  41. check_parameters("boost::math::laplace_distribution<%1%>::laplace_distribution()", &result);
  42. }
  43. // ----------------------------------
  44. // Public functions
  45. // ----------------------------------
  46. RealType location() const
  47. {
  48. return m_location;
  49. }
  50. RealType scale() const
  51. {
  52. return m_scale;
  53. }
  54. bool check_parameters(const char* function, RealType* result) const
  55. {
  56. if(false == detail::check_scale(function, m_scale, result, Policy())) return false;
  57. if(false == detail::check_location(function, m_location, result, Policy())) return false;
  58. return true;
  59. }
  60. private:
  61. RealType m_location;
  62. RealType m_scale;
  63. }; // class laplace_distribution
  64. //
  65. // Convenient type synonym for double.
  66. typedef laplace_distribution<double> laplace;
  67. //
  68. // Non-member functions.
  69. template <class RealType, class Policy>
  70. inline const std::pair<RealType, RealType> range(const laplace_distribution<RealType, Policy>&)
  71. {
  72. if (std::numeric_limits<RealType>::has_infinity)
  73. { // Can use infinity.
  74. return std::pair<RealType, RealType>(-std::numeric_limits<RealType>::infinity(), std::numeric_limits<RealType>::infinity()); // - to + infinity.
  75. }
  76. else
  77. { // Can only use max_value.
  78. using boost::math::tools::max_value;
  79. return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + max value.
  80. }
  81. }
  82. template <class RealType, class Policy>
  83. inline const std::pair<RealType, RealType> support(const laplace_distribution<RealType, Policy>&)
  84. {
  85. if (std::numeric_limits<RealType>::has_infinity)
  86. { // Can Use infinity.
  87. return std::pair<RealType, RealType>(-std::numeric_limits<RealType>::infinity(), std::numeric_limits<RealType>::infinity()); // - to + infinity.
  88. }
  89. else
  90. { // Can only use max_value.
  91. using boost::math::tools::max_value;
  92. return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + max value.
  93. }
  94. }
  95. template <class RealType, class Policy>
  96. inline RealType pdf(const laplace_distribution<RealType, Policy>& dist, const RealType& x)
  97. {
  98. BOOST_MATH_STD_USING // for ADL of std functions
  99. // Checking function argument
  100. RealType result = 0;
  101. const char* function = "boost::math::pdf(const laplace_distribution<%1%>&, %1%))";
  102. // Check scale and location.
  103. if (false == dist.check_parameters(function, &result)) return result;
  104. // Special pdf values.
  105. if((boost::math::isinf)(x))
  106. {
  107. return 0; // pdf + and - infinity is zero.
  108. }
  109. if (false == detail::check_x(function, x, &result, Policy())) return result;
  110. // General case
  111. RealType scale( dist.scale() );
  112. RealType location( dist.location() );
  113. RealType exponent = x - location;
  114. if (exponent>0) exponent = -exponent;
  115. exponent /= scale;
  116. result = exp(exponent);
  117. result /= 2 * scale;
  118. return result;
  119. } // pdf
  120. template <class RealType, class Policy>
  121. inline RealType cdf(const laplace_distribution<RealType, Policy>& dist, const RealType& x)
  122. {
  123. BOOST_MATH_STD_USING // For ADL of std functions.
  124. RealType result = 0;
  125. // Checking function argument.
  126. const char* function = "boost::math::cdf(const laplace_distribution<%1%>&, %1%)";
  127. // Check scale and location.
  128. if (false == dist.check_parameters(function, &result)) return result;
  129. // Special cdf values:
  130. if((boost::math::isinf)(x))
  131. {
  132. if(x < 0) return 0; // -infinity.
  133. return 1; // + infinity.
  134. }
  135. if (false == detail::check_x(function, x, &result, Policy())) return result;
  136. // General cdf values
  137. RealType scale( dist.scale() );
  138. RealType location( dist.location() );
  139. if (x < location)
  140. {
  141. result = exp( (x-location)/scale )/2;
  142. }
  143. else
  144. {
  145. result = 1 - exp( (location-x)/scale )/2;
  146. }
  147. return result;
  148. } // cdf
  149. template <class RealType, class Policy>
  150. inline RealType quantile(const laplace_distribution<RealType, Policy>& dist, const RealType& p)
  151. {
  152. BOOST_MATH_STD_USING // for ADL of std functions.
  153. // Checking function argument
  154. RealType result = 0;
  155. const char* function = "boost::math::quantile(const laplace_distribution<%1%>&, %1%)";
  156. if (false == dist.check_parameters(function, &result)) return result;
  157. if(false == detail::check_probability(function, p, &result, Policy())) return result;
  158. // Extreme values of p:
  159. if(p == 0)
  160. {
  161. result = policies::raise_overflow_error<RealType>(function,
  162. "probability parameter is 0, but must be > 0!", Policy());
  163. return -result; // -std::numeric_limits<RealType>::infinity();
  164. }
  165. if(p == 1)
  166. {
  167. result = policies::raise_overflow_error<RealType>(function,
  168. "probability parameter is 1, but must be < 1!", Policy());
  169. return result; // std::numeric_limits<RealType>::infinity();
  170. }
  171. // Calculate Quantile
  172. RealType scale( dist.scale() );
  173. RealType location( dist.location() );
  174. if (p - 0.5 < 0.0)
  175. result = location + scale*log( static_cast<RealType>(p*2) );
  176. else
  177. result = location - scale*log( static_cast<RealType>(-p*2 + 2) );
  178. return result;
  179. } // quantile
  180. template <class RealType, class Policy>
  181. inline RealType cdf(const complemented2_type<laplace_distribution<RealType, Policy>, RealType>& c)
  182. {
  183. // Calculate complement of cdf.
  184. BOOST_MATH_STD_USING // for ADL of std functions
  185. RealType scale = c.dist.scale();
  186. RealType location = c.dist.location();
  187. RealType x = c.param;
  188. RealType result = 0;
  189. // Checking function argument.
  190. const char* function = "boost::math::cdf(const complemented2_type<laplace_distribution<%1%>, %1%>&)";
  191. // Check scale and location.
  192. //if(false == detail::check_scale(function, scale, result, Policy())) return false;
  193. //if(false == detail::check_location(function, location, result, Policy())) return false;
  194. if (false == c.dist.check_parameters(function, &result)) return result;
  195. // Special cdf values.
  196. if((boost::math::isinf)(x))
  197. {
  198. if(x < 0) return 1; // cdf complement -infinity is unity.
  199. return 0; // cdf complement +infinity is zero.
  200. }
  201. if(false == detail::check_x(function, x, &result, Policy()))return result;
  202. // Cdf interval value.
  203. if (-x < -location)
  204. {
  205. result = exp( (-x+location)/scale )/2;
  206. }
  207. else
  208. {
  209. result = 1 - exp( (-location+x)/scale )/2;
  210. }
  211. return result;
  212. } // cdf complement
  213. template <class RealType, class Policy>
  214. inline RealType quantile(const complemented2_type<laplace_distribution<RealType, Policy>, RealType>& c)
  215. {
  216. BOOST_MATH_STD_USING // for ADL of std functions.
  217. // Calculate quantile.
  218. RealType scale = c.dist.scale();
  219. RealType location = c.dist.location();
  220. RealType q = c.param;
  221. RealType result = 0;
  222. // Checking function argument.
  223. const char* function = "quantile(const complemented2_type<laplace_distribution<%1%>, %1%>&)";
  224. if (false == c.dist.check_parameters(function, &result)) return result;
  225. // Extreme values.
  226. if(q == 0)
  227. {
  228. return std::numeric_limits<RealType>::infinity();
  229. }
  230. if(q == 1)
  231. {
  232. return -std::numeric_limits<RealType>::infinity();
  233. }
  234. if(false == detail::check_probability(function, q, &result, Policy())) return result;
  235. if (0.5 - q < 0.0)
  236. result = location + scale*log( static_cast<RealType>(-q*2 + 2) );
  237. else
  238. result = location - scale*log( static_cast<RealType>(q*2) );
  239. return result;
  240. } // quantile
  241. template <class RealType, class Policy>
  242. inline RealType mean(const laplace_distribution<RealType, Policy>& dist)
  243. {
  244. return dist.location();
  245. }
  246. template <class RealType, class Policy>
  247. inline RealType standard_deviation(const laplace_distribution<RealType, Policy>& dist)
  248. {
  249. return constants::root_two<RealType>() * dist.scale();
  250. }
  251. template <class RealType, class Policy>
  252. inline RealType mode(const laplace_distribution<RealType, Policy>& dist)
  253. {
  254. return dist.location();
  255. }
  256. template <class RealType, class Policy>
  257. inline RealType median(const laplace_distribution<RealType, Policy>& dist)
  258. {
  259. return dist.location();
  260. }
  261. template <class RealType, class Policy>
  262. inline RealType skewness(const laplace_distribution<RealType, Policy>& /*dist*/)
  263. {
  264. return 0;
  265. }
  266. template <class RealType, class Policy>
  267. inline RealType kurtosis(const laplace_distribution<RealType, Policy>& /*dist*/)
  268. {
  269. return 6;
  270. }
  271. template <class RealType, class Policy>
  272. inline RealType kurtosis_excess(const laplace_distribution<RealType, Policy>& /*dist*/)
  273. {
  274. return 3;
  275. }
  276. template <class RealType, class Policy>
  277. inline RealType entropy(const laplace_distribution<RealType, Policy> & dist)
  278. {
  279. using std::log;
  280. return log(2*dist.scale()*constants::e<RealType>());
  281. }
  282. #ifdef BOOST_MSVC
  283. # pragma warning(pop)
  284. #endif
  285. } // namespace math
  286. } // namespace boost
  287. // This include must be at the end, *after* the accessors
  288. // for this distribution have been defined, in order to
  289. // keep compilers that support two-phase lookup happy.
  290. #include <boost/math/distributions/detail/derived_accessors.hpp>
  291. #endif // BOOST_STATS_LAPLACE_HPP