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