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 #ifdef __cpp_deduction_guides
164 template <class RealType>
165 pareto_distribution(RealType)->pareto_distribution<typename boost::math::tools::promote_args<RealType>::type>;
166 template <class RealType>
167 pareto_distribution(RealType,RealType)->pareto_distribution<typename boost::math::tools::promote_args<RealType>::type>;
171 template <class RealType, class Policy>
172 inline const std::pair<RealType, RealType> range(const pareto_distribution<RealType, Policy>& /*dist*/)
173 { // Range of permissible values for random variable x.
174 using boost::math::tools::max_value;
175 return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>()); // scale zero to + infinity.
178 template <class RealType, class Policy>
179 inline const std::pair<RealType, RealType> support(const pareto_distribution<RealType, Policy>& dist)
180 { // Range of supported values for random variable x.
181 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
182 using boost::math::tools::max_value;
183 return std::pair<RealType, RealType>(dist.scale(), max_value<RealType>() ); // scale to + infinity.
186 template <class RealType, class Policy>
187 inline RealType pdf(const pareto_distribution<RealType, Policy>& dist, const RealType& x)
189 BOOST_MATH_STD_USING // for ADL of std function pow.
190 static const char* function = "boost::math::pdf(const pareto_distribution<%1%>&, %1%)";
191 RealType scale = dist.scale();
192 RealType shape = dist.shape();
194 if(false == (detail::check_pareto_x(function, x, &result, Policy())
195 && detail::check_pareto(function, scale, shape, &result, Policy())))
198 { // regardless of shape, pdf is zero (or should be disallow x < scale and throw an exception?).
201 result = shape * pow(scale, shape) / pow(x, shape+1);
205 template <class RealType, class Policy>
206 inline RealType cdf(const pareto_distribution<RealType, Policy>& dist, const RealType& x)
208 BOOST_MATH_STD_USING // for ADL of std function pow.
209 static const char* function = "boost::math::cdf(const pareto_distribution<%1%>&, %1%)";
210 RealType scale = dist.scale();
211 RealType shape = dist.shape();
214 if(false == (detail::check_pareto_x(function, x, &result, Policy())
215 && detail::check_pareto(function, scale, shape, &result, Policy())))
219 { // regardless of shape, cdf is zero.
223 // result = RealType(1) - pow((scale / x), shape);
224 result = -boost::math::powm1(scale/x, shape, Policy()); // should be more accurate.
228 template <class RealType, class Policy>
229 inline RealType quantile(const pareto_distribution<RealType, Policy>& dist, const RealType& p)
231 BOOST_MATH_STD_USING // for ADL of std function pow.
232 static const char* function = "boost::math::quantile(const pareto_distribution<%1%>&, %1%)";
234 RealType scale = dist.scale();
235 RealType shape = dist.shape();
236 if(false == (detail::check_probability(function, p, &result, Policy())
237 && detail::check_pareto(function, scale, shape, &result, Policy())))
243 return scale; // x must be scale (or less).
247 return policies::raise_overflow_error<RealType>(function, 0, Policy()); // x = + infinity.
250 (pow((1 - p), 1 / shape));
251 // K. Krishnamoorthy, ISBN 1-58488-635-8 eq 23.1.3
255 template <class RealType, class Policy>
256 inline RealType cdf(const complemented2_type<pareto_distribution<RealType, Policy>, RealType>& c)
258 BOOST_MATH_STD_USING // for ADL of std function pow.
259 static const char* function = "boost::math::cdf(const pareto_distribution<%1%>&, %1%)";
261 RealType x = c.param;
262 RealType scale = c.dist.scale();
263 RealType shape = c.dist.shape();
264 if(false == (detail::check_pareto_x(function, x, &result, Policy())
265 && detail::check_pareto(function, scale, shape, &result, Policy())))
269 { // regardless of shape, cdf is zero, and complement is unity.
272 result = pow((scale/x), shape);
277 template <class RealType, class Policy>
278 inline RealType quantile(const complemented2_type<pareto_distribution<RealType, Policy>, RealType>& c)
280 BOOST_MATH_STD_USING // for ADL of std function pow.
281 static const char* function = "boost::math::quantile(const pareto_distribution<%1%>&, %1%)";
283 RealType q = c.param;
284 RealType scale = c.dist.scale();
285 RealType shape = c.dist.shape();
286 if(false == (detail::check_probability(function, q, &result, Policy())
287 && detail::check_pareto(function, scale, shape, &result, Policy())))
293 return scale; // x must be scale (or less).
297 return policies::raise_overflow_error<RealType>(function, 0, Policy()); // x = + infinity.
299 result = scale / (pow(q, 1 / shape));
300 // K. Krishnamoorthy, ISBN 1-58488-635-8 eq 23.1.3
302 } // quantile complement
304 template <class RealType, class Policy>
305 inline RealType mean(const pareto_distribution<RealType, Policy>& dist)
308 static const char* function = "boost::math::mean(const pareto_distribution<%1%>&, %1%)";
309 if(false == detail::check_pareto(function, dist.scale(), dist.shape(), &result, Policy()))
313 if (dist.shape() > RealType(1))
315 return dist.shape() * dist.scale() / (dist.shape() - 1);
319 using boost::math::tools::max_value;
320 return max_value<RealType>(); // +infinity.
324 template <class RealType, class Policy>
325 inline RealType mode(const pareto_distribution<RealType, Policy>& dist)
330 template <class RealType, class Policy>
331 inline RealType median(const pareto_distribution<RealType, Policy>& dist)
334 static const char* function = "boost::math::median(const pareto_distribution<%1%>&, %1%)";
335 if(false == detail::check_pareto(function, dist.scale(), dist.shape(), &result, Policy()))
340 return dist.scale() * pow(RealType(2), (1/dist.shape()));
343 template <class RealType, class Policy>
344 inline RealType variance(const pareto_distribution<RealType, Policy>& dist)
347 RealType scale = dist.scale();
348 RealType shape = dist.shape();
349 static const char* function = "boost::math::variance(const pareto_distribution<%1%>&, %1%)";
350 if(false == detail::check_pareto(function, scale, shape, &result, Policy()))
356 result = (scale * scale * shape) /
357 ((shape - 1) * (shape - 1) * (shape - 2));
361 result = policies::raise_domain_error<RealType>(
363 "variance is undefined for shape <= 2, but got %1%.", dist.shape(), Policy());
368 template <class RealType, class Policy>
369 inline RealType skewness(const pareto_distribution<RealType, Policy>& dist)
373 RealType shape = dist.shape();
374 static const char* function = "boost::math::pdf(const pareto_distribution<%1%>&, %1%)";
375 if(false == detail::check_pareto(function, dist.scale(), shape, &result, Policy()))
381 result = sqrt((shape - 2) / shape) *
387 result = policies::raise_domain_error<RealType>(
389 "skewness is undefined for shape <= 3, but got %1%.", dist.shape(), Policy());
394 template <class RealType, class Policy>
395 inline RealType kurtosis(const pareto_distribution<RealType, Policy>& dist)
398 RealType shape = dist.shape();
399 static const char* function = "boost::math::pdf(const pareto_distribution<%1%>&, %1%)";
400 if(false == detail::check_pareto(function, dist.scale(), shape, &result, Policy()))
406 result = 3 * ((shape - 2) * (3 * shape * shape + shape + 2)) /
407 (shape * (shape - 3) * (shape - 4));
411 result = policies::raise_domain_error<RealType>(
413 "kurtosis_excess is undefined for shape <= 4, but got %1%.", shape, Policy());
418 template <class RealType, class Policy>
419 inline RealType kurtosis_excess(const pareto_distribution<RealType, Policy>& dist)
422 RealType shape = dist.shape();
423 static const char* function = "boost::math::pdf(const pareto_distribution<%1%>&, %1%)";
424 if(false == detail::check_pareto(function, dist.scale(), shape, &result, Policy()))
430 result = 6 * ((shape * shape * shape) + (shape * shape) - 6 * shape - 2) /
431 (shape * (shape - 3) * (shape - 4));
435 result = policies::raise_domain_error<RealType>(
437 "kurtosis_excess is undefined for shape <= 4, but got %1%.", dist.shape(), Policy());
442 template <class RealType, class Policy>
443 inline RealType entropy(const pareto_distribution<RealType, Policy>& dist)
446 RealType xm = dist.scale();
447 RealType alpha = dist.shape();
448 return log(xm/alpha) + 1 + 1/alpha;
454 // This include must be at the end, *after* the accessors
455 // for this distribution have been defined, in order to
456 // keep compilers that support two-phase lookup happy.
457 #include <boost/math/distributions/detail/derived_accessors.hpp>
459 #endif // BOOST_STATS_PARETO_HPP