exponential.hpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #include <boost/config/no_tr1/cmath.hpp>
  14. #ifdef BOOST_MSVC
  15. # pragma warning(push)
  16. # pragma warning(disable: 4127) // conditional expression is constant
  17. # pragma warning(disable: 4702) // unreachable code (return after domain_error throw).
  18. #endif
  19. #include <utility>
  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. typedef RealType value_type;
  55. typedef Policy policy_type;
  56. 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. typedef exponential_distribution<double> exponential;
  67. template <class RealType, class Policy>
  68. inline const std::pair<RealType, RealType> range(const exponential_distribution<RealType, Policy>& /*dist*/)
  69. { // Range of permissible values for random variable x.
  70. if (std::numeric_limits<RealType>::has_infinity)
  71. {
  72. return std::pair<RealType, RealType>(static_cast<RealType>(0), std::numeric_limits<RealType>::infinity()); // 0 to + infinity.
  73. }
  74. else
  75. {
  76. using boost::math::tools::max_value;
  77. return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>()); // 0 to + max
  78. }
  79. }
  80. template <class RealType, class Policy>
  81. inline const std::pair<RealType, RealType> support(const exponential_distribution<RealType, Policy>& /*dist*/)
  82. { // Range of supported values for random variable x.
  83. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  84. using boost::math::tools::max_value;
  85. using boost::math::tools::min_value;
  86. return std::pair<RealType, RealType>(min_value<RealType>(), max_value<RealType>());
  87. // min_value<RealType>() to avoid a discontinuity at x = 0.
  88. }
  89. template <class RealType, class Policy>
  90. inline RealType pdf(const exponential_distribution<RealType, Policy>& dist, const RealType& x)
  91. {
  92. BOOST_MATH_STD_USING // for ADL of std functions
  93. static const char* function = "boost::math::pdf(const exponential_distribution<%1%>&, %1%)";
  94. RealType lambda = dist.lambda();
  95. RealType result = 0;
  96. if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
  97. return result;
  98. if(0 == detail::verify_exp_x(function, x, &result, Policy()))
  99. return result;
  100. // Workaround for VC11/12 bug:
  101. if ((boost::math::isinf)(x))
  102. return 0;
  103. result = lambda * exp(-lambda * x);
  104. return result;
  105. } // pdf
  106. template <class RealType, class Policy>
  107. inline RealType cdf(const exponential_distribution<RealType, Policy>& dist, const RealType& x)
  108. {
  109. BOOST_MATH_STD_USING // for ADL of std functions
  110. static const char* function = "boost::math::cdf(const exponential_distribution<%1%>&, %1%)";
  111. RealType result = 0;
  112. RealType lambda = dist.lambda();
  113. if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
  114. return result;
  115. if(0 == detail::verify_exp_x(function, x, &result, Policy()))
  116. return result;
  117. result = -boost::math::expm1(-x * lambda, Policy());
  118. return result;
  119. } // cdf
  120. template <class RealType, class Policy>
  121. inline RealType quantile(const exponential_distribution<RealType, Policy>& dist, const RealType& p)
  122. {
  123. BOOST_MATH_STD_USING // for ADL of std functions
  124. static const char* function = "boost::math::quantile(const exponential_distribution<%1%>&, %1%)";
  125. RealType result = 0;
  126. RealType lambda = dist.lambda();
  127. if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
  128. return result;
  129. if(0 == detail::check_probability(function, p, &result, Policy()))
  130. return result;
  131. if(p == 0)
  132. return 0;
  133. if(p == 1)
  134. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  135. result = -boost::math::log1p(-p, Policy()) / lambda;
  136. return result;
  137. } // quantile
  138. template <class RealType, class Policy>
  139. inline RealType cdf(const complemented2_type<exponential_distribution<RealType, Policy>, RealType>& c)
  140. {
  141. BOOST_MATH_STD_USING // for ADL of std functions
  142. static const char* function = "boost::math::cdf(const exponential_distribution<%1%>&, %1%)";
  143. RealType result = 0;
  144. RealType lambda = c.dist.lambda();
  145. if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
  146. return result;
  147. if(0 == detail::verify_exp_x(function, c.param, &result, Policy()))
  148. return result;
  149. // Workaround for VC11/12 bug:
  150. if (c.param >= tools::max_value<RealType>())
  151. return 0;
  152. result = exp(-c.param * lambda);
  153. return result;
  154. }
  155. template <class RealType, class Policy>
  156. inline RealType quantile(const complemented2_type<exponential_distribution<RealType, Policy>, RealType>& c)
  157. {
  158. BOOST_MATH_STD_USING // for ADL of std functions
  159. static const char* function = "boost::math::quantile(const exponential_distribution<%1%>&, %1%)";
  160. RealType result = 0;
  161. RealType lambda = c.dist.lambda();
  162. if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
  163. return result;
  164. RealType q = c.param;
  165. if(0 == detail::check_probability(function, q, &result, Policy()))
  166. return result;
  167. if(q == 1)
  168. return 0;
  169. if(q == 0)
  170. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  171. result = -log(q) / lambda;
  172. return result;
  173. }
  174. template <class RealType, class Policy>
  175. inline RealType mean(const exponential_distribution<RealType, Policy>& dist)
  176. {
  177. RealType result = 0;
  178. RealType lambda = dist.lambda();
  179. if(0 == detail::verify_lambda("boost::math::mean(const exponential_distribution<%1%>&)", lambda, &result, Policy()))
  180. return result;
  181. return 1 / lambda;
  182. }
  183. template <class RealType, class Policy>
  184. inline RealType standard_deviation(const exponential_distribution<RealType, Policy>& dist)
  185. {
  186. RealType result = 0;
  187. RealType lambda = dist.lambda();
  188. if(0 == detail::verify_lambda("boost::math::standard_deviation(const exponential_distribution<%1%>&)", lambda, &result, Policy()))
  189. return result;
  190. return 1 / lambda;
  191. }
  192. template <class RealType, class Policy>
  193. inline RealType mode(const exponential_distribution<RealType, Policy>& /*dist*/)
  194. {
  195. return 0;
  196. }
  197. template <class RealType, class Policy>
  198. inline RealType median(const exponential_distribution<RealType, Policy>& dist)
  199. {
  200. using boost::math::constants::ln_two;
  201. return ln_two<RealType>() / dist.lambda(); // ln(2) / lambda
  202. }
  203. template <class RealType, class Policy>
  204. inline RealType skewness(const exponential_distribution<RealType, Policy>& /*dist*/)
  205. {
  206. return 2;
  207. }
  208. template <class RealType, class Policy>
  209. inline RealType kurtosis(const exponential_distribution<RealType, Policy>& /*dist*/)
  210. {
  211. return 9;
  212. }
  213. template <class RealType, class Policy>
  214. inline RealType kurtosis_excess(const exponential_distribution<RealType, Policy>& /*dist*/)
  215. {
  216. return 6;
  217. }
  218. template <class RealType, class Policy>
  219. inline RealType entropy(const exponential_distribution<RealType, Policy>& dist)
  220. {
  221. using std::log;
  222. return 1 - log(dist.lambda());
  223. }
  224. } // namespace math
  225. } // namespace boost
  226. #ifdef BOOST_MSVC
  227. # pragma warning(pop)
  228. #endif
  229. // This include must be at the end, *after* the accessors
  230. // for this distribution have been defined, in order to
  231. // keep compilers that support two-phase lookup happy.
  232. #include <boost/math/distributions/detail/derived_accessors.hpp>
  233. #endif // BOOST_STATS_EXPONENTIAL_HPP