extreme_value.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. inline RealType logcdf(const extreme_value_distribution<RealType, Policy>& dist, const RealType& x)
  151. {
  152. BOOST_MATH_STD_USING // for ADL of std functions
  153. static const char* function = "boost::math::logcdf(const extreme_value_distribution<%1%>&, %1%)";
  154. if((boost::math::isinf)(x))
  155. return x < 0 ? 0.0f : 1.0f;
  156. RealType a = dist.location();
  157. RealType b = dist.scale();
  158. RealType result = 0;
  159. if(0 == detail::verify_scale_b(function, b, &result, Policy()))
  160. return result;
  161. if(0 == detail::check_finite(function, a, &result, Policy()))
  162. return result;
  163. if(0 == detail::check_finite(function, a, &result, Policy()))
  164. return result;
  165. if(0 == detail::check_x("boost::math::logcdf(const extreme_value_distribution<%1%>&, %1%)", x, &result, Policy()))
  166. return result;
  167. result = -exp((a-x)/b);
  168. return result;
  169. } // logcdf
  170. template <class RealType, class Policy>
  171. RealType quantile(const extreme_value_distribution<RealType, Policy>& dist, const RealType& p)
  172. {
  173. BOOST_MATH_STD_USING // for ADL of std functions
  174. static const char* function = "boost::math::quantile(const extreme_value_distribution<%1%>&, %1%)";
  175. RealType a = dist.location();
  176. RealType b = dist.scale();
  177. RealType result = 0;
  178. if(0 == detail::verify_scale_b(function, b, &result, Policy()))
  179. return result;
  180. if(0 == detail::check_finite(function, a, &result, Policy()))
  181. return result;
  182. if(0 == detail::check_probability(function, p, &result, Policy()))
  183. return result;
  184. if(p == 0)
  185. return -policies::raise_overflow_error<RealType>(function, 0, Policy());
  186. if(p == 1)
  187. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  188. result = a - log(-log(p)) * b;
  189. return result;
  190. } // quantile
  191. template <class RealType, class Policy>
  192. inline RealType cdf(const complemented2_type<extreme_value_distribution<RealType, Policy>, RealType>& c)
  193. {
  194. BOOST_MATH_STD_USING // for ADL of std functions
  195. static const char* function = "boost::math::cdf(const extreme_value_distribution<%1%>&, %1%)";
  196. if((boost::math::isinf)(c.param))
  197. return c.param < 0 ? 1.0f : 0.0f;
  198. RealType a = c.dist.location();
  199. RealType b = c.dist.scale();
  200. RealType result = 0;
  201. if(0 == detail::verify_scale_b(function, b, &result, Policy()))
  202. return result;
  203. if(0 == detail::check_finite(function, a, &result, Policy()))
  204. return result;
  205. if(0 == detail::check_x(function, c.param, &result, Policy()))
  206. return result;
  207. result = -boost::math::expm1(-exp((a-c.param)/b), Policy());
  208. return result;
  209. }
  210. template <class RealType, class Policy>
  211. inline RealType logcdf(const complemented2_type<extreme_value_distribution<RealType, Policy>, RealType>& c)
  212. {
  213. BOOST_MATH_STD_USING // for ADL of std functions
  214. static const char* function = "boost::math::logcdf(const extreme_value_distribution<%1%>&, %1%)";
  215. if((boost::math::isinf)(c.param))
  216. return c.param < 0 ? 1.0f : 0.0f;
  217. RealType a = c.dist.location();
  218. RealType b = c.dist.scale();
  219. RealType result = 0;
  220. if(0 == detail::verify_scale_b(function, b, &result, Policy()))
  221. return result;
  222. if(0 == detail::check_finite(function, a, &result, Policy()))
  223. return result;
  224. if(0 == detail::check_x(function, c.param, &result, Policy()))
  225. return result;
  226. result = log1p(-exp(-exp((a-c.param)/b)), Policy());
  227. return result;
  228. }
  229. template <class RealType, class Policy>
  230. RealType quantile(const complemented2_type<extreme_value_distribution<RealType, Policy>, RealType>& c)
  231. {
  232. BOOST_MATH_STD_USING // for ADL of std functions
  233. static const char* function = "boost::math::quantile(const extreme_value_distribution<%1%>&, %1%)";
  234. RealType a = c.dist.location();
  235. RealType b = c.dist.scale();
  236. RealType q = c.param;
  237. RealType result = 0;
  238. if(0 == detail::verify_scale_b(function, b, &result, Policy()))
  239. return result;
  240. if(0 == detail::check_finite(function, a, &result, Policy()))
  241. return result;
  242. if(0 == detail::check_probability(function, q, &result, Policy()))
  243. return result;
  244. if(q == 0)
  245. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  246. if(q == 1)
  247. return -policies::raise_overflow_error<RealType>(function, 0, Policy());
  248. result = a - log(-boost::math::log1p(-q, Policy())) * b;
  249. return result;
  250. }
  251. template <class RealType, class Policy>
  252. inline RealType mean(const extreme_value_distribution<RealType, Policy>& dist)
  253. {
  254. RealType a = dist.location();
  255. RealType b = dist.scale();
  256. RealType result = 0;
  257. if(0 == detail::verify_scale_b("boost::math::mean(const extreme_value_distribution<%1%>&)", b, &result, Policy()))
  258. return result;
  259. if (0 == detail::check_finite("boost::math::mean(const extreme_value_distribution<%1%>&)", a, &result, Policy()))
  260. return result;
  261. return a + constants::euler<RealType>() * b;
  262. }
  263. template <class RealType, class Policy>
  264. inline RealType standard_deviation(const extreme_value_distribution<RealType, Policy>& dist)
  265. {
  266. BOOST_MATH_STD_USING // for ADL of std functions.
  267. RealType b = dist.scale();
  268. RealType result = 0;
  269. if(0 == detail::verify_scale_b("boost::math::standard_deviation(const extreme_value_distribution<%1%>&)", b, &result, Policy()))
  270. return result;
  271. if(0 == detail::check_finite("boost::math::standard_deviation(const extreme_value_distribution<%1%>&)", dist.location(), &result, Policy()))
  272. return result;
  273. return constants::pi<RealType>() * b / sqrt(static_cast<RealType>(6));
  274. }
  275. template <class RealType, class Policy>
  276. inline RealType mode(const extreme_value_distribution<RealType, Policy>& dist)
  277. {
  278. return dist.location();
  279. }
  280. template <class RealType, class Policy>
  281. inline RealType median(const extreme_value_distribution<RealType, Policy>& dist)
  282. {
  283. using constants::ln_ln_two;
  284. return dist.location() - dist.scale() * ln_ln_two<RealType>();
  285. }
  286. template <class RealType, class Policy>
  287. inline RealType skewness(const extreme_value_distribution<RealType, Policy>& /*dist*/)
  288. {
  289. //
  290. // This is 12 * sqrt(6) * zeta(3) / pi^3:
  291. // See http://mathworld.wolfram.com/ExtremeValueDistribution.html
  292. //
  293. return static_cast<RealType>(1.1395470994046486574927930193898461120875997958366L);
  294. }
  295. template <class RealType, class Policy>
  296. inline RealType kurtosis(const extreme_value_distribution<RealType, Policy>& /*dist*/)
  297. {
  298. // See http://mathworld.wolfram.com/ExtremeValueDistribution.html
  299. return RealType(27) / 5;
  300. }
  301. template <class RealType, class Policy>
  302. inline RealType kurtosis_excess(const extreme_value_distribution<RealType, Policy>& /*dist*/)
  303. {
  304. // See http://mathworld.wolfram.com/ExtremeValueDistribution.html
  305. return RealType(12) / 5;
  306. }
  307. } // namespace math
  308. } // namespace boost
  309. #ifdef _MSC_VER
  310. # pragma warning(pop)
  311. #endif
  312. // This include must be at the end, *after* the accessors
  313. // for this distribution have been defined, in order to
  314. // keep compilers that support two-phase lookup happy.
  315. #include <boost/math/distributions/detail/derived_accessors.hpp>
  316. #endif // BOOST_STATS_EXTREME_VALUE_HPP