extreme_value.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. //
  14. // This is the maximum extreme value distribution, see
  15. // http://www.itl.nist.gov/div898/handbook/eda/section3/eda366g.htm
  16. // and http://mathworld.wolfram.com/ExtremeValueDistribution.html
  17. // Also known as a Fisher-Tippett distribution, a log-Weibull
  18. // distribution or a Gumbel distribution.
  19. #include <utility>
  20. #include <cmath>
  21. #ifdef _MSC_VER
  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. using value_type = RealType;
  48. using policy_type = Policy;
  49. explicit 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;
  60. RealType m_b;
  61. };
  62. using extreme_value = extreme_value_distribution<double>;
  63. #ifdef __cpp_deduction_guides
  64. template <class RealType>
  65. extreme_value_distribution(RealType)->extreme_value_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  66. template <class RealType>
  67. extreme_value_distribution(RealType,RealType)->extreme_value_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  68. #endif
  69. template <class RealType, class Policy>
  70. inline std::pair<RealType, RealType> range(const extreme_value_distribution<RealType, Policy>& /*dist*/)
  71. { // Range of permissible values for random variable x.
  72. using boost::math::tools::max_value;
  73. return std::pair<RealType, RealType>(
  74. std::numeric_limits<RealType>::has_infinity ? -std::numeric_limits<RealType>::infinity() : -max_value<RealType>(),
  75. std::numeric_limits<RealType>::has_infinity ? std::numeric_limits<RealType>::infinity() : max_value<RealType>());
  76. }
  77. template <class RealType, class Policy>
  78. inline std::pair<RealType, RealType> support(const extreme_value_distribution<RealType, Policy>& /*dist*/)
  79. { // Range of supported values for random variable x.
  80. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  81. using boost::math::tools::max_value;
  82. return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>());
  83. }
  84. template <class RealType, class Policy>
  85. inline RealType pdf(const extreme_value_distribution<RealType, Policy>& dist, const RealType& x)
  86. {
  87. BOOST_MATH_STD_USING // for ADL of std functions
  88. static const char* function = "boost::math::pdf(const extreme_value_distribution<%1%>&, %1%)";
  89. RealType a = dist.location();
  90. RealType b = dist.scale();
  91. RealType result = 0;
  92. if(0 == detail::verify_scale_b(function, b, &result, Policy()))
  93. return result;
  94. if(0 == detail::check_finite(function, a, &result, Policy()))
  95. return result;
  96. if((boost::math::isinf)(x))
  97. return 0.0f;
  98. if(0 == detail::check_x(function, x, &result, Policy()))
  99. return result;
  100. RealType e = (a - x) / b;
  101. if(e < tools::log_max_value<RealType>())
  102. result = exp(e) * exp(-exp(e)) / b;
  103. // else.... result *must* be zero since exp(e) is infinite...
  104. return result;
  105. } // pdf
  106. template <class RealType, class Policy>
  107. inline RealType logpdf(const extreme_value_distribution<RealType, Policy>& dist, const RealType& x)
  108. {
  109. BOOST_MATH_STD_USING // for ADL of std functions
  110. static const char* function = "boost::math::logpdf(const extreme_value_distribution<%1%>&, %1%)";
  111. RealType a = dist.location();
  112. RealType b = dist.scale();
  113. RealType result = -std::numeric_limits<RealType>::infinity();
  114. if(0 == detail::verify_scale_b(function, b, &result, Policy()))
  115. return result;
  116. if(0 == detail::check_finite(function, a, &result, Policy()))
  117. return result;
  118. if((boost::math::isinf)(x))
  119. return 0.0f;
  120. if(0 == detail::check_x(function, x, &result, Policy()))
  121. return result;
  122. RealType e = (a - x) / b;
  123. if(e < tools::log_max_value<RealType>())
  124. result = log(1/b) + e - exp(e);
  125. // else.... result *must* be zero since exp(e) is infinite...
  126. return result;
  127. } // logpdf
  128. template <class RealType, class Policy>
  129. inline RealType cdf(const extreme_value_distribution<RealType, Policy>& dist, const RealType& x)
  130. {
  131. BOOST_MATH_STD_USING // for ADL of std functions
  132. static const char* function = "boost::math::cdf(const extreme_value_distribution<%1%>&, %1%)";
  133. if((boost::math::isinf)(x))
  134. return x < 0 ? 0.0f : 1.0f;
  135. RealType a = dist.location();
  136. RealType b = dist.scale();
  137. RealType result = 0;
  138. if(0 == detail::verify_scale_b(function, b, &result, Policy()))
  139. return result;
  140. if(0 == detail::check_finite(function, a, &result, Policy()))
  141. return result;
  142. if(0 == detail::check_finite(function, a, &result, Policy()))
  143. return result;
  144. if(0 == detail::check_x("boost::math::cdf(const extreme_value_distribution<%1%>&, %1%)", x, &result, Policy()))
  145. return result;
  146. result = exp(-exp((a-x)/b));
  147. return result;
  148. } // cdf
  149. template <class RealType, class Policy>
  150. RealType quantile(const extreme_value_distribution<RealType, Policy>& dist, const RealType& p)
  151. {
  152. BOOST_MATH_STD_USING // for ADL of std functions
  153. static const char* function = "boost::math::quantile(const extreme_value_distribution<%1%>&, %1%)";
  154. RealType a = dist.location();
  155. RealType b = dist.scale();
  156. RealType result = 0;
  157. if(0 == detail::verify_scale_b(function, b, &result, Policy()))
  158. return result;
  159. if(0 == detail::check_finite(function, a, &result, Policy()))
  160. return result;
  161. if(0 == detail::check_probability(function, p, &result, Policy()))
  162. return result;
  163. if(p == 0)
  164. return -policies::raise_overflow_error<RealType>(function, 0, Policy());
  165. if(p == 1)
  166. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  167. result = a - log(-log(p)) * b;
  168. return result;
  169. } // quantile
  170. template <class RealType, class Policy>
  171. inline RealType cdf(const complemented2_type<extreme_value_distribution<RealType, Policy>, RealType>& c)
  172. {
  173. BOOST_MATH_STD_USING // for ADL of std functions
  174. static const char* function = "boost::math::cdf(const extreme_value_distribution<%1%>&, %1%)";
  175. if((boost::math::isinf)(c.param))
  176. return c.param < 0 ? 1.0f : 0.0f;
  177. RealType a = c.dist.location();
  178. RealType b = c.dist.scale();
  179. RealType result = 0;
  180. if(0 == detail::verify_scale_b(function, b, &result, Policy()))
  181. return result;
  182. if(0 == detail::check_finite(function, a, &result, Policy()))
  183. return result;
  184. if(0 == detail::check_x(function, c.param, &result, Policy()))
  185. return result;
  186. result = -boost::math::expm1(-exp((a-c.param)/b), Policy());
  187. return result;
  188. }
  189. template <class RealType, class Policy>
  190. RealType quantile(const complemented2_type<extreme_value_distribution<RealType, Policy>, RealType>& c)
  191. {
  192. BOOST_MATH_STD_USING // for ADL of std functions
  193. static const char* function = "boost::math::quantile(const extreme_value_distribution<%1%>&, %1%)";
  194. RealType a = c.dist.location();
  195. RealType b = c.dist.scale();
  196. RealType q = c.param;
  197. RealType result = 0;
  198. if(0 == detail::verify_scale_b(function, b, &result, Policy()))
  199. return result;
  200. if(0 == detail::check_finite(function, a, &result, Policy()))
  201. return result;
  202. if(0 == detail::check_probability(function, q, &result, Policy()))
  203. return result;
  204. if(q == 0)
  205. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  206. if(q == 1)
  207. return -policies::raise_overflow_error<RealType>(function, 0, Policy());
  208. result = a - log(-boost::math::log1p(-q, Policy())) * b;
  209. return result;
  210. }
  211. template <class RealType, class Policy>
  212. inline RealType mean(const extreme_value_distribution<RealType, Policy>& dist)
  213. {
  214. RealType a = dist.location();
  215. RealType b = dist.scale();
  216. RealType result = 0;
  217. if(0 == detail::verify_scale_b("boost::math::mean(const extreme_value_distribution<%1%>&)", b, &result, Policy()))
  218. return result;
  219. if (0 == detail::check_finite("boost::math::mean(const extreme_value_distribution<%1%>&)", a, &result, Policy()))
  220. return result;
  221. return a + constants::euler<RealType>() * b;
  222. }
  223. template <class RealType, class Policy>
  224. inline RealType standard_deviation(const extreme_value_distribution<RealType, Policy>& dist)
  225. {
  226. BOOST_MATH_STD_USING // for ADL of std functions.
  227. RealType b = dist.scale();
  228. RealType result = 0;
  229. if(0 == detail::verify_scale_b("boost::math::standard_deviation(const extreme_value_distribution<%1%>&)", b, &result, Policy()))
  230. return result;
  231. if(0 == detail::check_finite("boost::math::standard_deviation(const extreme_value_distribution<%1%>&)", dist.location(), &result, Policy()))
  232. return result;
  233. return constants::pi<RealType>() * b / sqrt(static_cast<RealType>(6));
  234. }
  235. template <class RealType, class Policy>
  236. inline RealType mode(const extreme_value_distribution<RealType, Policy>& dist)
  237. {
  238. return dist.location();
  239. }
  240. template <class RealType, class Policy>
  241. inline RealType median(const extreme_value_distribution<RealType, Policy>& dist)
  242. {
  243. using constants::ln_ln_two;
  244. return dist.location() - dist.scale() * ln_ln_two<RealType>();
  245. }
  246. template <class RealType, class Policy>
  247. inline RealType skewness(const extreme_value_distribution<RealType, Policy>& /*dist*/)
  248. {
  249. //
  250. // This is 12 * sqrt(6) * zeta(3) / pi^3:
  251. // See http://mathworld.wolfram.com/ExtremeValueDistribution.html
  252. //
  253. return static_cast<RealType>(1.1395470994046486574927930193898461120875997958366L);
  254. }
  255. template <class RealType, class Policy>
  256. inline RealType kurtosis(const extreme_value_distribution<RealType, Policy>& /*dist*/)
  257. {
  258. // See http://mathworld.wolfram.com/ExtremeValueDistribution.html
  259. return RealType(27) / 5;
  260. }
  261. template <class RealType, class Policy>
  262. inline RealType kurtosis_excess(const extreme_value_distribution<RealType, Policy>& /*dist*/)
  263. {
  264. // See http://mathworld.wolfram.com/ExtremeValueDistribution.html
  265. return RealType(12) / 5;
  266. }
  267. } // namespace math
  268. } // namespace boost
  269. #ifdef _MSC_VER
  270. # pragma warning(pop)
  271. #endif
  272. // This include must be at the end, *after* the accessors
  273. // for this distribution have been defined, in order to
  274. // keep compilers that support two-phase lookup happy.
  275. #include <boost/math/distributions/detail/derived_accessors.hpp>
  276. #endif // BOOST_STATS_EXTREME_VALUE_HPP