1 // Copyright John Maddock 2007.
2 // Copyright Paul A. Bristow 2007, 2009
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)
7 #ifndef BOOST_STATS_PARETO_HPP
8 #define BOOST_STATS_PARETO_HPP
10 // http://en.wikipedia.org/wiki/Pareto_distribution
11 // http://www.itl.nist.gov/div898/handbook/eda/section3/eda3661.htm
13 // Weisstein, Eric W. "Pareto Distribution."
14 // From MathWorld--A Wolfram Web Resource.
15 // http://mathworld.wolfram.com/ParetoDistribution.html
16 // Handbook of Statistical Distributions with Applications, K Krishnamoorthy, ISBN 1-58488-635-8, Chapter 23, pp 257 - 267.
17 // Caution KK's a and b are the reverse of Mathworld!
19 #include <boost/math/distributions/fwd.hpp>
20 #include <boost/math/distributions/complement.hpp>
21 #include <boost/math/distributions/detail/common_error_handling.hpp>
22 #include <boost/math/special_functions/powm1.hpp>
24 #include <utility> // for BOOST_CURRENT_VALUE?
31 { // Parameter checking.
32 template <class RealType, class Policy>
33 inline bool check_pareto_scale(
36 RealType* result, const Policy& pol)
38 if((boost::math::isfinite)(scale))
39 { // any > 0 finite value is OK.
46 *result = policies::raise_domain_error<RealType>(
48 "Scale parameter is %1%, but must be > 0!", scale, pol);
54 *result = policies::raise_domain_error<RealType>(
56 "Scale parameter is %1%, but must be finite!", scale, pol);
59 } // bool check_pareto_scale
61 template <class RealType, class Policy>
62 inline bool check_pareto_shape(
65 RealType* result, const Policy& pol)
67 if((boost::math::isfinite)(shape))
68 { // Any finite value > 0 is OK.
75 *result = policies::raise_domain_error<RealType>(
77 "Shape parameter is %1%, but must be > 0!", shape, pol);
83 *result = policies::raise_domain_error<RealType>(
85 "Shape parameter is %1%, but must be finite!", shape, pol);
88 } // bool check_pareto_shape(
90 template <class RealType, class Policy>
91 inline bool check_pareto_x(
94 RealType* result, const Policy& pol)
96 if((boost::math::isfinite)(x))
104 *result = policies::raise_domain_error<RealType>(
106 "x parameter is %1%, but must be > 0 !", x, pol);
112 *result = policies::raise_domain_error<RealType>(
114 "x parameter is %1%, but must be finite!", x, pol);
117 } // bool check_pareto_x
119 template <class RealType, class Policy>
120 inline bool check_pareto( // distribution parameters.
121 const char* function,
124 RealType* result, const Policy& pol)
126 return check_pareto_scale(function, scale, result, pol)
127 && check_pareto_shape(function, shape, result, pol);
128 } // bool check_pareto(
130 } // namespace detail
132 template <class RealType = double, class Policy = policies::policy<> >
133 class pareto_distribution
136 typedef RealType value_type;
137 typedef Policy policy_type;
139 pareto_distribution(RealType l_scale = 1, RealType l_shape = 1)
140 : m_scale(l_scale), m_shape(l_shape)
143 detail::check_pareto("boost::math::pareto_distribution<%1%>::pareto_distribution", l_scale, l_shape, &result, Policy());
146 RealType scale()const
147 { // AKA Xm and Wolfram b and beta
151 RealType shape()const
152 { // AKA k and Wolfram a and alpha
157 RealType m_scale; // distribution scale (xm) or beta
158 RealType m_shape; // distribution shape (k) or alpha
161 typedef pareto_distribution<double> pareto; // Convenience to allow pareto(2., 3.);
163 template <class RealType, class Policy>
164 inline const std::pair<RealType, RealType> range(const pareto_distribution<RealType, Policy>& /*dist*/)
165 { // Range of permissible values for random variable x.
166 using boost::math::tools::max_value;
167 return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>()); // scale zero to + infinity.
170 template <class RealType, class Policy>
171 inline const std::pair<RealType, RealType> support(const pareto_distribution<RealType, Policy>& dist)
172 { // Range of supported values for random variable x.
173 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
174 using boost::math::tools::max_value;
175 return std::pair<RealType, RealType>(dist.scale(), max_value<RealType>() ); // scale to + infinity.
178 template <class RealType, class Policy>
179 inline RealType pdf(const pareto_distribution<RealType, Policy>& dist, const RealType& x)
181 BOOST_MATH_STD_USING // for ADL of std function pow.
182 static const char* function = "boost::math::pdf(const pareto_distribution<%1%>&, %1%)";
183 RealType scale = dist.scale();
184 RealType shape = dist.shape();
186 if(false == (detail::check_pareto_x(function, x, &result, Policy())
187 && detail::check_pareto(function, scale, shape, &result, Policy())))
190 { // regardless of shape, pdf is zero (or should be disallow x < scale and throw an exception?).
193 result = shape * pow(scale, shape) / pow(x, shape+1);
197 template <class RealType, class Policy>
198 inline RealType cdf(const pareto_distribution<RealType, Policy>& dist, const RealType& x)
200 BOOST_MATH_STD_USING // for ADL of std function pow.
201 static const char* function = "boost::math::cdf(const pareto_distribution<%1%>&, %1%)";
202 RealType scale = dist.scale();
203 RealType shape = dist.shape();
206 if(false == (detail::check_pareto_x(function, x, &result, Policy())
207 && detail::check_pareto(function, scale, shape, &result, Policy())))
211 { // regardless of shape, cdf is zero.
215 // result = RealType(1) - pow((scale / x), shape);
216 result = -boost::math::powm1(scale/x, shape, Policy()); // should be more accurate.
220 template <class RealType, class Policy>
221 inline RealType quantile(const pareto_distribution<RealType, Policy>& dist, const RealType& p)
223 BOOST_MATH_STD_USING // for ADL of std function pow.
224 static const char* function = "boost::math::quantile(const pareto_distribution<%1%>&, %1%)";
226 RealType scale = dist.scale();
227 RealType shape = dist.shape();
228 if(false == (detail::check_probability(function, p, &result, Policy())
229 && detail::check_pareto(function, scale, shape, &result, Policy())))
235 return scale; // x must be scale (or less).
239 return policies::raise_overflow_error<RealType>(function, 0, Policy()); // x = + infinity.
242 (pow((1 - p), 1 / shape));
243 // K. Krishnamoorthy, ISBN 1-58488-635-8 eq 23.1.3
247 template <class RealType, class Policy>
248 inline RealType cdf(const complemented2_type<pareto_distribution<RealType, Policy>, RealType>& c)
250 BOOST_MATH_STD_USING // for ADL of std function pow.
251 static const char* function = "boost::math::cdf(const pareto_distribution<%1%>&, %1%)";
253 RealType x = c.param;
254 RealType scale = c.dist.scale();
255 RealType shape = c.dist.shape();
256 if(false == (detail::check_pareto_x(function, x, &result, Policy())
257 && detail::check_pareto(function, scale, shape, &result, Policy())))
261 { // regardless of shape, cdf is zero, and complement is unity.
264 result = pow((scale/x), shape);
269 template <class RealType, class Policy>
270 inline RealType quantile(const complemented2_type<pareto_distribution<RealType, Policy>, RealType>& c)
272 BOOST_MATH_STD_USING // for ADL of std function pow.
273 static const char* function = "boost::math::quantile(const pareto_distribution<%1%>&, %1%)";
275 RealType q = c.param;
276 RealType scale = c.dist.scale();
277 RealType shape = c.dist.shape();
278 if(false == (detail::check_probability(function, q, &result, Policy())
279 && detail::check_pareto(function, scale, shape, &result, Policy())))
285 return scale; // x must be scale (or less).
289 return policies::raise_overflow_error<RealType>(function, 0, Policy()); // x = + infinity.
291 result = scale / (pow(q, 1 / shape));
292 // K. Krishnamoorthy, ISBN 1-58488-635-8 eq 23.1.3
294 } // quantile complement
296 template <class RealType, class Policy>
297 inline RealType mean(const pareto_distribution<RealType, Policy>& dist)
300 static const char* function = "boost::math::mean(const pareto_distribution<%1%>&, %1%)";
301 if(false == detail::check_pareto(function, dist.scale(), dist.shape(), &result, Policy()))
305 if (dist.shape() > RealType(1))
307 return dist.shape() * dist.scale() / (dist.shape() - 1);
311 using boost::math::tools::max_value;
312 return max_value<RealType>(); // +infinity.
316 template <class RealType, class Policy>
317 inline RealType mode(const pareto_distribution<RealType, Policy>& dist)
322 template <class RealType, class Policy>
323 inline RealType median(const pareto_distribution<RealType, Policy>& dist)
326 static const char* function = "boost::math::median(const pareto_distribution<%1%>&, %1%)";
327 if(false == detail::check_pareto(function, dist.scale(), dist.shape(), &result, Policy()))
332 return dist.scale() * pow(RealType(2), (1/dist.shape()));
335 template <class RealType, class Policy>
336 inline RealType variance(const pareto_distribution<RealType, Policy>& dist)
339 RealType scale = dist.scale();
340 RealType shape = dist.shape();
341 static const char* function = "boost::math::variance(const pareto_distribution<%1%>&, %1%)";
342 if(false == detail::check_pareto(function, scale, shape, &result, Policy()))
348 result = (scale * scale * shape) /
349 ((shape - 1) * (shape - 1) * (shape - 2));
353 result = policies::raise_domain_error<RealType>(
355 "variance is undefined for shape <= 2, but got %1%.", dist.shape(), Policy());
360 template <class RealType, class Policy>
361 inline RealType skewness(const pareto_distribution<RealType, Policy>& dist)
365 RealType shape = dist.shape();
366 static const char* function = "boost::math::pdf(const pareto_distribution<%1%>&, %1%)";
367 if(false == detail::check_pareto(function, dist.scale(), shape, &result, Policy()))
373 result = sqrt((shape - 2) / shape) *
379 result = policies::raise_domain_error<RealType>(
381 "skewness is undefined for shape <= 3, but got %1%.", dist.shape(), Policy());
386 template <class RealType, class Policy>
387 inline RealType kurtosis(const pareto_distribution<RealType, Policy>& dist)
390 RealType shape = dist.shape();
391 static const char* function = "boost::math::pdf(const pareto_distribution<%1%>&, %1%)";
392 if(false == detail::check_pareto(function, dist.scale(), shape, &result, Policy()))
398 result = 3 * ((shape - 2) * (3 * shape * shape + shape + 2)) /
399 (shape * (shape - 3) * (shape - 4));
403 result = policies::raise_domain_error<RealType>(
405 "kurtosis_excess is undefined for shape <= 4, but got %1%.", shape, Policy());
410 template <class RealType, class Policy>
411 inline RealType kurtosis_excess(const pareto_distribution<RealType, Policy>& dist)
414 RealType shape = dist.shape();
415 static const char* function = "boost::math::pdf(const pareto_distribution<%1%>&, %1%)";
416 if(false == detail::check_pareto(function, dist.scale(), shape, &result, Policy()))
422 result = 6 * ((shape * shape * shape) + (shape * shape) - 6 * shape - 2) /
423 (shape * (shape - 3) * (shape - 4));
427 result = policies::raise_domain_error<RealType>(
429 "kurtosis_excess is undefined for shape <= 4, but got %1%.", dist.shape(), Policy());
434 template <class RealType, class Policy>
435 inline RealType entropy(const pareto_distribution<RealType, Policy>& dist)
438 RealType xm = dist.scale();
439 RealType alpha = dist.shape();
440 return log(xm/alpha) + 1 + 1/alpha;
446 // This include must be at the end, *after* the accessors
447 // for this distribution have been defined, in order to
448 // keep compilers that support two-phase lookup happy.
449 #include <boost/math/distributions/detail/derived_accessors.hpp>
451 #endif // BOOST_STATS_PARETO_HPP