extreme_value.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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(0 == detail::verify_scale_b(function, b, &result, Policy()))
  86. return result;
  87. if(0 == detail::check_finite(function, a, &result, Policy()))
  88. return result;
  89. if((boost::math::isinf)(x))
  90. return 0.0f;
  91. if(0 == detail::check_x(function, x, &result, Policy()))
  92. return result;
  93. RealType e = (a - x) / b;
  94. if(e < tools::log_max_value<RealType>())
  95. result = exp(e) * exp(-exp(e)) / b;
  96. // else.... result *must* be zero since exp(e) is infinite...
  97. return result;
  98. } // pdf
  99. template <class RealType, class Policy>
  100. inline RealType cdf(const extreme_value_distribution<RealType, Policy>& dist, const RealType& x)
  101. {
  102. BOOST_MATH_STD_USING // for ADL of std functions
  103. static const char* function = "boost::math::cdf(const extreme_value_distribution<%1%>&, %1%)";
  104. if((boost::math::isinf)(x))
  105. return x < 0 ? 0.0f : 1.0f;
  106. RealType a = dist.location();
  107. RealType b = dist.scale();
  108. RealType result = 0;
  109. if(0 == detail::verify_scale_b(function, b, &result, Policy()))
  110. return result;
  111. if(0 == detail::check_finite(function, a, &result, Policy()))
  112. return result;
  113. if(0 == detail::check_finite(function, a, &result, Policy()))
  114. return result;
  115. if(0 == detail::check_x("boost::math::cdf(const extreme_value_distribution<%1%>&, %1%)", x, &result, Policy()))
  116. return result;
  117. result = exp(-exp((a-x)/b));
  118. return result;
  119. } // cdf
  120. template <class RealType, class Policy>
  121. RealType quantile(const extreme_value_distribution<RealType, Policy>& dist, const RealType& p)
  122. {
  123. BOOST_MATH_STD_USING // for ADL of std functions
  124. static const char* function = "boost::math::quantile(const extreme_value_distribution<%1%>&, %1%)";
  125. RealType a = dist.location();
  126. RealType b = dist.scale();
  127. RealType result = 0;
  128. if(0 == detail::verify_scale_b(function, b, &result, Policy()))
  129. return result;
  130. if(0 == detail::check_finite(function, a, &result, Policy()))
  131. return result;
  132. if(0 == detail::check_probability(function, p, &result, Policy()))
  133. return result;
  134. if(p == 0)
  135. return -policies::raise_overflow_error<RealType>(function, 0, Policy());
  136. if(p == 1)
  137. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  138. result = a - log(-log(p)) * b;
  139. return result;
  140. } // quantile
  141. template <class RealType, class Policy>
  142. inline RealType cdf(const complemented2_type<extreme_value_distribution<RealType, Policy>, RealType>& c)
  143. {
  144. BOOST_MATH_STD_USING // for ADL of std functions
  145. static const char* function = "boost::math::cdf(const extreme_value_distribution<%1%>&, %1%)";
  146. if((boost::math::isinf)(c.param))
  147. return c.param < 0 ? 1.0f : 0.0f;
  148. RealType a = c.dist.location();
  149. RealType b = c.dist.scale();
  150. RealType result = 0;
  151. if(0 == detail::verify_scale_b(function, b, &result, Policy()))
  152. return result;
  153. if(0 == detail::check_finite(function, a, &result, Policy()))
  154. return result;
  155. if(0 == detail::check_x(function, c.param, &result, Policy()))
  156. return result;
  157. result = -boost::math::expm1(-exp((a-c.param)/b), Policy());
  158. return result;
  159. }
  160. template <class RealType, class Policy>
  161. RealType quantile(const complemented2_type<extreme_value_distribution<RealType, Policy>, RealType>& c)
  162. {
  163. BOOST_MATH_STD_USING // for ADL of std functions
  164. static const char* function = "boost::math::quantile(const extreme_value_distribution<%1%>&, %1%)";
  165. RealType a = c.dist.location();
  166. RealType b = c.dist.scale();
  167. RealType q = c.param;
  168. RealType result = 0;
  169. if(0 == detail::verify_scale_b(function, b, &result, Policy()))
  170. return result;
  171. if(0 == detail::check_finite(function, a, &result, Policy()))
  172. return result;
  173. if(0 == detail::check_probability(function, q, &result, Policy()))
  174. return result;
  175. if(q == 0)
  176. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  177. if(q == 1)
  178. return -policies::raise_overflow_error<RealType>(function, 0, Policy());
  179. result = a - log(-boost::math::log1p(-q, Policy())) * b;
  180. return result;
  181. }
  182. template <class RealType, class Policy>
  183. inline RealType mean(const extreme_value_distribution<RealType, Policy>& dist)
  184. {
  185. RealType a = dist.location();
  186. RealType b = dist.scale();
  187. RealType result = 0;
  188. if(0 == detail::verify_scale_b("boost::math::mean(const extreme_value_distribution<%1%>&)", b, &result, Policy()))
  189. return result;
  190. if (0 == detail::check_finite("boost::math::mean(const extreme_value_distribution<%1%>&)", a, &result, Policy()))
  191. return result;
  192. return a + constants::euler<RealType>() * b;
  193. }
  194. template <class RealType, class Policy>
  195. inline RealType standard_deviation(const extreme_value_distribution<RealType, Policy>& dist)
  196. {
  197. BOOST_MATH_STD_USING // for ADL of std functions.
  198. RealType b = dist.scale();
  199. RealType result = 0;
  200. if(0 == detail::verify_scale_b("boost::math::standard_deviation(const extreme_value_distribution<%1%>&)", b, &result, Policy()))
  201. return result;
  202. if(0 == detail::check_finite("boost::math::standard_deviation(const extreme_value_distribution<%1%>&)", dist.location(), &result, Policy()))
  203. return result;
  204. return constants::pi<RealType>() * b / sqrt(static_cast<RealType>(6));
  205. }
  206. template <class RealType, class Policy>
  207. inline RealType mode(const extreme_value_distribution<RealType, Policy>& dist)
  208. {
  209. return dist.location();
  210. }
  211. template <class RealType, class Policy>
  212. inline RealType median(const extreme_value_distribution<RealType, Policy>& dist)
  213. {
  214. using constants::ln_ln_two;
  215. return dist.location() - dist.scale() * ln_ln_two<RealType>();
  216. }
  217. template <class RealType, class Policy>
  218. inline RealType skewness(const extreme_value_distribution<RealType, Policy>& /*dist*/)
  219. {
  220. //
  221. // This is 12 * sqrt(6) * zeta(3) / pi^3:
  222. // See http://mathworld.wolfram.com/ExtremeValueDistribution.html
  223. //
  224. return static_cast<RealType>(1.1395470994046486574927930193898461120875997958366L);
  225. }
  226. template <class RealType, class Policy>
  227. inline RealType kurtosis(const extreme_value_distribution<RealType, Policy>& /*dist*/)
  228. {
  229. // See http://mathworld.wolfram.com/ExtremeValueDistribution.html
  230. return RealType(27) / 5;
  231. }
  232. template <class RealType, class Policy>
  233. inline RealType kurtosis_excess(const extreme_value_distribution<RealType, Policy>& /*dist*/)
  234. {
  235. // See http://mathworld.wolfram.com/ExtremeValueDistribution.html
  236. return RealType(12) / 5;
  237. }
  238. } // namespace math
  239. } // namespace boost
  240. #ifdef BOOST_MSVC
  241. # pragma warning(pop)
  242. #endif
  243. // This include must be at the end, *after* the accessors
  244. // for this distribution have been defined, in order to
  245. // keep compilers that support two-phase lookup happy.
  246. #include <boost/math/distributions/detail/derived_accessors.hpp>
  247. #endif // BOOST_STATS_EXTREME_VALUE_HPP