gamma.hpp 10 KB

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