exponential.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. result = lambda * exp(-lambda * x);
  101. return result;
  102. } // pdf
  103. template <class RealType, class Policy>
  104. inline RealType cdf(const exponential_distribution<RealType, Policy>& dist, const RealType& x)
  105. {
  106. BOOST_MATH_STD_USING // for ADL of std functions
  107. static const char* function = "boost::math::cdf(const exponential_distribution<%1%>&, %1%)";
  108. RealType result = 0;
  109. RealType lambda = dist.lambda();
  110. if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
  111. return result;
  112. if(0 == detail::verify_exp_x(function, x, &result, Policy()))
  113. return result;
  114. result = -boost::math::expm1(-x * lambda, Policy());
  115. return result;
  116. } // cdf
  117. template <class RealType, class Policy>
  118. inline RealType quantile(const exponential_distribution<RealType, Policy>& dist, const RealType& p)
  119. {
  120. BOOST_MATH_STD_USING // for ADL of std functions
  121. static const char* function = "boost::math::quantile(const exponential_distribution<%1%>&, %1%)";
  122. RealType result = 0;
  123. RealType lambda = dist.lambda();
  124. if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
  125. return result;
  126. if(0 == detail::check_probability(function, p, &result, Policy()))
  127. return result;
  128. if(p == 0)
  129. return 0;
  130. if(p == 1)
  131. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  132. result = -boost::math::log1p(-p, Policy()) / lambda;
  133. return result;
  134. } // quantile
  135. template <class RealType, class Policy>
  136. inline RealType cdf(const complemented2_type<exponential_distribution<RealType, Policy>, RealType>& c)
  137. {
  138. BOOST_MATH_STD_USING // for ADL of std functions
  139. static const char* function = "boost::math::cdf(const exponential_distribution<%1%>&, %1%)";
  140. RealType result = 0;
  141. RealType lambda = c.dist.lambda();
  142. if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
  143. return result;
  144. if(0 == detail::verify_exp_x(function, c.param, &result, Policy()))
  145. return result;
  146. result = exp(-c.param * lambda);
  147. return result;
  148. }
  149. template <class RealType, class Policy>
  150. inline RealType quantile(const complemented2_type<exponential_distribution<RealType, Policy>, RealType>& c)
  151. {
  152. BOOST_MATH_STD_USING // for ADL of std functions
  153. static const char* function = "boost::math::quantile(const exponential_distribution<%1%>&, %1%)";
  154. RealType result = 0;
  155. RealType lambda = c.dist.lambda();
  156. if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
  157. return result;
  158. RealType q = c.param;
  159. if(0 == detail::check_probability(function, q, &result, Policy()))
  160. return result;
  161. if(q == 1)
  162. return 0;
  163. if(q == 0)
  164. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  165. result = -log(q) / lambda;
  166. return result;
  167. }
  168. template <class RealType, class Policy>
  169. inline RealType mean(const exponential_distribution<RealType, Policy>& dist)
  170. {
  171. RealType result = 0;
  172. RealType lambda = dist.lambda();
  173. if(0 == detail::verify_lambda("boost::math::mean(const exponential_distribution<%1%>&)", lambda, &result, Policy()))
  174. return result;
  175. return 1 / lambda;
  176. }
  177. template <class RealType, class Policy>
  178. inline RealType standard_deviation(const exponential_distribution<RealType, Policy>& dist)
  179. {
  180. RealType result = 0;
  181. RealType lambda = dist.lambda();
  182. if(0 == detail::verify_lambda("boost::math::standard_deviation(const exponential_distribution<%1%>&)", lambda, &result, Policy()))
  183. return result;
  184. return 1 / lambda;
  185. }
  186. template <class RealType, class Policy>
  187. inline RealType mode(const exponential_distribution<RealType, Policy>& /*dist*/)
  188. {
  189. return 0;
  190. }
  191. template <class RealType, class Policy>
  192. inline RealType median(const exponential_distribution<RealType, Policy>& dist)
  193. {
  194. using boost::math::constants::ln_two;
  195. return ln_two<RealType>() / dist.lambda(); // ln(2) / lambda
  196. }
  197. template <class RealType, class Policy>
  198. inline RealType skewness(const exponential_distribution<RealType, Policy>& /*dist*/)
  199. {
  200. return 2;
  201. }
  202. template <class RealType, class Policy>
  203. inline RealType kurtosis(const exponential_distribution<RealType, Policy>& /*dist*/)
  204. {
  205. return 9;
  206. }
  207. template <class RealType, class Policy>
  208. inline RealType kurtosis_excess(const exponential_distribution<RealType, Policy>& /*dist*/)
  209. {
  210. return 6;
  211. }
  212. } // namespace math
  213. } // namespace boost
  214. #ifdef BOOST_MSVC
  215. # pragma warning(pop)
  216. #endif
  217. // This include must be at the end, *after* the accessors
  218. // for this distribution have been defined, in order to
  219. // keep compilers that support two-phase lookup happy.
  220. #include <boost/math/distributions/detail/derived_accessors.hpp>
  221. #endif // BOOST_STATS_EXPONENTIAL_HPP