gamma.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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_GAMMA_HPP
  6. #define BOOST_STATS_GAMMA_HPP
  7. // http://www.itl.nist.gov/div898/handbook/eda/section3/eda366b.htm
  8. // http://mathworld.wolfram.com/GammaDistribution.html
  9. // http://en.wikipedia.org/wiki/Gamma_distribution
  10. #include <boost/math/distributions/fwd.hpp>
  11. #include <boost/math/special_functions/gamma.hpp>
  12. #include <boost/math/special_functions/digamma.hpp>
  13. #include <boost/math/distributions/detail/common_error_handling.hpp>
  14. #include <boost/math/distributions/complement.hpp>
  15. #include <utility>
  16. #include <type_traits>
  17. namespace boost{ namespace math
  18. {
  19. namespace detail
  20. {
  21. template <class RealType, class Policy>
  22. inline bool check_gamma_shape(
  23. const char* function,
  24. RealType shape,
  25. RealType* result, const Policy& pol)
  26. {
  27. if((shape <= 0) || !(boost::math::isfinite)(shape))
  28. {
  29. *result = policies::raise_domain_error<RealType>(
  30. function,
  31. "Shape parameter is %1%, but must be > 0 !", shape, pol);
  32. return false;
  33. }
  34. return true;
  35. }
  36. template <class RealType, class Policy>
  37. inline bool check_gamma_x(
  38. const char* function,
  39. RealType const& x,
  40. RealType* result, const Policy& pol)
  41. {
  42. if((x < 0) || !(boost::math::isfinite)(x))
  43. {
  44. *result = policies::raise_domain_error<RealType>(
  45. function,
  46. "Random variate is %1% but must be >= 0 !", x, pol);
  47. return false;
  48. }
  49. return true;
  50. }
  51. template <class RealType, class Policy>
  52. inline bool check_gamma(
  53. const char* function,
  54. RealType scale,
  55. RealType shape,
  56. RealType* result, const Policy& pol)
  57. {
  58. return check_scale(function, scale, result, pol) && check_gamma_shape(function, shape, result, pol);
  59. }
  60. } // namespace detail
  61. template <class RealType = double, class Policy = policies::policy<> >
  62. class gamma_distribution
  63. {
  64. public:
  65. using value_type = RealType;
  66. using policy_type = Policy;
  67. explicit gamma_distribution(RealType l_shape, RealType l_scale = 1)
  68. : m_shape(l_shape), m_scale(l_scale)
  69. {
  70. RealType result;
  71. detail::check_gamma("boost::math::gamma_distribution<%1%>::gamma_distribution", l_scale, l_shape, &result, Policy());
  72. }
  73. RealType shape()const
  74. {
  75. return m_shape;
  76. }
  77. RealType scale()const
  78. {
  79. return m_scale;
  80. }
  81. private:
  82. //
  83. // Data members:
  84. //
  85. RealType m_shape; // distribution shape
  86. RealType m_scale; // distribution scale
  87. };
  88. // NO typedef because of clash with name of gamma function.
  89. #ifdef __cpp_deduction_guides
  90. template <class RealType>
  91. gamma_distribution(RealType)->gamma_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  92. template <class RealType>
  93. gamma_distribution(RealType,RealType)->gamma_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  94. #endif
  95. template <class RealType, class Policy>
  96. inline std::pair<RealType, RealType> range(const gamma_distribution<RealType, Policy>& /* dist */)
  97. { // Range of permissible values for random variable x.
  98. using boost::math::tools::max_value;
  99. return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>());
  100. }
  101. template <class RealType, class Policy>
  102. inline std::pair<RealType, RealType> support(const gamma_distribution<RealType, Policy>& /* dist */)
  103. { // Range of supported values for random variable x.
  104. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  105. using boost::math::tools::max_value;
  106. using boost::math::tools::min_value;
  107. return std::pair<RealType, RealType>(min_value<RealType>(), max_value<RealType>());
  108. }
  109. template <class RealType, class Policy>
  110. inline RealType pdf(const gamma_distribution<RealType, Policy>& dist, const RealType& x)
  111. {
  112. BOOST_MATH_STD_USING // for ADL of std functions
  113. static const char* function = "boost::math::pdf(const gamma_distribution<%1%>&, %1%)";
  114. RealType shape = dist.shape();
  115. RealType scale = dist.scale();
  116. RealType result = 0;
  117. if(false == detail::check_gamma(function, scale, shape, &result, Policy()))
  118. return result;
  119. if(false == detail::check_gamma_x(function, x, &result, Policy()))
  120. return result;
  121. if(x == 0)
  122. {
  123. return 0;
  124. }
  125. result = gamma_p_derivative(shape, x / scale, Policy()) / scale;
  126. return result;
  127. } // pdf
  128. template <class RealType, class Policy>
  129. inline RealType logpdf(const gamma_distribution<RealType, Policy>& dist, const RealType& x)
  130. {
  131. BOOST_MATH_STD_USING // for ADL of std functions
  132. using boost::math::lgamma;
  133. static const char* function = "boost::math::logpdf(const gamma_distribution<%1%>&, %1%)";
  134. RealType k = dist.shape();
  135. RealType theta = dist.scale();
  136. RealType result = -std::numeric_limits<RealType>::infinity();
  137. if(false == detail::check_gamma(function, theta, k, &result, Policy()))
  138. return result;
  139. if(false == detail::check_gamma_x(function, x, &result, Policy()))
  140. return result;
  141. if(x == 0)
  142. {
  143. return std::numeric_limits<RealType>::quiet_NaN();
  144. }
  145. result = -k*log(theta) + (k-1)*log(x) - lgamma(k) - (x/theta);
  146. return result;
  147. } // logpdf
  148. template <class RealType, class Policy>
  149. inline RealType cdf(const gamma_distribution<RealType, Policy>& dist, const RealType& x)
  150. {
  151. BOOST_MATH_STD_USING // for ADL of std functions
  152. static const char* function = "boost::math::cdf(const gamma_distribution<%1%>&, %1%)";
  153. RealType shape = dist.shape();
  154. RealType scale = dist.scale();
  155. RealType result = 0;
  156. if(false == detail::check_gamma(function, scale, shape, &result, Policy()))
  157. return result;
  158. if(false == detail::check_gamma_x(function, x, &result, Policy()))
  159. return result;
  160. result = boost::math::gamma_p(shape, x / scale, Policy());
  161. return result;
  162. } // cdf
  163. template <class RealType, class Policy>
  164. inline RealType quantile(const gamma_distribution<RealType, Policy>& dist, const RealType& p)
  165. {
  166. BOOST_MATH_STD_USING // for ADL of std functions
  167. static const char* function = "boost::math::quantile(const gamma_distribution<%1%>&, %1%)";
  168. RealType shape = dist.shape();
  169. RealType scale = dist.scale();
  170. RealType result = 0;
  171. if(false == detail::check_gamma(function, scale, shape, &result, Policy()))
  172. return result;
  173. if(false == detail::check_probability(function, p, &result, Policy()))
  174. return result;
  175. if(p == 1)
  176. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  177. result = gamma_p_inv(shape, p, Policy()) * scale;
  178. return result;
  179. }
  180. template <class RealType, class Policy>
  181. inline RealType cdf(const complemented2_type<gamma_distribution<RealType, Policy>, RealType>& c)
  182. {
  183. BOOST_MATH_STD_USING // for ADL of std functions
  184. static const char* function = "boost::math::quantile(const gamma_distribution<%1%>&, %1%)";
  185. RealType shape = c.dist.shape();
  186. RealType scale = c.dist.scale();
  187. RealType result = 0;
  188. if(false == detail::check_gamma(function, scale, shape, &result, Policy()))
  189. return result;
  190. if(false == detail::check_gamma_x(function, c.param, &result, Policy()))
  191. return result;
  192. result = gamma_q(shape, c.param / scale, Policy());
  193. return result;
  194. }
  195. template <class RealType, class Policy>
  196. inline RealType quantile(const complemented2_type<gamma_distribution<RealType, Policy>, RealType>& c)
  197. {
  198. BOOST_MATH_STD_USING // for ADL of std functions
  199. static const char* function = "boost::math::quantile(const gamma_distribution<%1%>&, %1%)";
  200. RealType shape = c.dist.shape();
  201. RealType scale = c.dist.scale();
  202. RealType q = c.param;
  203. RealType result = 0;
  204. if(false == detail::check_gamma(function, scale, shape, &result, Policy()))
  205. return result;
  206. if(false == detail::check_probability(function, q, &result, Policy()))
  207. return result;
  208. if(q == 0)
  209. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  210. result = gamma_q_inv(shape, q, Policy()) * scale;
  211. return result;
  212. }
  213. template <class RealType, class Policy>
  214. inline RealType mean(const gamma_distribution<RealType, Policy>& dist)
  215. {
  216. BOOST_MATH_STD_USING // for ADL of std functions
  217. static const char* function = "boost::math::mean(const gamma_distribution<%1%>&)";
  218. RealType shape = dist.shape();
  219. RealType scale = dist.scale();
  220. RealType result = 0;
  221. if(false == detail::check_gamma(function, scale, shape, &result, Policy()))
  222. return result;
  223. result = shape * scale;
  224. return result;
  225. }
  226. template <class RealType, class Policy>
  227. inline RealType variance(const gamma_distribution<RealType, Policy>& dist)
  228. {
  229. BOOST_MATH_STD_USING // for ADL of std functions
  230. static const char* function = "boost::math::variance(const gamma_distribution<%1%>&)";
  231. RealType shape = dist.shape();
  232. RealType scale = dist.scale();
  233. RealType result = 0;
  234. if(false == detail::check_gamma(function, scale, shape, &result, Policy()))
  235. return result;
  236. result = shape * scale * scale;
  237. return result;
  238. }
  239. template <class RealType, class Policy>
  240. inline RealType mode(const gamma_distribution<RealType, Policy>& dist)
  241. {
  242. BOOST_MATH_STD_USING // for ADL of std functions
  243. static const char* function = "boost::math::mode(const gamma_distribution<%1%>&)";
  244. RealType shape = dist.shape();
  245. RealType scale = dist.scale();
  246. RealType result = 0;
  247. if(false == detail::check_gamma(function, scale, shape, &result, Policy()))
  248. return result;
  249. if(shape < 1)
  250. return policies::raise_domain_error<RealType>(
  251. function,
  252. "The mode of the gamma distribution is only defined for values of the shape parameter >= 1, but got %1%.",
  253. shape, Policy());
  254. result = (shape - 1) * scale;
  255. return result;
  256. }
  257. //template <class RealType, class Policy>
  258. //inline RealType median(const gamma_distribution<RealType, Policy>& dist)
  259. //{ // Rely on default definition in derived accessors.
  260. //}
  261. template <class RealType, class Policy>
  262. inline RealType skewness(const gamma_distribution<RealType, Policy>& dist)
  263. {
  264. BOOST_MATH_STD_USING // for ADL of std functions
  265. static const char* function = "boost::math::skewness(const gamma_distribution<%1%>&)";
  266. RealType shape = dist.shape();
  267. RealType scale = dist.scale();
  268. RealType result = 0;
  269. if(false == detail::check_gamma(function, scale, shape, &result, Policy()))
  270. return result;
  271. result = 2 / sqrt(shape);
  272. return result;
  273. }
  274. template <class RealType, class Policy>
  275. inline RealType kurtosis_excess(const gamma_distribution<RealType, Policy>& dist)
  276. {
  277. BOOST_MATH_STD_USING // for ADL of std functions
  278. static const char* function = "boost::math::kurtosis_excess(const gamma_distribution<%1%>&)";
  279. RealType shape = dist.shape();
  280. RealType scale = dist.scale();
  281. RealType result = 0;
  282. if(false == detail::check_gamma(function, scale, shape, &result, Policy()))
  283. return result;
  284. result = 6 / shape;
  285. return result;
  286. }
  287. template <class RealType, class Policy>
  288. inline RealType kurtosis(const gamma_distribution<RealType, Policy>& dist)
  289. {
  290. return kurtosis_excess(dist) + 3;
  291. }
  292. template <class RealType, class Policy>
  293. inline RealType entropy(const gamma_distribution<RealType, Policy>& dist)
  294. {
  295. RealType k = dist.shape();
  296. RealType theta = dist.scale();
  297. using std::log;
  298. return k + log(theta) + lgamma(k) + (1-k)*digamma(k);
  299. }
  300. } // namespace math
  301. } // namespace boost
  302. // This include must be at the end, *after* the accessors
  303. // for this distribution have been defined, in order to
  304. // keep compilers that support two-phase lookup happy.
  305. #include <boost/math/distributions/detail/derived_accessors.hpp>
  306. #endif // BOOST_STATS_GAMMA_HPP