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)
6 #ifndef BOOST_STATS_EXTREME_VALUE_HPP
7 #define BOOST_STATS_EXTREME_VALUE_HPP
9 #include <boost/math/distributions/fwd.hpp>
10 #include <boost/math/constants/constants.hpp>
11 #include <boost/math/special_functions/log1p.hpp>
12 #include <boost/math/special_functions/expm1.hpp>
13 #include <boost/math/distributions/complement.hpp>
14 #include <boost/math/distributions/detail/common_error_handling.hpp>
15 #include <boost/config/no_tr1/cmath.hpp>
18 // This is the maximum extreme value distribution, see
19 // http://www.itl.nist.gov/div898/handbook/eda/section3/eda366g.htm
20 // and http://mathworld.wolfram.com/ExtremeValueDistribution.html
21 // Also known as a Fisher-Tippett distribution, a log-Weibull
22 // distribution or a Gumbel distribution.
27 # pragma warning(push)
28 # pragma warning(disable: 4702) // unreachable code (return after domain_error throw).
31 namespace boost{ namespace math{
37 template <class RealType, class Policy>
38 inline bool verify_scale_b(const char* function, RealType b, RealType* presult, const Policy& pol)
40 if((b <= 0) || !(boost::math::isfinite)(b))
42 *presult = policies::raise_domain_error<RealType>(
44 "The scale parameter \"b\" must be finite and > 0, but was: %1%.", b, pol);
52 template <class RealType = double, class Policy = policies::policy<> >
53 class extreme_value_distribution
56 typedef RealType value_type;
57 typedef Policy policy_type;
59 extreme_value_distribution(RealType a = 0, RealType b = 1)
63 detail::verify_scale_b("boost::math::extreme_value_distribution<%1%>::extreme_value_distribution", b, &err, Policy());
64 detail::check_finite("boost::math::extreme_value_distribution<%1%>::extreme_value_distribution", a, &err, Policy());
65 } // extreme_value_distribution
67 RealType location()const { return m_a; }
68 RealType scale()const { return m_b; }
74 typedef extreme_value_distribution<double> extreme_value;
76 template <class RealType, class Policy>
77 inline const std::pair<RealType, RealType> range(const extreme_value_distribution<RealType, Policy>& /*dist*/)
78 { // Range of permissible values for random variable x.
79 using boost::math::tools::max_value;
80 return std::pair<RealType, RealType>(
81 std::numeric_limits<RealType>::has_infinity ? -std::numeric_limits<RealType>::infinity() : -max_value<RealType>(),
82 std::numeric_limits<RealType>::has_infinity ? std::numeric_limits<RealType>::infinity() : max_value<RealType>());
85 template <class RealType, class Policy>
86 inline const std::pair<RealType, RealType> support(const extreme_value_distribution<RealType, Policy>& /*dist*/)
87 { // Range of supported values for random variable x.
88 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
89 using boost::math::tools::max_value;
90 return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>());
93 template <class RealType, class Policy>
94 inline RealType pdf(const extreme_value_distribution<RealType, Policy>& dist, const RealType& x)
96 BOOST_MATH_STD_USING // for ADL of std functions
98 static const char* function = "boost::math::pdf(const extreme_value_distribution<%1%>&, %1%)";
100 RealType a = dist.location();
101 RealType b = dist.scale();
103 if(0 == detail::verify_scale_b(function, b, &result, Policy()))
105 if(0 == detail::check_finite(function, a, &result, Policy()))
107 if((boost::math::isinf)(x))
109 if(0 == detail::check_x(function, x, &result, Policy()))
111 RealType e = (a - x) / b;
112 if(e < tools::log_max_value<RealType>())
113 result = exp(e) * exp(-exp(e)) / b;
114 // else.... result *must* be zero since exp(e) is infinite...
118 template <class RealType, class Policy>
119 inline RealType cdf(const extreme_value_distribution<RealType, Policy>& dist, const RealType& x)
121 BOOST_MATH_STD_USING // for ADL of std functions
123 static const char* function = "boost::math::cdf(const extreme_value_distribution<%1%>&, %1%)";
125 if((boost::math::isinf)(x))
126 return x < 0 ? 0.0f : 1.0f;
127 RealType a = dist.location();
128 RealType b = dist.scale();
130 if(0 == detail::verify_scale_b(function, b, &result, Policy()))
132 if(0 == detail::check_finite(function, a, &result, Policy()))
134 if(0 == detail::check_finite(function, a, &result, Policy()))
136 if(0 == detail::check_x("boost::math::cdf(const extreme_value_distribution<%1%>&, %1%)", x, &result, Policy()))
139 result = exp(-exp((a-x)/b));
144 template <class RealType, class Policy>
145 RealType quantile(const extreme_value_distribution<RealType, Policy>& dist, const RealType& p)
147 BOOST_MATH_STD_USING // for ADL of std functions
149 static const char* function = "boost::math::quantile(const extreme_value_distribution<%1%>&, %1%)";
151 RealType a = dist.location();
152 RealType b = dist.scale();
154 if(0 == detail::verify_scale_b(function, b, &result, Policy()))
156 if(0 == detail::check_finite(function, a, &result, Policy()))
158 if(0 == detail::check_probability(function, p, &result, Policy()))
162 return -policies::raise_overflow_error<RealType>(function, 0, Policy());
164 return policies::raise_overflow_error<RealType>(function, 0, Policy());
166 result = a - log(-log(p)) * b;
171 template <class RealType, class Policy>
172 inline RealType cdf(const complemented2_type<extreme_value_distribution<RealType, Policy>, RealType>& c)
174 BOOST_MATH_STD_USING // for ADL of std functions
176 static const char* function = "boost::math::cdf(const extreme_value_distribution<%1%>&, %1%)";
178 if((boost::math::isinf)(c.param))
179 return c.param < 0 ? 1.0f : 0.0f;
180 RealType a = c.dist.location();
181 RealType b = c.dist.scale();
183 if(0 == detail::verify_scale_b(function, b, &result, Policy()))
185 if(0 == detail::check_finite(function, a, &result, Policy()))
187 if(0 == detail::check_x(function, c.param, &result, Policy()))
190 result = -boost::math::expm1(-exp((a-c.param)/b), Policy());
195 template <class RealType, class Policy>
196 RealType quantile(const complemented2_type<extreme_value_distribution<RealType, Policy>, RealType>& c)
198 BOOST_MATH_STD_USING // for ADL of std functions
200 static const char* function = "boost::math::quantile(const extreme_value_distribution<%1%>&, %1%)";
202 RealType a = c.dist.location();
203 RealType b = c.dist.scale();
204 RealType q = c.param;
206 if(0 == detail::verify_scale_b(function, b, &result, Policy()))
208 if(0 == detail::check_finite(function, a, &result, Policy()))
210 if(0 == detail::check_probability(function, q, &result, Policy()))
214 return policies::raise_overflow_error<RealType>(function, 0, Policy());
216 return -policies::raise_overflow_error<RealType>(function, 0, Policy());
218 result = a - log(-boost::math::log1p(-q, Policy())) * b;
223 template <class RealType, class Policy>
224 inline RealType mean(const extreme_value_distribution<RealType, Policy>& dist)
226 RealType a = dist.location();
227 RealType b = dist.scale();
229 if(0 == detail::verify_scale_b("boost::math::mean(const extreme_value_distribution<%1%>&)", b, &result, Policy()))
231 if(0 == detail::check_scale("boost::math::mean(const extreme_value_distribution<%1%>&)", a, &result, Policy()))
233 return a + constants::euler<RealType>() * b;
236 template <class RealType, class Policy>
237 inline RealType standard_deviation(const extreme_value_distribution<RealType, Policy>& dist)
239 BOOST_MATH_STD_USING // for ADL of std functions.
241 RealType b = dist.scale();
243 if(0 == detail::verify_scale_b("boost::math::standard_deviation(const extreme_value_distribution<%1%>&)", b, &result, Policy()))
245 if(0 == detail::check_scale("boost::math::standard_deviation(const extreme_value_distribution<%1%>&)", dist.location(), &result, Policy()))
247 return constants::pi<RealType>() * b / sqrt(static_cast<RealType>(6));
250 template <class RealType, class Policy>
251 inline RealType mode(const extreme_value_distribution<RealType, Policy>& dist)
253 return dist.location();
256 template <class RealType, class Policy>
257 inline RealType median(const extreme_value_distribution<RealType, Policy>& dist)
259 using constants::ln_ln_two;
260 return dist.location() - dist.scale() * ln_ln_two<RealType>();
263 template <class RealType, class Policy>
264 inline RealType skewness(const extreme_value_distribution<RealType, Policy>& /*dist*/)
267 // This is 12 * sqrt(6) * zeta(3) / pi^3:
268 // See http://mathworld.wolfram.com/ExtremeValueDistribution.html
270 return static_cast<RealType>(1.1395470994046486574927930193898461120875997958366L);
273 template <class RealType, class Policy>
274 inline RealType kurtosis(const extreme_value_distribution<RealType, Policy>& /*dist*/)
276 // See http://mathworld.wolfram.com/ExtremeValueDistribution.html
277 return RealType(27) / 5;
280 template <class RealType, class Policy>
281 inline RealType kurtosis_excess(const extreme_value_distribution<RealType, Policy>& /*dist*/)
283 // See http://mathworld.wolfram.com/ExtremeValueDistribution.html
284 return RealType(12) / 5;
292 # pragma warning(pop)
295 // This include must be at the end, *after* the accessors
296 // for this distribution have been defined, in order to
297 // keep compilers that support two-phase lookup happy.
298 #include <boost/math/distributions/detail/derived_accessors.hpp>
300 #endif // BOOST_STATS_EXTREME_VALUE_HPP