extreme_value.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. #ifndef BOOST_STATS_EXTREME_VALUE_HPP
  6. #define BOOST_STATS_EXTREME_VALUE_HPP
  7. #include <boost/math/distributions/fwd.hpp>
  8. #include <boost/math/constants/constants.hpp>
  9. #include <boost/math/special_functions/log1p.hpp>
  10. #include <boost/math/special_functions/expm1.hpp>
  11. #include <boost/math/distributions/complement.hpp>
  12. #include <boost/math/distributions/detail/common_error_handling.hpp>
  13. #include <boost/config/no_tr1/cmath.hpp>
  14. //
  15. // This is the maximum extreme value distribution, see
  16. // http://www.itl.nist.gov/div898/handbook/eda/section3/eda366g.htm
  17. // and http://mathworld.wolfram.com/ExtremeValueDistribution.html
  18. // Also known as a Fisher-Tippett distribution, a log-Weibull
  19. // distribution or a Gumbel distribution.
  20. #include <utility>
  21. #ifdef BOOST_MSVC
  22. # pragma warning(push)
  23. # pragma warning(disable: 4702) // unreachable code (return after domain_error throw).
  24. #endif
  25. namespace boost{ namespace math{
  26. namespace detail{
  27. //
  28. // Error check:
  29. //
  30. template <class RealType, class Policy>
  31. inline bool verify_scale_b(const char* function, RealType b, RealType* presult, const Policy& pol)
  32. {
  33. if((b <= 0) || !(boost::math::isfinite)(b))
  34. {
  35. *presult = policies::raise_domain_error<RealType>(
  36. function,
  37. "The scale parameter \"b\" must be finite and > 0, but was: %1%.", b, pol);
  38. return false;
  39. }
  40. return true;
  41. }
  42. } // namespace detail
  43. template <class RealType = double, class Policy = policies::policy<> >
  44. class extreme_value_distribution
  45. {
  46. public:
  47. typedef RealType value_type;
  48. typedef Policy policy_type;
  49. extreme_value_distribution(RealType a = 0, RealType b = 1)
  50. : m_a(a), m_b(b)
  51. {
  52. RealType err;
  53. detail::verify_scale_b("boost::math::extreme_value_distribution<%1%>::extreme_value_distribution", b, &err, Policy());
  54. detail::check_finite("boost::math::extreme_value_distribution<%1%>::extreme_value_distribution", a, &err, Policy());
  55. } // extreme_value_distribution
  56. RealType location()const { return m_a; }
  57. RealType scale()const { return m_b; }
  58. private:
  59. RealType m_a, m_b;
  60. };
  61. typedef extreme_value_distribution<double> extreme_value;
  62. template <class RealType, class Policy>
  63. inline const std::pair<RealType, RealType> range(const extreme_value_distribution<RealType, Policy>& /*dist*/)
  64. { // Range of permissible values for random variable x.
  65. using boost::math::tools::max_value;
  66. return std::pair<RealType, RealType>(
  67. std::numeric_limits<RealType>::has_infinity ? -std::numeric_limits<RealType>::infinity() : -max_value<RealType>(),
  68. std::numeric_limits<RealType>::has_infinity ? std::numeric_limits<RealType>::infinity() : max_value<RealType>());
  69. }
  70. template <class RealType, class Policy>
  71. inline const std::pair<RealType, RealType> support(const extreme_value_distribution<RealType, Policy>& /*dist*/)
  72. { // Range of supported values for random variable x.
  73. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  74. using boost::math::tools::max_value;
  75. return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>());
  76. }
  77. template <class RealType, class Policy>
  78. inline RealType pdf(const extreme_value_distribution<RealType, Policy>& dist, const RealType& x)
  79. {
  80. BOOST_MATH_STD_USING // for ADL of std functions
  81. static const char* function = "boost::math::pdf(const extreme_value_distribution<%1%>&, %1%)";
  82. RealType a = dist.location();
  83. RealType b = dist.scale();
  84. RealType result = 0;
  85. if((boost::math::isinf)(x))
  86. return 0.0f;
  87. if(0 == detail::verify_scale_b(function, b, &result, Policy()))
  88. return result;
  89. if(0 == detail::check_finite(function, a, &result, Policy()))
  90. return result;
  91. if(0 == detail::check_x(function, x, &result, Policy()))
  92. return result;
  93. result = exp((a-x)/b) * exp(-exp((a-x)/b)) / b;
  94. return result;
  95. } // pdf
  96. template <class RealType, class Policy>
  97. inline RealType cdf(const extreme_value_distribution<RealType, Policy>& dist, const RealType& x)
  98. {
  99. BOOST_MATH_STD_USING // for ADL of std functions
  100. static const char* function = "boost::math::cdf(const extreme_value_distribution<%1%>&, %1%)";
  101. if((boost::math::isinf)(x))
  102. return x < 0 ? 0.0f : 1.0f;
  103. RealType a = dist.location();
  104. RealType b = dist.scale();
  105. RealType result = 0;
  106. if(0 == detail::verify_scale_b(function, b, &result, Policy()))
  107. return result;
  108. if(0 == detail::check_finite(function, a, &result, Policy()))
  109. return result;
  110. if(0 == detail::check_finite(function, a, &result, Policy()))
  111. return result;
  112. if(0 == detail::check_x("boost::math::cdf(const extreme_value_distribution<%1%>&, %1%)", x, &result, Policy()))
  113. return result;
  114. result = exp(-exp((a-x)/b));
  115. return result;
  116. } // cdf
  117. template <class RealType, class Policy>
  118. RealType quantile(const extreme_value_distribution<RealType, Policy>& dist, const RealType& p)
  119. {
  120. BOOST_MATH_STD_USING // for ADL of std functions
  121. static const char* function = "boost::math::quantile(const extreme_value_distribution<%1%>&, %1%)";
  122. RealType a = dist.location();
  123. RealType b = dist.scale();
  124. RealType result = 0;
  125. if(0 == detail::verify_scale_b(function, b, &result, Policy()))
  126. return result;
  127. if(0 == detail::check_finite(function, a, &result, Policy()))
  128. return result;
  129. if(0 == detail::check_probability(function, p, &result, Policy()))
  130. return result;
  131. if(p == 0)
  132. return -policies::raise_overflow_error<RealType>(function, 0, Policy());
  133. if(p == 1)
  134. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  135. result = a - log(-log(p)) * b;
  136. return result;
  137. } // quantile
  138. template <class RealType, class Policy>
  139. inline RealType cdf(const complemented2_type<extreme_value_distribution<RealType, Policy>, RealType>& c)
  140. {
  141. BOOST_MATH_STD_USING // for ADL of std functions
  142. static const char* function = "boost::math::cdf(const extreme_value_distribution<%1%>&, %1%)";
  143. if((boost::math::isinf)(c.param))
  144. return c.param < 0 ? 1.0f : 0.0f;
  145. RealType a = c.dist.location();
  146. RealType b = c.dist.scale();
  147. RealType result = 0;
  148. if(0 == detail::verify_scale_b(function, b, &result, Policy()))
  149. return result;
  150. if(0 == detail::check_finite(function, a, &result, Policy()))
  151. return result;
  152. if(0 == detail::check_x(function, c.param, &result, Policy()))
  153. return result;
  154. result = -boost::math::expm1(-exp((a-c.param)/b), Policy());
  155. return result;
  156. }
  157. template <class RealType, class Policy>
  158. RealType quantile(const complemented2_type<extreme_value_distribution<RealType, Policy>, RealType>& c)
  159. {
  160. BOOST_MATH_STD_USING // for ADL of std functions
  161. static const char* function = "boost::math::quantile(const extreme_value_distribution<%1%>&, %1%)";
  162. RealType a = c.dist.location();
  163. RealType b = c.dist.scale();
  164. RealType q = c.param;
  165. RealType result = 0;
  166. if(0 == detail::verify_scale_b(function, b, &result, Policy()))
  167. return result;
  168. if(0 == detail::check_finite(function, a, &result, Policy()))
  169. return result;
  170. if(0 == detail::check_probability(function, q, &result, Policy()))
  171. return result;
  172. if(q == 0)
  173. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  174. if(q == 1)
  175. return -policies::raise_overflow_error<RealType>(function, 0, Policy());
  176. result = a - log(-boost::math::log1p(-q, Policy())) * b;
  177. return result;
  178. }
  179. template <class RealType, class Policy>
  180. inline RealType mean(const extreme_value_distribution<RealType, Policy>& dist)
  181. {
  182. RealType a = dist.location();
  183. RealType b = dist.scale();
  184. RealType result = 0;
  185. if(0 == detail::verify_scale_b("boost::math::mean(const extreme_value_distribution<%1%>&)", b, &result, Policy()))
  186. return result;
  187. if(0 == detail::check_scale("boost::math::mean(const extreme_value_distribution<%1%>&)", a, &result, Policy()))
  188. return result;
  189. return a + constants::euler<RealType>() * b;
  190. }
  191. template <class RealType, class Policy>
  192. inline RealType standard_deviation(const extreme_value_distribution<RealType, Policy>& dist)
  193. {
  194. BOOST_MATH_STD_USING // for ADL of std functions.
  195. RealType b = dist.scale();
  196. RealType result = 0;
  197. if(0 == detail::verify_scale_b("boost::math::standard_deviation(const extreme_value_distribution<%1%>&)", b, &result, Policy()))
  198. return result;
  199. if(0 == detail::check_scale("boost::math::standard_deviation(const extreme_value_distribution<%1%>&)", dist.location(), &result, Policy()))
  200. return result;
  201. return constants::pi<RealType>() * b / sqrt(static_cast<RealType>(6));
  202. }
  203. template <class RealType, class Policy>
  204. inline RealType mode(const extreme_value_distribution<RealType, Policy>& dist)
  205. {
  206. return dist.location();
  207. }
  208. template <class RealType, class Policy>
  209. inline RealType median(const extreme_value_distribution<RealType, Policy>& dist)
  210. {
  211. using constants::ln_ln_two;
  212. return dist.location() - dist.scale() * ln_ln_two<RealType>();
  213. }
  214. template <class RealType, class Policy>
  215. inline RealType skewness(const extreme_value_distribution<RealType, Policy>& /*dist*/)
  216. {
  217. //
  218. // This is 12 * sqrt(6) * zeta(3) / pi^3:
  219. // See http://mathworld.wolfram.com/ExtremeValueDistribution.html
  220. //
  221. return static_cast<RealType>(1.1395470994046486574927930193898461120875997958366L);
  222. }
  223. template <class RealType, class Policy>
  224. inline RealType kurtosis(const extreme_value_distribution<RealType, Policy>& /*dist*/)
  225. {
  226. // See http://mathworld.wolfram.com/ExtremeValueDistribution.html
  227. return RealType(27) / 5;
  228. }
  229. template <class RealType, class Policy>
  230. inline RealType kurtosis_excess(const extreme_value_distribution<RealType, Policy>& /*dist*/)
  231. {
  232. // See http://mathworld.wolfram.com/ExtremeValueDistribution.html
  233. return RealType(12) / 5;
  234. }
  235. } // namespace math
  236. } // namespace boost
  237. #ifdef BOOST_MSVC
  238. # pragma warning(pop)
  239. #endif
  240. // This include must be at the end, *after* the accessors
  241. // for this distribution have been defined, in order to
  242. // keep compilers that support two-phase lookup happy.
  243. #include <boost/math/distributions/detail/derived_accessors.hpp>
  244. #endif // BOOST_STATS_EXTREME_VALUE_HPP