pareto.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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)
  6. #ifndef BOOST_STATS_PARETO_HPP
  7. #define BOOST_STATS_PARETO_HPP
  8. // http://en.wikipedia.org/wiki/Pareto_distribution
  9. // http://www.itl.nist.gov/div898/handbook/eda/section3/eda3661.htm
  10. // Also:
  11. // Weisstein, Eric W. "Pareto Distribution."
  12. // From MathWorld--A Wolfram Web Resource.
  13. // http://mathworld.wolfram.com/ParetoDistribution.html
  14. // Handbook of Statistical Distributions with Applications, K Krishnamoorthy, ISBN 1-58488-635-8, Chapter 23, pp 257 - 267.
  15. // Caution KK's a and b are the reverse of Mathworld!
  16. #include <boost/math/distributions/fwd.hpp>
  17. #include <boost/math/distributions/complement.hpp>
  18. #include <boost/math/distributions/detail/common_error_handling.hpp>
  19. #include <boost/math/special_functions/powm1.hpp>
  20. #include <utility> // for BOOST_CURRENT_VALUE?
  21. namespace boost
  22. {
  23. namespace math
  24. {
  25. namespace detail
  26. { // Parameter checking.
  27. template <class RealType, class Policy>
  28. inline bool check_pareto_scale(
  29. const char* function,
  30. RealType scale,
  31. RealType* result, const Policy& pol)
  32. {
  33. if((boost::math::isfinite)(scale))
  34. { // any > 0 finite value is OK.
  35. if (scale > 0)
  36. {
  37. return true;
  38. }
  39. else
  40. {
  41. *result = policies::raise_domain_error<RealType>(
  42. function,
  43. "Scale parameter is %1%, but must be > 0!", scale, pol);
  44. return false;
  45. }
  46. }
  47. else
  48. { // Not finite.
  49. *result = policies::raise_domain_error<RealType>(
  50. function,
  51. "Scale parameter is %1%, but must be finite!", scale, pol);
  52. return false;
  53. }
  54. } // bool check_pareto_scale
  55. template <class RealType, class Policy>
  56. inline bool check_pareto_shape(
  57. const char* function,
  58. RealType shape,
  59. RealType* result, const Policy& pol)
  60. {
  61. if((boost::math::isfinite)(shape))
  62. { // Any finite value > 0 is OK.
  63. if (shape > 0)
  64. {
  65. return true;
  66. }
  67. else
  68. {
  69. *result = policies::raise_domain_error<RealType>(
  70. function,
  71. "Shape parameter is %1%, but must be > 0!", shape, pol);
  72. return false;
  73. }
  74. }
  75. else
  76. { // Not finite.
  77. *result = policies::raise_domain_error<RealType>(
  78. function,
  79. "Shape parameter is %1%, but must be finite!", shape, pol);
  80. return false;
  81. }
  82. } // bool check_pareto_shape(
  83. template <class RealType, class Policy>
  84. inline bool check_pareto_x(
  85. const char* function,
  86. RealType const& x,
  87. RealType* result, const Policy& pol)
  88. {
  89. if((boost::math::isfinite)(x))
  90. { //
  91. if (x > 0)
  92. {
  93. return true;
  94. }
  95. else
  96. {
  97. *result = policies::raise_domain_error<RealType>(
  98. function,
  99. "x parameter is %1%, but must be > 0 !", x, pol);
  100. return false;
  101. }
  102. }
  103. else
  104. { // Not finite..
  105. *result = policies::raise_domain_error<RealType>(
  106. function,
  107. "x parameter is %1%, but must be finite!", x, pol);
  108. return false;
  109. }
  110. } // bool check_pareto_x
  111. template <class RealType, class Policy>
  112. inline bool check_pareto( // distribution parameters.
  113. const char* function,
  114. RealType scale,
  115. RealType shape,
  116. RealType* result, const Policy& pol)
  117. {
  118. return check_pareto_scale(function, scale, result, pol)
  119. && check_pareto_shape(function, shape, result, pol);
  120. } // bool check_pareto(
  121. } // namespace detail
  122. template <class RealType = double, class Policy = policies::policy<> >
  123. class pareto_distribution
  124. {
  125. public:
  126. typedef RealType value_type;
  127. typedef Policy policy_type;
  128. pareto_distribution(RealType l_scale = 1, RealType l_shape = 1)
  129. : m_scale(l_scale), m_shape(l_shape)
  130. { // Constructor.
  131. RealType result = 0;
  132. detail::check_pareto("boost::math::pareto_distribution<%1%>::pareto_distribution", l_scale, l_shape, &result, Policy());
  133. }
  134. RealType scale()const
  135. { // AKA Xm and Wolfram b and beta
  136. return m_scale;
  137. }
  138. RealType shape()const
  139. { // AKA k and Wolfram a and alpha
  140. return m_shape;
  141. }
  142. private:
  143. // Data members:
  144. RealType m_scale; // distribution scale (xm) or beta
  145. RealType m_shape; // distribution shape (k) or alpha
  146. };
  147. typedef pareto_distribution<double> pareto; // Convenience to allow pareto(2., 3.);
  148. template <class RealType, class Policy>
  149. inline const std::pair<RealType, RealType> range(const pareto_distribution<RealType, Policy>& /*dist*/)
  150. { // Range of permissible values for random variable x.
  151. using boost::math::tools::max_value;
  152. return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>()); // scale zero to + infinity.
  153. } // range
  154. template <class RealType, class Policy>
  155. inline const std::pair<RealType, RealType> support(const pareto_distribution<RealType, Policy>& dist)
  156. { // Range of supported values for random variable x.
  157. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  158. using boost::math::tools::max_value;
  159. return std::pair<RealType, RealType>(dist.scale(), max_value<RealType>() ); // scale to + infinity.
  160. } // support
  161. template <class RealType, class Policy>
  162. inline RealType pdf(const pareto_distribution<RealType, Policy>& dist, const RealType& x)
  163. {
  164. BOOST_MATH_STD_USING // for ADL of std function pow.
  165. static const char* function = "boost::math::pdf(const pareto_distribution<%1%>&, %1%)";
  166. RealType scale = dist.scale();
  167. RealType shape = dist.shape();
  168. RealType result = 0;
  169. if(false == (detail::check_pareto_x(function, x, &result, Policy())
  170. && detail::check_pareto(function, scale, shape, &result, Policy())))
  171. return result;
  172. if (x < scale)
  173. { // regardless of shape, pdf is zero (or should be disallow x < scale and throw an exception?).
  174. return 0;
  175. }
  176. result = shape * pow(scale, shape) / pow(x, shape+1);
  177. return result;
  178. } // pdf
  179. template <class RealType, class Policy>
  180. inline RealType cdf(const pareto_distribution<RealType, Policy>& dist, const RealType& x)
  181. {
  182. BOOST_MATH_STD_USING // for ADL of std function pow.
  183. static const char* function = "boost::math::cdf(const pareto_distribution<%1%>&, %1%)";
  184. RealType scale = dist.scale();
  185. RealType shape = dist.shape();
  186. RealType result = 0;
  187. if(false == (detail::check_pareto_x(function, x, &result, Policy())
  188. && detail::check_pareto(function, scale, shape, &result, Policy())))
  189. return result;
  190. if (x <= scale)
  191. { // regardless of shape, cdf is zero.
  192. return 0;
  193. }
  194. // result = RealType(1) - pow((scale / x), shape);
  195. result = -boost::math::powm1(scale/x, shape, Policy()); // should be more accurate.
  196. return result;
  197. } // cdf
  198. template <class RealType, class Policy>
  199. inline RealType quantile(const pareto_distribution<RealType, Policy>& dist, const RealType& p)
  200. {
  201. BOOST_MATH_STD_USING // for ADL of std function pow.
  202. static const char* function = "boost::math::quantile(const pareto_distribution<%1%>&, %1%)";
  203. RealType result = 0;
  204. RealType scale = dist.scale();
  205. RealType shape = dist.shape();
  206. if(false == (detail::check_probability(function, p, &result, Policy())
  207. && detail::check_pareto(function, scale, shape, &result, Policy())))
  208. {
  209. return result;
  210. }
  211. if (p == 0)
  212. {
  213. return scale; // x must be scale (or less).
  214. }
  215. if (p == 1)
  216. {
  217. return policies::raise_overflow_error<RealType>(function, 0, Policy()); // x = + infinity.
  218. }
  219. result = scale /
  220. (pow((1 - p), 1 / shape));
  221. // K. Krishnamoorthy, ISBN 1-58488-635-8 eq 23.1.3
  222. return result;
  223. } // quantile
  224. template <class RealType, class Policy>
  225. inline RealType cdf(const complemented2_type<pareto_distribution<RealType, Policy>, RealType>& c)
  226. {
  227. BOOST_MATH_STD_USING // for ADL of std function pow.
  228. static const char* function = "boost::math::cdf(const pareto_distribution<%1%>&, %1%)";
  229. RealType result = 0;
  230. RealType x = c.param;
  231. RealType scale = c.dist.scale();
  232. RealType shape = c.dist.shape();
  233. if(false == (detail::check_pareto_x(function, x, &result, Policy())
  234. && detail::check_pareto(function, scale, shape, &result, Policy())))
  235. return result;
  236. if (x <= scale)
  237. { // regardless of shape, cdf is zero, and complement is unity.
  238. return 1;
  239. }
  240. result = pow((scale/x), shape);
  241. return result;
  242. } // cdf complement
  243. template <class RealType, class Policy>
  244. inline RealType quantile(const complemented2_type<pareto_distribution<RealType, Policy>, RealType>& c)
  245. {
  246. BOOST_MATH_STD_USING // for ADL of std function pow.
  247. static const char* function = "boost::math::quantile(const pareto_distribution<%1%>&, %1%)";
  248. RealType result = 0;
  249. RealType q = c.param;
  250. RealType scale = c.dist.scale();
  251. RealType shape = c.dist.shape();
  252. if(false == (detail::check_probability(function, q, &result, Policy())
  253. && detail::check_pareto(function, scale, shape, &result, Policy())))
  254. {
  255. return result;
  256. }
  257. if (q == 1)
  258. {
  259. return scale; // x must be scale (or less).
  260. }
  261. if (q == 0)
  262. {
  263. return policies::raise_overflow_error<RealType>(function, 0, Policy()); // x = + infinity.
  264. }
  265. result = scale / (pow(q, 1 / shape));
  266. // K. Krishnamoorthy, ISBN 1-58488-635-8 eq 23.1.3
  267. return result;
  268. } // quantile complement
  269. template <class RealType, class Policy>
  270. inline RealType mean(const pareto_distribution<RealType, Policy>& dist)
  271. {
  272. RealType result = 0;
  273. static const char* function = "boost::math::mean(const pareto_distribution<%1%>&, %1%)";
  274. if(false == detail::check_pareto(function, dist.scale(), dist.shape(), &result, Policy()))
  275. {
  276. return result;
  277. }
  278. if (dist.shape() > RealType(1))
  279. {
  280. return dist.shape() * dist.scale() / (dist.shape() - 1);
  281. }
  282. else
  283. {
  284. using boost::math::tools::max_value;
  285. return max_value<RealType>(); // +infinity.
  286. }
  287. } // mean
  288. template <class RealType, class Policy>
  289. inline RealType mode(const pareto_distribution<RealType, Policy>& dist)
  290. {
  291. return dist.scale();
  292. } // mode
  293. template <class RealType, class Policy>
  294. inline RealType median(const pareto_distribution<RealType, Policy>& dist)
  295. {
  296. RealType result = 0;
  297. static const char* function = "boost::math::median(const pareto_distribution<%1%>&, %1%)";
  298. if(false == detail::check_pareto(function, dist.scale(), dist.shape(), &result, Policy()))
  299. {
  300. return result;
  301. }
  302. BOOST_MATH_STD_USING
  303. return dist.scale() * pow(RealType(2), (1/dist.shape()));
  304. } // median
  305. template <class RealType, class Policy>
  306. inline RealType variance(const pareto_distribution<RealType, Policy>& dist)
  307. {
  308. RealType result = 0;
  309. RealType scale = dist.scale();
  310. RealType shape = dist.shape();
  311. static const char* function = "boost::math::variance(const pareto_distribution<%1%>&, %1%)";
  312. if(false == detail::check_pareto(function, scale, shape, &result, Policy()))
  313. {
  314. return result;
  315. }
  316. if (shape > 2)
  317. {
  318. result = (scale * scale * shape) /
  319. ((shape - 1) * (shape - 1) * (shape - 2));
  320. }
  321. else
  322. {
  323. result = policies::raise_domain_error<RealType>(
  324. function,
  325. "variance is undefined for shape <= 2, but got %1%.", dist.shape(), Policy());
  326. }
  327. return result;
  328. } // variance
  329. template <class RealType, class Policy>
  330. inline RealType skewness(const pareto_distribution<RealType, Policy>& dist)
  331. {
  332. BOOST_MATH_STD_USING
  333. RealType result = 0;
  334. RealType shape = dist.shape();
  335. static const char* function = "boost::math::pdf(const pareto_distribution<%1%>&, %1%)";
  336. if(false == detail::check_pareto(function, dist.scale(), shape, &result, Policy()))
  337. {
  338. return result;
  339. }
  340. if (shape > 3)
  341. {
  342. result = sqrt((shape - 2) / shape) *
  343. 2 * (shape + 1) /
  344. (shape - 3);
  345. }
  346. else
  347. {
  348. result = policies::raise_domain_error<RealType>(
  349. function,
  350. "skewness is undefined for shape <= 3, but got %1%.", dist.shape(), Policy());
  351. }
  352. return result;
  353. } // skewness
  354. template <class RealType, class Policy>
  355. inline RealType kurtosis(const pareto_distribution<RealType, Policy>& dist)
  356. {
  357. RealType result = 0;
  358. RealType shape = dist.shape();
  359. static const char* function = "boost::math::pdf(const pareto_distribution<%1%>&, %1%)";
  360. if(false == detail::check_pareto(function, dist.scale(), shape, &result, Policy()))
  361. {
  362. return result;
  363. }
  364. if (shape > 4)
  365. {
  366. result = 3 * ((shape - 2) * (3 * shape * shape + shape + 2)) /
  367. (shape * (shape - 3) * (shape - 4));
  368. }
  369. else
  370. {
  371. result = policies::raise_domain_error<RealType>(
  372. function,
  373. "kurtosis_excess is undefined for shape <= 4, but got %1%.", shape, Policy());
  374. }
  375. return result;
  376. } // kurtosis
  377. template <class RealType, class Policy>
  378. inline RealType kurtosis_excess(const pareto_distribution<RealType, Policy>& dist)
  379. {
  380. RealType result = 0;
  381. RealType shape = dist.shape();
  382. static const char* function = "boost::math::pdf(const pareto_distribution<%1%>&, %1%)";
  383. if(false == detail::check_pareto(function, dist.scale(), shape, &result, Policy()))
  384. {
  385. return result;
  386. }
  387. if (shape > 4)
  388. {
  389. result = 6 * ((shape * shape * shape) + (shape * shape) - 6 * shape - 2) /
  390. (shape * (shape - 3) * (shape - 4));
  391. }
  392. else
  393. {
  394. result = policies::raise_domain_error<RealType>(
  395. function,
  396. "kurtosis_excess is undefined for shape <= 4, but got %1%.", dist.shape(), Policy());
  397. }
  398. return result;
  399. } // kurtosis_excess
  400. } // namespace math
  401. } // namespace boost
  402. // This include must be at the end, *after* the accessors
  403. // for this distribution have been defined, in order to
  404. // keep compilers that support two-phase lookup happy.
  405. #include <boost/math/distributions/detail/derived_accessors.hpp>
  406. #endif // BOOST_STATS_PARETO_HPP