exponential.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 logcdf(const exponential_distribution<RealType, Policy>& dist, const RealType& x)
  140. {
  141. BOOST_MATH_STD_USING // for ADL of std functions
  142. static const char* function = "boost::math::logcdf(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::verify_exp_x(function, x, &result, Policy()))
  148. return result;
  149. result = boost::math::log1p(-exp(-x * lambda), Policy());
  150. return result;
  151. } // cdf
  152. template <class RealType, class Policy>
  153. inline RealType quantile(const exponential_distribution<RealType, Policy>& dist, const RealType& p)
  154. {
  155. BOOST_MATH_STD_USING // for ADL of std functions
  156. static const char* function = "boost::math::quantile(const exponential_distribution<%1%>&, %1%)";
  157. RealType result = 0;
  158. RealType lambda = dist.lambda();
  159. if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
  160. return result;
  161. if(0 == detail::check_probability(function, p, &result, Policy()))
  162. return result;
  163. if(p == 0)
  164. return 0;
  165. if(p == 1)
  166. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  167. result = -boost::math::log1p(-p, Policy()) / lambda;
  168. return result;
  169. } // quantile
  170. template <class RealType, class Policy>
  171. inline RealType cdf(const complemented2_type<exponential_distribution<RealType, Policy>, RealType>& c)
  172. {
  173. BOOST_MATH_STD_USING // for ADL of std functions
  174. static const char* function = "boost::math::cdf(const exponential_distribution<%1%>&, %1%)";
  175. RealType result = 0;
  176. RealType lambda = c.dist.lambda();
  177. if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
  178. return result;
  179. if(0 == detail::verify_exp_x(function, c.param, &result, Policy()))
  180. return result;
  181. // Workaround for VC11/12 bug:
  182. if (c.param >= tools::max_value<RealType>())
  183. return 0;
  184. result = exp(-c.param * lambda);
  185. return result;
  186. }
  187. template <class RealType, class Policy>
  188. inline RealType logcdf(const complemented2_type<exponential_distribution<RealType, Policy>, RealType>& c)
  189. {
  190. BOOST_MATH_STD_USING // for ADL of std functions
  191. static const char* function = "boost::math::logcdf(const exponential_distribution<%1%>&, %1%)";
  192. RealType result = 0;
  193. RealType lambda = c.dist.lambda();
  194. if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
  195. return result;
  196. if(0 == detail::verify_exp_x(function, c.param, &result, Policy()))
  197. return result;
  198. // Workaround for VC11/12 bug:
  199. if (c.param >= tools::max_value<RealType>())
  200. return 0;
  201. result = -c.param * lambda;
  202. return result;
  203. }
  204. template <class RealType, class Policy>
  205. inline RealType quantile(const complemented2_type<exponential_distribution<RealType, Policy>, RealType>& c)
  206. {
  207. BOOST_MATH_STD_USING // for ADL of std functions
  208. static const char* function = "boost::math::quantile(const exponential_distribution<%1%>&, %1%)";
  209. RealType result = 0;
  210. RealType lambda = c.dist.lambda();
  211. if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
  212. return result;
  213. RealType q = c.param;
  214. if(0 == detail::check_probability(function, q, &result, Policy()))
  215. return result;
  216. if(q == 1)
  217. return 0;
  218. if(q == 0)
  219. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  220. result = -log(q) / lambda;
  221. return result;
  222. }
  223. template <class RealType, class Policy>
  224. inline RealType mean(const exponential_distribution<RealType, Policy>& dist)
  225. {
  226. RealType result = 0;
  227. RealType lambda = dist.lambda();
  228. if(0 == detail::verify_lambda("boost::math::mean(const exponential_distribution<%1%>&)", lambda, &result, Policy()))
  229. return result;
  230. return 1 / lambda;
  231. }
  232. template <class RealType, class Policy>
  233. inline RealType standard_deviation(const exponential_distribution<RealType, Policy>& dist)
  234. {
  235. RealType result = 0;
  236. RealType lambda = dist.lambda();
  237. if(0 == detail::verify_lambda("boost::math::standard_deviation(const exponential_distribution<%1%>&)", lambda, &result, Policy()))
  238. return result;
  239. return 1 / lambda;
  240. }
  241. template <class RealType, class Policy>
  242. inline RealType mode(const exponential_distribution<RealType, Policy>& /*dist*/)
  243. {
  244. return 0;
  245. }
  246. template <class RealType, class Policy>
  247. inline RealType median(const exponential_distribution<RealType, Policy>& dist)
  248. {
  249. using boost::math::constants::ln_two;
  250. return ln_two<RealType>() / dist.lambda(); // ln(2) / lambda
  251. }
  252. template <class RealType, class Policy>
  253. inline RealType skewness(const exponential_distribution<RealType, Policy>& /*dist*/)
  254. {
  255. return 2;
  256. }
  257. template <class RealType, class Policy>
  258. inline RealType kurtosis(const exponential_distribution<RealType, Policy>& /*dist*/)
  259. {
  260. return 9;
  261. }
  262. template <class RealType, class Policy>
  263. inline RealType kurtosis_excess(const exponential_distribution<RealType, Policy>& /*dist*/)
  264. {
  265. return 6;
  266. }
  267. template <class RealType, class Policy>
  268. inline RealType entropy(const exponential_distribution<RealType, Policy>& dist)
  269. {
  270. using std::log;
  271. return 1 - log(dist.lambda());
  272. }
  273. } // namespace math
  274. } // namespace boost
  275. #ifdef _MSC_VER
  276. # pragma warning(pop)
  277. #endif
  278. // This include must be at the end, *after* the accessors
  279. // for this distribution have been defined, in order to
  280. // keep compilers that support two-phase lookup happy.
  281. #include <boost/math/distributions/detail/derived_accessors.hpp>
  282. #endif // BOOST_STATS_EXPONENTIAL_HPP