exponential.hpp 12 KB

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