exponential.hpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // Copyright John Maddock 2006.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_STATS_EXPONENTIAL_HPP
  6. #define BOOST_STATS_EXPONENTIAL_HPP
  7. #include <boost/math/distributions/fwd.hpp>
  8. #include <boost/math/constants/constants.hpp>
  9. #include <boost/math/special_functions/log1p.hpp>
  10. #include <boost/math/special_functions/expm1.hpp>
  11. #include <boost/math/distributions/complement.hpp>
  12. #include <boost/math/distributions/detail/common_error_handling.hpp>
  13. #ifdef _MSC_VER
  14. # pragma warning(push)
  15. # pragma warning(disable: 4127) // conditional expression is constant
  16. # pragma warning(disable: 4702) // unreachable code (return after domain_error throw).
  17. #endif
  18. #include <utility>
  19. #include <cmath>
  20. namespace boost{ namespace math{
  21. namespace detail{
  22. //
  23. // Error check:
  24. //
  25. template <class RealType, class Policy>
  26. inline bool verify_lambda(const char* function, RealType l, RealType* presult, const Policy& pol)
  27. {
  28. if((l <= 0) || !(boost::math::isfinite)(l))
  29. {
  30. *presult = policies::raise_domain_error<RealType>(
  31. function,
  32. "The scale parameter \"lambda\" must be > 0, but was: %1%.", l, pol);
  33. return false;
  34. }
  35. return true;
  36. }
  37. template <class RealType, class Policy>
  38. inline bool verify_exp_x(const char* function, RealType x, RealType* presult, const Policy& pol)
  39. {
  40. if((x < 0) || (boost::math::isnan)(x))
  41. {
  42. *presult = policies::raise_domain_error<RealType>(
  43. function,
  44. "The random variable must be >= 0, but was: %1%.", x, pol);
  45. return false;
  46. }
  47. return true;
  48. }
  49. } // namespace detail
  50. template <class RealType = double, class Policy = policies::policy<> >
  51. class exponential_distribution
  52. {
  53. public:
  54. using value_type = RealType;
  55. using policy_type = Policy;
  56. explicit exponential_distribution(RealType l_lambda = 1)
  57. : m_lambda(l_lambda)
  58. {
  59. RealType err;
  60. detail::verify_lambda("boost::math::exponential_distribution<%1%>::exponential_distribution", l_lambda, &err, Policy());
  61. } // exponential_distribution
  62. RealType lambda()const { return m_lambda; }
  63. private:
  64. RealType m_lambda;
  65. };
  66. using exponential = exponential_distribution<double>;
  67. #ifdef __cpp_deduction_guides
  68. template <class RealType>
  69. exponential_distribution(RealType)->exponential_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  70. #endif
  71. template <class RealType, class Policy>
  72. inline std::pair<RealType, RealType> range(const exponential_distribution<RealType, Policy>& /*dist*/)
  73. { // Range of permissible values for random variable x.
  74. if (std::numeric_limits<RealType>::has_infinity)
  75. {
  76. return std::pair<RealType, RealType>(static_cast<RealType>(0), std::numeric_limits<RealType>::infinity()); // 0 to + infinity.
  77. }
  78. else
  79. {
  80. using boost::math::tools::max_value;
  81. return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>()); // 0 to + max
  82. }
  83. }
  84. template <class RealType, class Policy>
  85. inline std::pair<RealType, RealType> support(const exponential_distribution<RealType, Policy>& /*dist*/)
  86. { // Range of supported values for random variable x.
  87. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  88. using boost::math::tools::max_value;
  89. using boost::math::tools::min_value;
  90. return std::pair<RealType, RealType>(min_value<RealType>(), max_value<RealType>());
  91. // min_value<RealType>() to avoid a discontinuity at x = 0.
  92. }
  93. template <class RealType, class Policy>
  94. inline RealType pdf(const exponential_distribution<RealType, Policy>& dist, const RealType& x)
  95. {
  96. BOOST_MATH_STD_USING // for ADL of std functions
  97. static const char* function = "boost::math::pdf(const exponential_distribution<%1%>&, %1%)";
  98. RealType lambda = dist.lambda();
  99. RealType result = 0;
  100. if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
  101. return result;
  102. if(0 == detail::verify_exp_x(function, x, &result, Policy()))
  103. return result;
  104. // Workaround for VC11/12 bug:
  105. if ((boost::math::isinf)(x))
  106. return 0;
  107. result = lambda * exp(-lambda * x);
  108. return result;
  109. } // pdf
  110. template <class RealType, class Policy>
  111. inline RealType logpdf(const exponential_distribution<RealType, Policy>& dist, const RealType& x)
  112. {
  113. BOOST_MATH_STD_USING // for ADL of std functions
  114. static const char* function = "boost::math::logpdf(const exponential_distribution<%1%>&, %1%)";
  115. RealType lambda = dist.lambda();
  116. RealType result = -std::numeric_limits<RealType>::infinity();
  117. if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
  118. return result;
  119. if(0 == detail::verify_exp_x(function, x, &result, Policy()))
  120. return result;
  121. result = log(lambda) - lambda * x;
  122. return result;
  123. } // logpdf
  124. template <class RealType, class Policy>
  125. inline RealType cdf(const exponential_distribution<RealType, Policy>& dist, const RealType& x)
  126. {
  127. BOOST_MATH_STD_USING // for ADL of std functions
  128. static const char* function = "boost::math::cdf(const exponential_distribution<%1%>&, %1%)";
  129. RealType result = 0;
  130. RealType lambda = dist.lambda();
  131. if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
  132. return result;
  133. if(0 == detail::verify_exp_x(function, x, &result, Policy()))
  134. return result;
  135. result = -boost::math::expm1(-x * lambda, Policy());
  136. return result;
  137. } // cdf
  138. template <class RealType, class Policy>
  139. inline RealType quantile(const exponential_distribution<RealType, Policy>& dist, const RealType& p)
  140. {
  141. BOOST_MATH_STD_USING // for ADL of std functions
  142. static const char* function = "boost::math::quantile(const exponential_distribution<%1%>&, %1%)";
  143. RealType result = 0;
  144. RealType lambda = dist.lambda();
  145. if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
  146. return result;
  147. if(0 == detail::check_probability(function, p, &result, Policy()))
  148. return result;
  149. if(p == 0)
  150. return 0;
  151. if(p == 1)
  152. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  153. result = -boost::math::log1p(-p, Policy()) / lambda;
  154. return result;
  155. } // quantile
  156. template <class RealType, class Policy>
  157. inline RealType cdf(const complemented2_type<exponential_distribution<RealType, Policy>, RealType>& c)
  158. {
  159. BOOST_MATH_STD_USING // for ADL of std functions
  160. static const char* function = "boost::math::cdf(const exponential_distribution<%1%>&, %1%)";
  161. RealType result = 0;
  162. RealType lambda = c.dist.lambda();
  163. if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
  164. return result;
  165. if(0 == detail::verify_exp_x(function, c.param, &result, Policy()))
  166. return result;
  167. // Workaround for VC11/12 bug:
  168. if (c.param >= tools::max_value<RealType>())
  169. return 0;
  170. result = exp(-c.param * lambda);
  171. return result;
  172. }
  173. template <class RealType, class Policy>
  174. inline RealType quantile(const complemented2_type<exponential_distribution<RealType, Policy>, RealType>& c)
  175. {
  176. BOOST_MATH_STD_USING // for ADL of std functions
  177. static const char* function = "boost::math::quantile(const exponential_distribution<%1%>&, %1%)";
  178. RealType result = 0;
  179. RealType lambda = c.dist.lambda();
  180. if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
  181. return result;
  182. RealType q = c.param;
  183. if(0 == detail::check_probability(function, q, &result, Policy()))
  184. return result;
  185. if(q == 1)
  186. return 0;
  187. if(q == 0)
  188. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  189. result = -log(q) / lambda;
  190. return result;
  191. }
  192. template <class RealType, class Policy>
  193. inline RealType mean(const exponential_distribution<RealType, Policy>& dist)
  194. {
  195. RealType result = 0;
  196. RealType lambda = dist.lambda();
  197. if(0 == detail::verify_lambda("boost::math::mean(const exponential_distribution<%1%>&)", lambda, &result, Policy()))
  198. return result;
  199. return 1 / lambda;
  200. }
  201. template <class RealType, class Policy>
  202. inline RealType standard_deviation(const exponential_distribution<RealType, Policy>& dist)
  203. {
  204. RealType result = 0;
  205. RealType lambda = dist.lambda();
  206. if(0 == detail::verify_lambda("boost::math::standard_deviation(const exponential_distribution<%1%>&)", lambda, &result, Policy()))
  207. return result;
  208. return 1 / lambda;
  209. }
  210. template <class RealType, class Policy>
  211. inline RealType mode(const exponential_distribution<RealType, Policy>& /*dist*/)
  212. {
  213. return 0;
  214. }
  215. template <class RealType, class Policy>
  216. inline RealType median(const exponential_distribution<RealType, Policy>& dist)
  217. {
  218. using boost::math::constants::ln_two;
  219. return ln_two<RealType>() / dist.lambda(); // ln(2) / lambda
  220. }
  221. template <class RealType, class Policy>
  222. inline RealType skewness(const exponential_distribution<RealType, Policy>& /*dist*/)
  223. {
  224. return 2;
  225. }
  226. template <class RealType, class Policy>
  227. inline RealType kurtosis(const exponential_distribution<RealType, Policy>& /*dist*/)
  228. {
  229. return 9;
  230. }
  231. template <class RealType, class Policy>
  232. inline RealType kurtosis_excess(const exponential_distribution<RealType, Policy>& /*dist*/)
  233. {
  234. return 6;
  235. }
  236. template <class RealType, class Policy>
  237. inline RealType entropy(const exponential_distribution<RealType, Policy>& dist)
  238. {
  239. using std::log;
  240. return 1 - log(dist.lambda());
  241. }
  242. } // namespace math
  243. } // namespace boost
  244. #ifdef _MSC_VER
  245. # pragma warning(pop)
  246. #endif
  247. // This include must be at the end, *after* the accessors
  248. // for this distribution have been defined, in order to
  249. // keep compilers that support two-phase lookup happy.
  250. #include <boost/math/distributions/detail/derived_accessors.hpp>
  251. #endif // BOOST_STATS_EXPONENTIAL_HPP