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)
6 #ifndef BOOST_STATS_GAMMA_HPP
7 #define BOOST_STATS_GAMMA_HPP
9 // http://www.itl.nist.gov/div898/handbook/eda/section3/eda366b.htm
10 // http://mathworld.wolfram.com/GammaDistribution.html
11 // http://en.wikipedia.org/wiki/Gamma_distribution
13 #include <boost/math/distributions/fwd.hpp>
14 #include <boost/math/special_functions/gamma.hpp>
15 #include <boost/math/distributions/detail/common_error_handling.hpp>
16 #include <boost/math/distributions/complement.hpp>
20 namespace boost{ namespace math
25 template <class RealType, class Policy>
26 inline bool check_gamma_shape(
29 RealType* result, const Policy& pol)
31 if((shape <= 0) || !(boost::math::isfinite)(shape))
33 *result = policies::raise_domain_error<RealType>(
35 "Shape parameter is %1%, but must be > 0 !", shape, pol);
41 template <class RealType, class Policy>
42 inline bool check_gamma_x(
45 RealType* result, const Policy& pol)
47 if((x < 0) || !(boost::math::isfinite)(x))
49 *result = policies::raise_domain_error<RealType>(
51 "Random variate is %1% but must be >= 0 !", x, pol);
57 template <class RealType, class Policy>
58 inline bool check_gamma(
62 RealType* result, const Policy& pol)
64 return check_scale(function, scale, result, pol) && check_gamma_shape(function, shape, result, pol);
69 template <class RealType = double, class Policy = policies::policy<> >
70 class gamma_distribution
73 typedef RealType value_type;
74 typedef Policy policy_type;
76 gamma_distribution(RealType l_shape, RealType l_scale = 1)
77 : m_shape(l_shape), m_scale(l_scale)
80 detail::check_gamma("boost::math::gamma_distribution<%1%>::gamma_distribution", l_scale, l_shape, &result, Policy());
96 RealType m_shape; // distribution shape
97 RealType m_scale; // distribution scale
100 // NO typedef because of clash with name of gamma function.
102 template <class RealType, class Policy>
103 inline const std::pair<RealType, RealType> range(const gamma_distribution<RealType, Policy>& /* dist */)
104 { // Range of permissible values for random variable x.
105 using boost::math::tools::max_value;
106 return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>());
109 template <class RealType, class Policy>
110 inline const std::pair<RealType, RealType> support(const gamma_distribution<RealType, Policy>& /* dist */)
111 { // Range of supported values for random variable x.
112 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
113 using boost::math::tools::max_value;
114 using boost::math::tools::min_value;
115 return std::pair<RealType, RealType>(min_value<RealType>(), max_value<RealType>());
118 template <class RealType, class Policy>
119 inline RealType pdf(const gamma_distribution<RealType, Policy>& dist, const RealType& x)
121 BOOST_MATH_STD_USING // for ADL of std functions
123 static const char* function = "boost::math::pdf(const gamma_distribution<%1%>&, %1%)";
125 RealType shape = dist.shape();
126 RealType scale = dist.scale();
129 if(false == detail::check_gamma(function, scale, shape, &result, Policy()))
131 if(false == detail::check_gamma_x(function, x, &result, Policy()))
138 result = gamma_p_derivative(shape, x / scale, Policy()) / scale;
142 template <class RealType, class Policy>
143 inline RealType cdf(const gamma_distribution<RealType, Policy>& dist, const RealType& x)
145 BOOST_MATH_STD_USING // for ADL of std functions
147 static const char* function = "boost::math::cdf(const gamma_distribution<%1%>&, %1%)";
149 RealType shape = dist.shape();
150 RealType scale = dist.scale();
153 if(false == detail::check_gamma(function, scale, shape, &result, Policy()))
155 if(false == detail::check_gamma_x(function, x, &result, Policy()))
158 result = boost::math::gamma_p(shape, x / scale, Policy());
162 template <class RealType, class Policy>
163 inline RealType quantile(const gamma_distribution<RealType, Policy>& dist, const RealType& p)
165 BOOST_MATH_STD_USING // for ADL of std functions
167 static const char* function = "boost::math::quantile(const gamma_distribution<%1%>&, %1%)";
169 RealType shape = dist.shape();
170 RealType scale = dist.scale();
173 if(false == detail::check_gamma(function, scale, shape, &result, Policy()))
175 if(false == detail::check_probability(function, p, &result, Policy()))
179 return policies::raise_overflow_error<RealType>(function, 0, Policy());
181 result = gamma_p_inv(shape, p, Policy()) * scale;
186 template <class RealType, class Policy>
187 inline RealType cdf(const complemented2_type<gamma_distribution<RealType, Policy>, RealType>& c)
189 BOOST_MATH_STD_USING // for ADL of std functions
191 static const char* function = "boost::math::quantile(const gamma_distribution<%1%>&, %1%)";
193 RealType shape = c.dist.shape();
194 RealType scale = c.dist.scale();
197 if(false == detail::check_gamma(function, scale, shape, &result, Policy()))
199 if(false == detail::check_gamma_x(function, c.param, &result, Policy()))
202 result = gamma_q(shape, c.param / scale, Policy());
207 template <class RealType, class Policy>
208 inline RealType quantile(const complemented2_type<gamma_distribution<RealType, Policy>, RealType>& c)
210 BOOST_MATH_STD_USING // for ADL of std functions
212 static const char* function = "boost::math::quantile(const gamma_distribution<%1%>&, %1%)";
214 RealType shape = c.dist.shape();
215 RealType scale = c.dist.scale();
216 RealType q = c.param;
219 if(false == detail::check_gamma(function, scale, shape, &result, Policy()))
221 if(false == detail::check_probability(function, q, &result, Policy()))
225 return policies::raise_overflow_error<RealType>(function, 0, Policy());
227 result = gamma_q_inv(shape, q, Policy()) * scale;
232 template <class RealType, class Policy>
233 inline RealType mean(const gamma_distribution<RealType, Policy>& dist)
235 BOOST_MATH_STD_USING // for ADL of std functions
237 static const char* function = "boost::math::mean(const gamma_distribution<%1%>&)";
239 RealType shape = dist.shape();
240 RealType scale = dist.scale();
243 if(false == detail::check_gamma(function, scale, shape, &result, Policy()))
246 result = shape * scale;
250 template <class RealType, class Policy>
251 inline RealType variance(const gamma_distribution<RealType, Policy>& dist)
253 BOOST_MATH_STD_USING // for ADL of std functions
255 static const char* function = "boost::math::variance(const gamma_distribution<%1%>&)";
257 RealType shape = dist.shape();
258 RealType scale = dist.scale();
261 if(false == detail::check_gamma(function, scale, shape, &result, Policy()))
264 result = shape * scale * scale;
268 template <class RealType, class Policy>
269 inline RealType mode(const gamma_distribution<RealType, Policy>& dist)
271 BOOST_MATH_STD_USING // for ADL of std functions
273 static const char* function = "boost::math::mode(const gamma_distribution<%1%>&)";
275 RealType shape = dist.shape();
276 RealType scale = dist.scale();
279 if(false == detail::check_gamma(function, scale, shape, &result, Policy()))
283 return policies::raise_domain_error<RealType>(
285 "The mode of the gamma distribution is only defined for values of the shape parameter >= 1, but got %1%.",
288 result = (shape - 1) * scale;
292 //template <class RealType, class Policy>
293 //inline RealType median(const gamma_distribution<RealType, Policy>& dist)
294 //{ // Rely on default definition in derived accessors.
297 template <class RealType, class Policy>
298 inline RealType skewness(const gamma_distribution<RealType, Policy>& dist)
300 BOOST_MATH_STD_USING // for ADL of std functions
302 static const char* function = "boost::math::skewness(const gamma_distribution<%1%>&)";
304 RealType shape = dist.shape();
305 RealType scale = dist.scale();
308 if(false == detail::check_gamma(function, scale, shape, &result, Policy()))
311 result = 2 / sqrt(shape);
315 template <class RealType, class Policy>
316 inline RealType kurtosis_excess(const gamma_distribution<RealType, Policy>& dist)
318 BOOST_MATH_STD_USING // for ADL of std functions
320 static const char* function = "boost::math::kurtosis_excess(const gamma_distribution<%1%>&)";
322 RealType shape = dist.shape();
323 RealType scale = dist.scale();
326 if(false == detail::check_gamma(function, scale, shape, &result, Policy()))
333 template <class RealType, class Policy>
334 inline RealType kurtosis(const gamma_distribution<RealType, Policy>& dist)
336 return kurtosis_excess(dist) + 3;
342 // This include must be at the end, *after* the accessors
343 // for this distribution have been defined, in order to
344 // keep compilers that support two-phase lookup happy.
345 #include <boost/math/distributions/detail/derived_accessors.hpp>
347 #endif // BOOST_STATS_GAMMA_HPP