Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:35:24

0001 //  Copyright John Maddock 2006.
0002 //  Copyright Matt Borland 2024.
0003 //  Use, modification and distribution are subject to the
0004 //  Boost Software License, Version 1.0. (See accompanying file
0005 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 
0007 #ifndef BOOST_STATS_EXTREME_VALUE_HPP
0008 #define BOOST_STATS_EXTREME_VALUE_HPP
0009 
0010 #include <boost/math/tools/config.hpp>
0011 #include <boost/math/tools/numeric_limits.hpp>
0012 #include <boost/math/tools/tuple.hpp>
0013 #include <boost/math/tools/precision.hpp>
0014 #include <boost/math/constants/constants.hpp>
0015 #include <boost/math/special_functions/log1p.hpp>
0016 #include <boost/math/special_functions/expm1.hpp>
0017 #include <boost/math/distributions/complement.hpp>
0018 #include <boost/math/distributions/detail/common_error_handling.hpp>
0019 #include <boost/math/policies/policy.hpp>
0020 #include <boost/math/policies/error_handling.hpp>
0021 
0022 //
0023 // This is the maximum extreme value distribution, see
0024 // http://www.itl.nist.gov/div898/handbook/eda/section3/eda366g.htm
0025 // and http://mathworld.wolfram.com/ExtremeValueDistribution.html
0026 // Also known as a Fisher-Tippett distribution, a log-Weibull
0027 // distribution or a Gumbel distribution.
0028 
0029 #ifndef BOOST_MATH_HAS_NVRTC
0030 #include <boost/math/distributions/fwd.hpp>
0031 #include <utility>
0032 #include <cmath>
0033 #endif
0034 
0035 #ifdef _MSC_VER
0036 # pragma warning(push)
0037 # pragma warning(disable: 4702) // unreachable code (return after domain_error throw).
0038 #endif
0039 
0040 namespace boost{ namespace math{
0041 
0042 namespace detail{
0043 //
0044 // Error check:
0045 //
0046 template <class RealType, class Policy>
0047 BOOST_MATH_GPU_ENABLED inline bool verify_scale_b(const char* function, RealType b, RealType* presult, const Policy& pol)
0048 {
0049    if((b <= 0) || !(boost::math::isfinite)(b))
0050    {
0051       *presult = policies::raise_domain_error<RealType>(
0052          function,
0053          "The scale parameter \"b\" must be finite and > 0, but was: %1%.", b, pol);
0054       return false;
0055    }
0056    return true;
0057 }
0058 
0059 } // namespace detail
0060 
0061 template <class RealType = double, class Policy = policies::policy<> >
0062 class extreme_value_distribution
0063 {
0064 public:
0065    using value_type = RealType;
0066    using policy_type = Policy;
0067 
0068    BOOST_MATH_GPU_ENABLED explicit extreme_value_distribution(RealType a = 0, RealType b = 1)
0069       : m_a(a), m_b(b)
0070    {
0071       RealType err;
0072       detail::verify_scale_b("boost::math::extreme_value_distribution<%1%>::extreme_value_distribution", b, &err, Policy());
0073       detail::check_finite("boost::math::extreme_value_distribution<%1%>::extreme_value_distribution", a, &err, Policy());
0074    } // extreme_value_distribution
0075 
0076    BOOST_MATH_GPU_ENABLED RealType location()const { return m_a; }
0077    BOOST_MATH_GPU_ENABLED RealType scale()const { return m_b; }
0078 
0079 private:
0080    RealType m_a;
0081    RealType m_b;
0082 };
0083 
0084 using extreme_value = extreme_value_distribution<double>;
0085 
0086 #ifdef __cpp_deduction_guides
0087 template <class RealType>
0088 extreme_value_distribution(RealType)->extreme_value_distribution<typename boost::math::tools::promote_args<RealType>::type>;
0089 template <class RealType>
0090 extreme_value_distribution(RealType,RealType)->extreme_value_distribution<typename boost::math::tools::promote_args<RealType>::type>;
0091 #endif
0092 
0093 template <class RealType, class Policy>
0094 BOOST_MATH_GPU_ENABLED inline boost::math::pair<RealType, RealType> range(const extreme_value_distribution<RealType, Policy>& /*dist*/)
0095 { // Range of permissible values for random variable x.
0096    using boost::math::tools::max_value;
0097    return boost::math::pair<RealType, RealType>(
0098       boost::math::numeric_limits<RealType>::has_infinity ? -boost::math::numeric_limits<RealType>::infinity() : -max_value<RealType>(), 
0099       boost::math::numeric_limits<RealType>::has_infinity ? boost::math::numeric_limits<RealType>::infinity() : max_value<RealType>());
0100 }
0101 
0102 template <class RealType, class Policy>
0103 BOOST_MATH_GPU_ENABLED inline boost::math::pair<RealType, RealType> support(const extreme_value_distribution<RealType, Policy>& /*dist*/)
0104 { // Range of supported values for random variable x.
0105    // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
0106    using boost::math::tools::max_value;
0107    return boost::math::pair<RealType, RealType>(-max_value<RealType>(),  max_value<RealType>());
0108 }
0109 
0110 template <class RealType, class Policy>
0111 BOOST_MATH_GPU_ENABLED inline RealType pdf(const extreme_value_distribution<RealType, Policy>& dist, const RealType& x)
0112 {
0113    BOOST_MATH_STD_USING // for ADL of std functions
0114 
0115    constexpr auto function = "boost::math::pdf(const extreme_value_distribution<%1%>&, %1%)";
0116 
0117    RealType a = dist.location();
0118    RealType b = dist.scale();
0119    RealType result = 0;
0120    if(0 == detail::verify_scale_b(function, b, &result, Policy()))
0121       return result;
0122    if(0 == detail::check_finite(function, a, &result, Policy()))
0123       return result;
0124    if((boost::math::isinf)(x))
0125       return 0.0f;
0126    if(0 == detail::check_x(function, x, &result, Policy()))
0127       return result;
0128    RealType e = (a - x) / b;
0129    if(e < tools::log_max_value<RealType>())
0130       result = exp(e) * exp(-exp(e)) / b;
0131    // else.... result *must* be zero since exp(e) is infinite...
0132    return result;
0133 } // pdf
0134 
0135 template <class RealType, class Policy>
0136 BOOST_MATH_GPU_ENABLED inline RealType logpdf(const extreme_value_distribution<RealType, Policy>& dist, const RealType& x)
0137 {
0138    BOOST_MATH_STD_USING // for ADL of std functions
0139 
0140    constexpr auto function = "boost::math::logpdf(const extreme_value_distribution<%1%>&, %1%)";
0141 
0142    RealType a = dist.location();
0143    RealType b = dist.scale();
0144    RealType result = -boost::math::numeric_limits<RealType>::infinity();
0145    if(0 == detail::verify_scale_b(function, b, &result, Policy()))
0146       return result;
0147    if(0 == detail::check_finite(function, a, &result, Policy()))
0148       return result;
0149    if((boost::math::isinf)(x))
0150       return 0.0f;
0151    if(0 == detail::check_x(function, x, &result, Policy()))
0152       return result;
0153    RealType e = (a - x) / b;
0154    if(e < tools::log_max_value<RealType>())
0155       result = log(1/b) + e - exp(e);
0156    // else.... result *must* be zero since exp(e) is infinite...
0157    return result;
0158 } // logpdf
0159 
0160 template <class RealType, class Policy>
0161 BOOST_MATH_GPU_ENABLED inline RealType cdf(const extreme_value_distribution<RealType, Policy>& dist, const RealType& x)
0162 {
0163    BOOST_MATH_STD_USING // for ADL of std functions
0164 
0165    constexpr auto function = "boost::math::cdf(const extreme_value_distribution<%1%>&, %1%)";
0166 
0167    if((boost::math::isinf)(x))
0168       return x < 0 ? 0.0f : 1.0f;
0169    RealType a = dist.location();
0170    RealType b = dist.scale();
0171    RealType result = 0;
0172    if(0 == detail::verify_scale_b(function, b, &result, Policy()))
0173       return result;
0174    if(0 == detail::check_finite(function, a, &result, Policy()))
0175       return result;
0176    if(0 == detail::check_finite(function, a, &result, Policy()))
0177       return result;
0178    if(0 == detail::check_x("boost::math::cdf(const extreme_value_distribution<%1%>&, %1%)", x, &result, Policy()))
0179       return result;
0180 
0181    result = exp(-exp((a-x)/b));
0182 
0183    return result;
0184 } // cdf
0185 
0186 template <class RealType, class Policy>
0187 BOOST_MATH_GPU_ENABLED inline RealType logcdf(const extreme_value_distribution<RealType, Policy>& dist, const RealType& x)
0188 {
0189    BOOST_MATH_STD_USING // for ADL of std functions
0190 
0191    constexpr auto function = "boost::math::logcdf(const extreme_value_distribution<%1%>&, %1%)";
0192 
0193    if((boost::math::isinf)(x))
0194       return x < 0 ? 0.0f : 1.0f;
0195    RealType a = dist.location();
0196    RealType b = dist.scale();
0197    RealType result = 0;
0198    if(0 == detail::verify_scale_b(function, b, &result, Policy()))
0199       return result;
0200    if(0 == detail::check_finite(function, a, &result, Policy()))
0201       return result;
0202    if(0 == detail::check_finite(function, a, &result, Policy()))
0203       return result;
0204    if(0 == detail::check_x("boost::math::logcdf(const extreme_value_distribution<%1%>&, %1%)", x, &result, Policy()))
0205       return result;
0206 
0207    result = -exp((a-x)/b);
0208 
0209    return result;
0210 } // logcdf
0211 
0212 template <class RealType, class Policy>
0213 BOOST_MATH_GPU_ENABLED RealType quantile(const extreme_value_distribution<RealType, Policy>& dist, const RealType& p)
0214 {
0215    BOOST_MATH_STD_USING // for ADL of std functions
0216 
0217    constexpr auto function = "boost::math::quantile(const extreme_value_distribution<%1%>&, %1%)";
0218 
0219    RealType a = dist.location();
0220    RealType b = dist.scale();
0221    RealType result = 0;
0222    if(0 == detail::verify_scale_b(function, b, &result, Policy()))
0223       return result;
0224    if(0 == detail::check_finite(function, a, &result, Policy()))
0225       return result;
0226    if(0 == detail::check_probability(function, p, &result, Policy()))
0227       return result;
0228 
0229    if(p == 0)
0230       return -policies::raise_overflow_error<RealType>(function, 0, Policy());
0231    if(p == 1)
0232       return policies::raise_overflow_error<RealType>(function, 0, Policy());
0233 
0234    result = a - log(-log(p)) * b;
0235 
0236    return result;
0237 } // quantile
0238 
0239 template <class RealType, class Policy>
0240 BOOST_MATH_GPU_ENABLED inline RealType cdf(const complemented2_type<extreme_value_distribution<RealType, Policy>, RealType>& c)
0241 {
0242    BOOST_MATH_STD_USING // for ADL of std functions
0243 
0244    constexpr auto function = "boost::math::cdf(const extreme_value_distribution<%1%>&, %1%)";
0245 
0246    if((boost::math::isinf)(c.param))
0247       return c.param < 0 ? 1.0f : 0.0f;
0248    RealType a = c.dist.location();
0249    RealType b = c.dist.scale();
0250    RealType result = 0;
0251    if(0 == detail::verify_scale_b(function, b, &result, Policy()))
0252       return result;
0253    if(0 == detail::check_finite(function, a, &result, Policy()))
0254       return result;
0255    if(0 == detail::check_x(function, c.param, &result, Policy()))
0256       return result;
0257 
0258    result = -boost::math::expm1(-exp((a-c.param)/b), Policy());
0259 
0260    return result;
0261 }
0262 
0263 template <class RealType, class Policy>
0264 BOOST_MATH_GPU_ENABLED inline RealType logcdf(const complemented2_type<extreme_value_distribution<RealType, Policy>, RealType>& c)
0265 {
0266    BOOST_MATH_STD_USING // for ADL of std functions
0267 
0268    constexpr auto function = "boost::math::logcdf(const extreme_value_distribution<%1%>&, %1%)";
0269 
0270    if((boost::math::isinf)(c.param))
0271       return c.param < 0 ? 1.0f : 0.0f;
0272    RealType a = c.dist.location();
0273    RealType b = c.dist.scale();
0274    RealType result = 0;
0275    if(0 == detail::verify_scale_b(function, b, &result, Policy()))
0276       return result;
0277    if(0 == detail::check_finite(function, a, &result, Policy()))
0278       return result;
0279    if(0 == detail::check_x(function, c.param, &result, Policy()))
0280       return result;
0281 
0282    result = log1p(-exp(-exp((a-c.param)/b)), Policy());
0283 
0284    return result;
0285 }
0286 
0287 template <class RealType, class Policy>
0288 BOOST_MATH_GPU_ENABLED RealType quantile(const complemented2_type<extreme_value_distribution<RealType, Policy>, RealType>& c)
0289 {
0290    BOOST_MATH_STD_USING // for ADL of std functions
0291 
0292    constexpr auto function = "boost::math::quantile(const extreme_value_distribution<%1%>&, %1%)";
0293 
0294    RealType a = c.dist.location();
0295    RealType b = c.dist.scale();
0296    RealType q = c.param;
0297    RealType result = 0;
0298    if(0 == detail::verify_scale_b(function, b, &result, Policy()))
0299       return result;
0300    if(0 == detail::check_finite(function, a, &result, Policy()))
0301       return result;
0302    if(0 == detail::check_probability(function, q, &result, Policy()))
0303       return result;
0304 
0305    if(q == 0)
0306       return policies::raise_overflow_error<RealType>(function, 0, Policy());
0307    if(q == 1)
0308       return -policies::raise_overflow_error<RealType>(function, 0, Policy());
0309 
0310    result = a - log(-boost::math::log1p(-q, Policy())) * b;
0311 
0312    return result;
0313 }
0314 
0315 template <class RealType, class Policy>
0316 BOOST_MATH_GPU_ENABLED inline RealType mean(const extreme_value_distribution<RealType, Policy>& dist)
0317 {
0318    RealType a = dist.location();
0319    RealType b = dist.scale();
0320    RealType result = 0;
0321    if(0 == detail::verify_scale_b("boost::math::mean(const extreme_value_distribution<%1%>&)", b, &result, Policy()))
0322       return result;
0323    if (0 == detail::check_finite("boost::math::mean(const extreme_value_distribution<%1%>&)", a, &result, Policy()))
0324       return result;
0325    return a + constants::euler<RealType>() * b;
0326 }
0327 
0328 template <class RealType, class Policy>
0329 BOOST_MATH_GPU_ENABLED inline RealType standard_deviation(const extreme_value_distribution<RealType, Policy>& dist)
0330 {
0331    BOOST_MATH_STD_USING // for ADL of std functions.
0332 
0333    RealType b = dist.scale();
0334    RealType result = 0;
0335    if(0 == detail::verify_scale_b("boost::math::standard_deviation(const extreme_value_distribution<%1%>&)", b, &result, Policy()))
0336       return result;
0337    if(0 == detail::check_finite("boost::math::standard_deviation(const extreme_value_distribution<%1%>&)", dist.location(), &result, Policy()))
0338       return result;
0339    return constants::pi<RealType>() * b / sqrt(static_cast<RealType>(6));
0340 }
0341 
0342 template <class RealType, class Policy>
0343 BOOST_MATH_GPU_ENABLED inline RealType mode(const extreme_value_distribution<RealType, Policy>& dist)
0344 {
0345    return dist.location();
0346 }
0347 
0348 template <class RealType, class Policy>
0349 BOOST_MATH_GPU_ENABLED inline RealType median(const extreme_value_distribution<RealType, Policy>& dist)
0350 {
0351   using constants::ln_ln_two;
0352    return dist.location() - dist.scale() * ln_ln_two<RealType>();
0353 }
0354 
0355 template <class RealType, class Policy>
0356 BOOST_MATH_GPU_ENABLED inline RealType skewness(const extreme_value_distribution<RealType, Policy>& /*dist*/)
0357 {
0358    //
0359    // This is 12 * sqrt(6) * zeta(3) / pi^3:
0360    // See http://mathworld.wolfram.com/ExtremeValueDistribution.html
0361    //
0362    return static_cast<RealType>(1.1395470994046486574927930193898461120875997958366L);
0363 }
0364 
0365 template <class RealType, class Policy>
0366 BOOST_MATH_GPU_ENABLED inline RealType kurtosis(const extreme_value_distribution<RealType, Policy>& /*dist*/)
0367 {
0368    // See http://mathworld.wolfram.com/ExtremeValueDistribution.html
0369    return RealType(27) / 5;
0370 }
0371 
0372 template <class RealType, class Policy>
0373 BOOST_MATH_GPU_ENABLED inline RealType kurtosis_excess(const extreme_value_distribution<RealType, Policy>& /*dist*/)
0374 {
0375    // See http://mathworld.wolfram.com/ExtremeValueDistribution.html
0376    return RealType(12) / 5;
0377 }
0378 
0379 
0380 } // namespace math
0381 } // namespace boost
0382 
0383 #ifdef _MSC_VER
0384 # pragma warning(pop)
0385 #endif
0386 
0387 // This include must be at the end, *after* the accessors
0388 // for this distribution have been defined, in order to
0389 // keep compilers that support two-phase lookup happy.
0390 #include <boost/math/distributions/detail/derived_accessors.hpp>
0391 
0392 #endif // BOOST_STATS_EXTREME_VALUE_HPP