Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:39

0001 //  Copyright John Maddock 2006.
0002 //  Use, modification and distribution are subject to the
0003 //  Boost Software License, Version 1.0. (See accompanying file
0004 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 #ifndef BOOST_STATS_EXPONENTIAL_HPP
0007 #define BOOST_STATS_EXPONENTIAL_HPP
0008 
0009 #include <boost/math/distributions/fwd.hpp>
0010 #include <boost/math/constants/constants.hpp>
0011 #include <boost/math/special_functions/log1p.hpp>
0012 #include <boost/math/special_functions/expm1.hpp>
0013 #include <boost/math/distributions/complement.hpp>
0014 #include <boost/math/distributions/detail/common_error_handling.hpp>
0015 
0016 #ifdef _MSC_VER
0017 # pragma warning(push)
0018 # pragma warning(disable: 4127) // conditional expression is constant
0019 # pragma warning(disable: 4702) // unreachable code (return after domain_error throw).
0020 #endif
0021 
0022 #include <utility>
0023 #include <cmath>
0024 
0025 namespace boost{ namespace math{
0026 
0027 namespace detail{
0028 //
0029 // Error check:
0030 //
0031 template <class RealType, class Policy>
0032 inline bool verify_lambda(const char* function, RealType l, RealType* presult, const Policy& pol)
0033 {
0034    if((l <= 0) || !(boost::math::isfinite)(l))
0035    {
0036       *presult = policies::raise_domain_error<RealType>(
0037          function,
0038          "The scale parameter \"lambda\" must be > 0, but was: %1%.", l, pol);
0039       return false;
0040    }
0041    return true;
0042 }
0043 
0044 template <class RealType, class Policy>
0045 inline bool verify_exp_x(const char* function, RealType x, RealType* presult, const Policy& pol)
0046 {
0047    if((x < 0) || (boost::math::isnan)(x))
0048    {
0049       *presult = policies::raise_domain_error<RealType>(
0050          function,
0051          "The random variable must be >= 0, but was: %1%.", x, pol);
0052       return false;
0053    }
0054    return true;
0055 }
0056 
0057 } // namespace detail
0058 
0059 template <class RealType = double, class Policy = policies::policy<> >
0060 class exponential_distribution
0061 {
0062 public:
0063    using value_type = RealType;
0064    using policy_type = Policy;
0065 
0066    explicit exponential_distribution(RealType l_lambda = 1)
0067       : m_lambda(l_lambda)
0068    {
0069       RealType err;
0070       detail::verify_lambda("boost::math::exponential_distribution<%1%>::exponential_distribution", l_lambda, &err, Policy());
0071    } // exponential_distribution
0072 
0073    RealType lambda()const { return m_lambda; }
0074 
0075 private:
0076    RealType m_lambda;
0077 };
0078 
0079 using exponential = exponential_distribution<double>;
0080 
0081 #ifdef __cpp_deduction_guides
0082 template <class RealType>
0083 exponential_distribution(RealType)->exponential_distribution<typename boost::math::tools::promote_args<RealType>::type>;
0084 #endif
0085 
0086 template <class RealType, class Policy>
0087 inline std::pair<RealType, RealType> range(const exponential_distribution<RealType, Policy>& /*dist*/)
0088 { // Range of permissible values for random variable x.
0089   if (std::numeric_limits<RealType>::has_infinity)
0090   { 
0091     return std::pair<RealType, RealType>(static_cast<RealType>(0), std::numeric_limits<RealType>::infinity()); // 0 to + infinity.
0092   }
0093   else
0094   {
0095    using boost::math::tools::max_value;
0096    return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>()); // 0 to + max
0097   }
0098 }
0099 
0100 template <class RealType, class Policy>
0101 inline std::pair<RealType, RealType> support(const exponential_distribution<RealType, Policy>& /*dist*/)
0102 { // Range of supported values for random variable x.
0103    // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
0104    using boost::math::tools::max_value;
0105    using boost::math::tools::min_value;
0106    return std::pair<RealType, RealType>(min_value<RealType>(),  max_value<RealType>());
0107    // min_value<RealType>() to avoid a discontinuity at x = 0.
0108 }
0109 
0110 template <class RealType, class Policy>
0111 inline RealType pdf(const exponential_distribution<RealType, Policy>& dist, const RealType& x)
0112 {
0113    BOOST_MATH_STD_USING // for ADL of std functions
0114 
0115    static const char* function = "boost::math::pdf(const exponential_distribution<%1%>&, %1%)";
0116 
0117    RealType lambda = dist.lambda();
0118    RealType result = 0;
0119    if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
0120       return result;
0121    if(0 == detail::verify_exp_x(function, x, &result, Policy()))
0122       return result;
0123    // Workaround for VC11/12 bug:
0124    if ((boost::math::isinf)(x))
0125       return 0;
0126    result = lambda * exp(-lambda * x);
0127    return result;
0128 } // pdf
0129 
0130 template <class RealType, class Policy>
0131 inline RealType logpdf(const exponential_distribution<RealType, Policy>& dist, const RealType& x)
0132 {
0133    BOOST_MATH_STD_USING // for ADL of std functions
0134 
0135    static const char* function = "boost::math::logpdf(const exponential_distribution<%1%>&, %1%)";
0136 
0137    RealType lambda = dist.lambda();
0138    RealType result = -std::numeric_limits<RealType>::infinity();
0139    if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
0140       return result;
0141    if(0 == detail::verify_exp_x(function, x, &result, Policy()))
0142       return result;
0143    
0144    result = log(lambda) - lambda * x;
0145    return result;
0146 } // logpdf
0147 
0148 template <class RealType, class Policy>
0149 inline RealType cdf(const exponential_distribution<RealType, Policy>& dist, const RealType& x)
0150 {
0151    BOOST_MATH_STD_USING // for ADL of std functions
0152 
0153    static const char* function = "boost::math::cdf(const exponential_distribution<%1%>&, %1%)";
0154 
0155    RealType result = 0;
0156    RealType lambda = dist.lambda();
0157    if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
0158       return result;
0159    if(0 == detail::verify_exp_x(function, x, &result, Policy()))
0160       return result;
0161    result = -boost::math::expm1(-x * lambda, Policy());
0162 
0163    return result;
0164 } // cdf
0165 
0166 template <class RealType, class Policy>
0167 inline RealType logcdf(const exponential_distribution<RealType, Policy>& dist, const RealType& x)
0168 {
0169    BOOST_MATH_STD_USING // for ADL of std functions
0170 
0171    static const char* function = "boost::math::logcdf(const exponential_distribution<%1%>&, %1%)";
0172 
0173    RealType result = 0;
0174    RealType lambda = dist.lambda();
0175    if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
0176       return result;
0177    if(0 == detail::verify_exp_x(function, x, &result, Policy()))
0178       return result;
0179    result = boost::math::log1p(-exp(-x * lambda), Policy());
0180 
0181    return result;
0182 } // cdf
0183 
0184 template <class RealType, class Policy>
0185 inline RealType quantile(const exponential_distribution<RealType, Policy>& dist, const RealType& p)
0186 {
0187    BOOST_MATH_STD_USING // for ADL of std functions
0188 
0189    static const char* function = "boost::math::quantile(const exponential_distribution<%1%>&, %1%)";
0190 
0191    RealType result = 0;
0192    RealType lambda = dist.lambda();
0193    if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
0194       return result;
0195    if(0 == detail::check_probability(function, p, &result, Policy()))
0196       return result;
0197 
0198    if(p == 0)
0199       return 0;
0200    if(p == 1)
0201       return policies::raise_overflow_error<RealType>(function, 0, Policy());
0202 
0203    result = -boost::math::log1p(-p, Policy()) / lambda;
0204    return result;
0205 } // quantile
0206 
0207 template <class RealType, class Policy>
0208 inline RealType cdf(const complemented2_type<exponential_distribution<RealType, Policy>, RealType>& c)
0209 {
0210    BOOST_MATH_STD_USING // for ADL of std functions
0211 
0212    static const char* function = "boost::math::cdf(const exponential_distribution<%1%>&, %1%)";
0213 
0214    RealType result = 0;
0215    RealType lambda = c.dist.lambda();
0216    if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
0217       return result;
0218    if(0 == detail::verify_exp_x(function, c.param, &result, Policy()))
0219       return result;
0220    // Workaround for VC11/12 bug:
0221    if (c.param >= tools::max_value<RealType>())
0222       return 0;
0223    result = exp(-c.param * lambda);
0224 
0225    return result;
0226 }
0227 
0228 template <class RealType, class Policy>
0229 inline RealType logcdf(const complemented2_type<exponential_distribution<RealType, Policy>, RealType>& c)
0230 {
0231    BOOST_MATH_STD_USING // for ADL of std functions
0232 
0233    static const char* function = "boost::math::logcdf(const exponential_distribution<%1%>&, %1%)";
0234 
0235    RealType result = 0;
0236    RealType lambda = c.dist.lambda();
0237    if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
0238       return result;
0239    if(0 == detail::verify_exp_x(function, c.param, &result, Policy()))
0240       return result;
0241    // Workaround for VC11/12 bug:
0242    if (c.param >= tools::max_value<RealType>())
0243       return 0;
0244    result = -c.param * lambda;
0245 
0246    return result;
0247 }
0248 
0249 template <class RealType, class Policy>
0250 inline RealType quantile(const complemented2_type<exponential_distribution<RealType, Policy>, RealType>& c)
0251 {
0252    BOOST_MATH_STD_USING // for ADL of std functions
0253 
0254    static const char* function = "boost::math::quantile(const exponential_distribution<%1%>&, %1%)";
0255 
0256    RealType result = 0;
0257    RealType lambda = c.dist.lambda();
0258    if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
0259       return result;
0260 
0261    RealType q = c.param;
0262    if(0 == detail::check_probability(function, q, &result, Policy()))
0263       return result;
0264 
0265    if(q == 1)
0266       return 0;
0267    if(q == 0)
0268       return policies::raise_overflow_error<RealType>(function, 0, Policy());
0269 
0270    result = -log(q) / lambda;
0271    return result;
0272 }
0273 
0274 template <class RealType, class Policy>
0275 inline RealType mean(const exponential_distribution<RealType, Policy>& dist)
0276 {
0277    RealType result = 0;
0278    RealType lambda = dist.lambda();
0279    if(0 == detail::verify_lambda("boost::math::mean(const exponential_distribution<%1%>&)", lambda, &result, Policy()))
0280       return result;
0281    return 1 / lambda;
0282 }
0283 
0284 template <class RealType, class Policy>
0285 inline RealType standard_deviation(const exponential_distribution<RealType, Policy>& dist)
0286 {
0287    RealType result = 0;
0288    RealType lambda = dist.lambda();
0289    if(0 == detail::verify_lambda("boost::math::standard_deviation(const exponential_distribution<%1%>&)", lambda, &result, Policy()))
0290       return result;
0291    return 1 / lambda;
0292 }
0293 
0294 template <class RealType, class Policy>
0295 inline RealType mode(const exponential_distribution<RealType, Policy>& /*dist*/)
0296 {
0297    return 0;
0298 }
0299 
0300 template <class RealType, class Policy>
0301 inline RealType median(const exponential_distribution<RealType, Policy>& dist)
0302 {
0303    using boost::math::constants::ln_two;
0304    return ln_two<RealType>() / dist.lambda(); // ln(2) / lambda
0305 }
0306 
0307 template <class RealType, class Policy>
0308 inline RealType skewness(const exponential_distribution<RealType, Policy>& /*dist*/)
0309 {
0310    return 2;
0311 }
0312 
0313 template <class RealType, class Policy>
0314 inline RealType kurtosis(const exponential_distribution<RealType, Policy>& /*dist*/)
0315 {
0316    return 9;
0317 }
0318 
0319 template <class RealType, class Policy>
0320 inline RealType kurtosis_excess(const exponential_distribution<RealType, Policy>& /*dist*/)
0321 {
0322    return 6;
0323 }
0324 
0325 template <class RealType, class Policy>
0326 inline RealType entropy(const exponential_distribution<RealType, Policy>& dist)
0327 {
0328    using std::log;
0329    return 1 - log(dist.lambda());
0330 }
0331 
0332 } // namespace math
0333 } // namespace boost
0334 
0335 #ifdef _MSC_VER
0336 # pragma warning(pop)
0337 #endif
0338 
0339 // This include must be at the end, *after* the accessors
0340 // for this distribution have been defined, in order to
0341 // keep compilers that support two-phase lookup happy.
0342 #include <boost/math/distributions/detail/derived_accessors.hpp>
0343 
0344 #endif // BOOST_STATS_EXPONENTIAL_HPP