laplace.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 _MSC_VER
  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. using value_type = RealType;
  33. using policy_type = Policy;
  34. // ----------------------------------
  35. // Constructor(s)
  36. // ----------------------------------
  37. explicit 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. using laplace = laplace_distribution<double>;
  67. #ifdef __cpp_deduction_guides
  68. template <class RealType>
  69. laplace_distribution(RealType)->laplace_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  70. template <class RealType>
  71. laplace_distribution(RealType,RealType)->laplace_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  72. #endif
  73. //
  74. // Non-member functions.
  75. template <class RealType, class Policy>
  76. inline std::pair<RealType, RealType> range(const laplace_distribution<RealType, Policy>&)
  77. {
  78. if (std::numeric_limits<RealType>::has_infinity)
  79. { // Can use infinity.
  80. return std::pair<RealType, RealType>(-std::numeric_limits<RealType>::infinity(), std::numeric_limits<RealType>::infinity()); // - to + infinity.
  81. }
  82. else
  83. { // Can only use max_value.
  84. using boost::math::tools::max_value;
  85. return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + max value.
  86. }
  87. }
  88. template <class RealType, class Policy>
  89. inline std::pair<RealType, RealType> support(const laplace_distribution<RealType, Policy>&)
  90. {
  91. if (std::numeric_limits<RealType>::has_infinity)
  92. { // Can Use infinity.
  93. return std::pair<RealType, RealType>(-std::numeric_limits<RealType>::infinity(), std::numeric_limits<RealType>::infinity()); // - to + infinity.
  94. }
  95. else
  96. { // Can only use max_value.
  97. using boost::math::tools::max_value;
  98. return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + max value.
  99. }
  100. }
  101. template <class RealType, class Policy>
  102. inline RealType pdf(const laplace_distribution<RealType, Policy>& dist, const RealType& x)
  103. {
  104. BOOST_MATH_STD_USING // for ADL of std functions
  105. // Checking function argument
  106. RealType result = 0;
  107. const char* function = "boost::math::pdf(const laplace_distribution<%1%>&, %1%))";
  108. // Check scale and location.
  109. if (false == dist.check_parameters(function, &result)) return result;
  110. // Special pdf values.
  111. if((boost::math::isinf)(x))
  112. {
  113. return 0; // pdf + and - infinity is zero.
  114. }
  115. if (false == detail::check_x(function, x, &result, Policy())) return result;
  116. // General case
  117. RealType scale( dist.scale() );
  118. RealType location( dist.location() );
  119. RealType exponent = x - location;
  120. if (exponent>0) exponent = -exponent;
  121. exponent /= scale;
  122. result = exp(exponent);
  123. result /= 2 * scale;
  124. return result;
  125. } // pdf
  126. template <class RealType, class Policy>
  127. inline RealType logpdf(const laplace_distribution<RealType, Policy>& dist, const RealType& x)
  128. {
  129. BOOST_MATH_STD_USING // for ADL of std functions
  130. // Checking function argument
  131. RealType result = -std::numeric_limits<RealType>::infinity();
  132. const char* function = "boost::math::logpdf(const laplace_distribution<%1%>&, %1%))";
  133. // Check scale and location.
  134. if (false == dist.check_parameters(function, &result))
  135. {
  136. return result;
  137. }
  138. // Special pdf values.
  139. if((boost::math::isinf)(x))
  140. {
  141. return result; // pdf + and - infinity is zero so logpdf is -INF
  142. }
  143. if (false == detail::check_x(function, x, &result, Policy()))
  144. {
  145. return result;
  146. }
  147. const RealType mu = dist.scale();
  148. const RealType b = dist.location();
  149. // if b is 0 avoid divde by 0 error
  150. if(abs(b) < std::numeric_limits<RealType>::epsilon())
  151. {
  152. result = log(pdf(dist, x));
  153. }
  154. else
  155. {
  156. // General case
  157. const RealType log2 = boost::math::constants::ln_two<RealType>();
  158. result = -abs(x-mu)/b - log(b) - log2;
  159. }
  160. return result;
  161. } // logpdf
  162. template <class RealType, class Policy>
  163. inline RealType cdf(const laplace_distribution<RealType, Policy>& dist, const RealType& x)
  164. {
  165. BOOST_MATH_STD_USING // For ADL of std functions.
  166. RealType result = 0;
  167. // Checking function argument.
  168. const char* function = "boost::math::cdf(const laplace_distribution<%1%>&, %1%)";
  169. // Check scale and location.
  170. if (false == dist.check_parameters(function, &result)) return result;
  171. // Special cdf values:
  172. if((boost::math::isinf)(x))
  173. {
  174. if(x < 0) return 0; // -infinity.
  175. return 1; // + infinity.
  176. }
  177. if (false == detail::check_x(function, x, &result, Policy())) return result;
  178. // General cdf values
  179. RealType scale( dist.scale() );
  180. RealType location( dist.location() );
  181. if (x < location)
  182. {
  183. result = exp( (x-location)/scale )/2;
  184. }
  185. else
  186. {
  187. result = 1 - exp( (location-x)/scale )/2;
  188. }
  189. return result;
  190. } // cdf
  191. template <class RealType, class Policy>
  192. inline RealType quantile(const laplace_distribution<RealType, Policy>& dist, const RealType& p)
  193. {
  194. BOOST_MATH_STD_USING // for ADL of std functions.
  195. // Checking function argument
  196. RealType result = 0;
  197. const char* function = "boost::math::quantile(const laplace_distribution<%1%>&, %1%)";
  198. if (false == dist.check_parameters(function, &result)) return result;
  199. if(false == detail::check_probability(function, p, &result, Policy())) return result;
  200. // Extreme values of p:
  201. if(p == 0)
  202. {
  203. result = policies::raise_overflow_error<RealType>(function,
  204. "probability parameter is 0, but must be > 0!", Policy());
  205. return -result; // -inf
  206. }
  207. if(p == 1)
  208. {
  209. result = policies::raise_overflow_error<RealType>(function,
  210. "probability parameter is 1, but must be < 1!", Policy());
  211. return result; // inf
  212. }
  213. // Calculate Quantile
  214. RealType scale( dist.scale() );
  215. RealType location( dist.location() );
  216. if (p - 0.5 < 0.0)
  217. result = location + scale*log( static_cast<RealType>(p*2) );
  218. else
  219. result = location - scale*log( static_cast<RealType>(-p*2 + 2) );
  220. return result;
  221. } // quantile
  222. template <class RealType, class Policy>
  223. inline RealType cdf(const complemented2_type<laplace_distribution<RealType, Policy>, RealType>& c)
  224. {
  225. // Calculate complement of cdf.
  226. BOOST_MATH_STD_USING // for ADL of std functions
  227. RealType scale = c.dist.scale();
  228. RealType location = c.dist.location();
  229. RealType x = c.param;
  230. RealType result = 0;
  231. // Checking function argument.
  232. const char* function = "boost::math::cdf(const complemented2_type<laplace_distribution<%1%>, %1%>&)";
  233. // Check scale and location.
  234. if (false == c.dist.check_parameters(function, &result)) return result;
  235. // Special cdf values.
  236. if((boost::math::isinf)(x))
  237. {
  238. if(x < 0) return 1; // cdf complement -infinity is unity.
  239. return 0; // cdf complement +infinity is zero.
  240. }
  241. if(false == detail::check_x(function, x, &result, Policy()))return result;
  242. // Cdf interval value.
  243. if (-x < -location)
  244. {
  245. result = exp( (-x+location)/scale )/2;
  246. }
  247. else
  248. {
  249. result = 1 - exp( (-location+x)/scale )/2;
  250. }
  251. return result;
  252. } // cdf complement
  253. template <class RealType, class Policy>
  254. inline RealType quantile(const complemented2_type<laplace_distribution<RealType, Policy>, RealType>& c)
  255. {
  256. BOOST_MATH_STD_USING // for ADL of std functions.
  257. // Calculate quantile.
  258. RealType scale = c.dist.scale();
  259. RealType location = c.dist.location();
  260. RealType q = c.param;
  261. RealType result = 0;
  262. // Checking function argument.
  263. const char* function = "quantile(const complemented2_type<laplace_distribution<%1%>, %1%>&)";
  264. if (false == c.dist.check_parameters(function, &result)) return result;
  265. // Extreme values.
  266. if(q == 0)
  267. {
  268. return std::numeric_limits<RealType>::infinity();
  269. }
  270. if(q == 1)
  271. {
  272. return -std::numeric_limits<RealType>::infinity();
  273. }
  274. if(false == detail::check_probability(function, q, &result, Policy())) return result;
  275. if (0.5 - q < 0.0)
  276. result = location + scale*log( static_cast<RealType>(-q*2 + 2) );
  277. else
  278. result = location - scale*log( static_cast<RealType>(q*2) );
  279. return result;
  280. } // quantile
  281. template <class RealType, class Policy>
  282. inline RealType mean(const laplace_distribution<RealType, Policy>& dist)
  283. {
  284. return dist.location();
  285. }
  286. template <class RealType, class Policy>
  287. inline RealType standard_deviation(const laplace_distribution<RealType, Policy>& dist)
  288. {
  289. return constants::root_two<RealType>() * dist.scale();
  290. }
  291. template <class RealType, class Policy>
  292. inline RealType mode(const laplace_distribution<RealType, Policy>& dist)
  293. {
  294. return dist.location();
  295. }
  296. template <class RealType, class Policy>
  297. inline RealType median(const laplace_distribution<RealType, Policy>& dist)
  298. {
  299. return dist.location();
  300. }
  301. template <class RealType, class Policy>
  302. inline RealType skewness(const laplace_distribution<RealType, Policy>& /*dist*/)
  303. {
  304. return 0;
  305. }
  306. template <class RealType, class Policy>
  307. inline RealType kurtosis(const laplace_distribution<RealType, Policy>& /*dist*/)
  308. {
  309. return 6;
  310. }
  311. template <class RealType, class Policy>
  312. inline RealType kurtosis_excess(const laplace_distribution<RealType, Policy>& /*dist*/)
  313. {
  314. return 3;
  315. }
  316. template <class RealType, class Policy>
  317. inline RealType entropy(const laplace_distribution<RealType, Policy> & dist)
  318. {
  319. using std::log;
  320. return log(2*dist.scale()*constants::e<RealType>());
  321. }
  322. #ifdef _MSC_VER
  323. # pragma warning(pop)
  324. #endif
  325. } // namespace math
  326. } // namespace boost
  327. // This include must be at the end, *after* the accessors
  328. // for this distribution have been defined, in order to
  329. // keep compilers that support two-phase lookup happy.
  330. #include <boost/math/distributions/detail/derived_accessors.hpp>
  331. #endif // BOOST_STATS_LAPLACE_HPP