| 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 | |
| 6 | #ifndef BOOST_STATS_EXPONENTIAL_HPP |
| 7 | #define BOOST_STATS_EXPONENTIAL_HPP |
| 8 | |
| 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 | |
| 16 | #ifdef _MSC_VER |
| 17 | # pragma warning(push) |
| 18 | # pragma warning(disable: 4127) // conditional expression is constant |
| 19 | # pragma warning(disable: 4702) // unreachable code (return after domain_error throw). |
| 20 | #endif |
| 21 | |
| 22 | #include <utility> |
| 23 | #include <cmath> |
| 24 | |
| 25 | namespace boost{ namespace math{ |
| 26 | |
| 27 | namespace detail{ |
| 28 | // |
| 29 | // Error check: |
| 30 | // |
| 31 | template <class RealType, class Policy> |
| 32 | inline bool verify_lambda(const char* function, RealType l, RealType* presult, const Policy& pol) |
| 33 | { |
| 34 | if((l <= 0) || !(boost::math::isfinite)(l)) |
| 35 | { |
| 36 | *presult = policies::raise_domain_error<RealType>( |
| 37 | function, |
| 38 | "The scale parameter \"lambda\" must be > 0, but was: %1%." , l, pol); |
| 39 | return false; |
| 40 | } |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | template <class RealType, class Policy> |
| 45 | inline bool verify_exp_x(const char* function, RealType x, RealType* presult, const Policy& pol) |
| 46 | { |
| 47 | if((x < 0) || (boost::math::isnan)(x)) |
| 48 | { |
| 49 | *presult = policies::raise_domain_error<RealType>( |
| 50 | function, |
| 51 | "The random variable must be >= 0, but was: %1%." , x, pol); |
| 52 | return false; |
| 53 | } |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | } // namespace detail |
| 58 | |
| 59 | template <class RealType = double, class Policy = policies::policy<> > |
| 60 | class exponential_distribution |
| 61 | { |
| 62 | public: |
| 63 | using value_type = RealType; |
| 64 | using policy_type = Policy; |
| 65 | |
| 66 | explicit exponential_distribution(RealType l_lambda = 1) |
| 67 | : m_lambda(l_lambda) |
| 68 | { |
| 69 | RealType err; |
| 70 | detail::verify_lambda("boost::math::exponential_distribution<%1%>::exponential_distribution" , l_lambda, &err, Policy()); |
| 71 | } // exponential_distribution |
| 72 | |
| 73 | RealType lambda()const { return m_lambda; } |
| 74 | |
| 75 | private: |
| 76 | RealType m_lambda; |
| 77 | }; |
| 78 | |
| 79 | using exponential = exponential_distribution<double>; |
| 80 | |
| 81 | #ifdef __cpp_deduction_guides |
| 82 | template <class RealType> |
| 83 | exponential_distribution(RealType)->exponential_distribution<typename boost::math::tools::promote_args<RealType>::type>; |
| 84 | #endif |
| 85 | |
| 86 | template <class RealType, class Policy> |
| 87 | inline std::pair<RealType, RealType> range(const exponential_distribution<RealType, Policy>& /*dist*/) |
| 88 | { // Range of permissible values for random variable x. |
| 89 | if (std::numeric_limits<RealType>::has_infinity) |
| 90 | { |
| 91 | return std::pair<RealType, RealType>(static_cast<RealType>(0), std::numeric_limits<RealType>::infinity()); // 0 to + infinity. |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | using boost::math::tools::max_value; |
| 96 | return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>()); // 0 to + max |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | template <class RealType, class Policy> |
| 101 | inline std::pair<RealType, RealType> support(const exponential_distribution<RealType, Policy>& /*dist*/) |
| 102 | { // Range of supported values for random variable x. |
| 103 | // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero. |
| 104 | using boost::math::tools::max_value; |
| 105 | using boost::math::tools::min_value; |
| 106 | return std::pair<RealType, RealType>(min_value<RealType>(), max_value<RealType>()); |
| 107 | // min_value<RealType>() to avoid a discontinuity at x = 0. |
| 108 | } |
| 109 | |
| 110 | template <class RealType, class Policy> |
| 111 | inline RealType pdf(const exponential_distribution<RealType, Policy>& dist, const RealType& x) |
| 112 | { |
| 113 | BOOST_MATH_STD_USING // for ADL of std functions |
| 114 | |
| 115 | static const char* function = "boost::math::pdf(const exponential_distribution<%1%>&, %1%)" ; |
| 116 | |
| 117 | RealType lambda = dist.lambda(); |
| 118 | RealType result = 0; |
| 119 | if(0 == detail::verify_lambda(function, lambda, &result, Policy())) |
| 120 | return result; |
| 121 | if(0 == detail::verify_exp_x(function, x, &result, Policy())) |
| 122 | return result; |
| 123 | // Workaround for VC11/12 bug: |
| 124 | if ((boost::math::isinf)(x)) |
| 125 | return 0; |
| 126 | result = lambda * exp(-lambda * x); |
| 127 | return result; |
| 128 | } // pdf |
| 129 | |
| 130 | template <class RealType, class Policy> |
| 131 | inline RealType logpdf(const exponential_distribution<RealType, Policy>& dist, const RealType& x) |
| 132 | { |
| 133 | BOOST_MATH_STD_USING // for ADL of std functions |
| 134 | |
| 135 | static const char* function = "boost::math::logpdf(const exponential_distribution<%1%>&, %1%)" ; |
| 136 | |
| 137 | RealType lambda = dist.lambda(); |
| 138 | RealType result = -std::numeric_limits<RealType>::infinity(); |
| 139 | if(0 == detail::verify_lambda(function, lambda, &result, Policy())) |
| 140 | return result; |
| 141 | if(0 == detail::verify_exp_x(function, x, &result, Policy())) |
| 142 | return result; |
| 143 | |
| 144 | result = log(lambda) - lambda * x; |
| 145 | return result; |
| 146 | } // logpdf |
| 147 | |
| 148 | template <class RealType, class Policy> |
| 149 | inline RealType cdf(const exponential_distribution<RealType, Policy>& dist, const RealType& x) |
| 150 | { |
| 151 | BOOST_MATH_STD_USING // for ADL of std functions |
| 152 | |
| 153 | static const char* function = "boost::math::cdf(const exponential_distribution<%1%>&, %1%)" ; |
| 154 | |
| 155 | RealType result = 0; |
| 156 | RealType lambda = dist.lambda(); |
| 157 | if(0 == detail::verify_lambda(function, lambda, &result, Policy())) |
| 158 | return result; |
| 159 | if(0 == detail::verify_exp_x(function, x, &result, Policy())) |
| 160 | return result; |
| 161 | result = -boost::math::expm1(-x * lambda, Policy()); |
| 162 | |
| 163 | return result; |
| 164 | } // cdf |
| 165 | |
| 166 | template <class RealType, class Policy> |
| 167 | inline RealType logcdf(const exponential_distribution<RealType, Policy>& dist, const RealType& x) |
| 168 | { |
| 169 | BOOST_MATH_STD_USING // for ADL of std functions |
| 170 | |
| 171 | static const char* function = "boost::math::logcdf(const exponential_distribution<%1%>&, %1%)" ; |
| 172 | |
| 173 | RealType result = 0; |
| 174 | RealType lambda = dist.lambda(); |
| 175 | if(0 == detail::verify_lambda(function, lambda, &result, Policy())) |
| 176 | return result; |
| 177 | if(0 == detail::verify_exp_x(function, x, &result, Policy())) |
| 178 | return result; |
| 179 | result = boost::math::log1p(-exp(-x * lambda), Policy()); |
| 180 | |
| 181 | return result; |
| 182 | } // cdf |
| 183 | |
| 184 | template <class RealType, class Policy> |
| 185 | inline RealType quantile(const exponential_distribution<RealType, Policy>& dist, const RealType& p) |
| 186 | { |
| 187 | BOOST_MATH_STD_USING // for ADL of std functions |
| 188 | |
| 189 | static const char* function = "boost::math::quantile(const exponential_distribution<%1%>&, %1%)" ; |
| 190 | |
| 191 | RealType result = 0; |
| 192 | RealType lambda = dist.lambda(); |
| 193 | if(0 == detail::verify_lambda(function, lambda, &result, Policy())) |
| 194 | return result; |
| 195 | if(0 == detail::check_probability(function, p, &result, Policy())) |
| 196 | return result; |
| 197 | |
| 198 | if(p == 0) |
| 199 | return 0; |
| 200 | if(p == 1) |
| 201 | return policies::raise_overflow_error<RealType>(function, 0, Policy()); |
| 202 | |
| 203 | result = -boost::math::log1p(-p, Policy()) / lambda; |
| 204 | return result; |
| 205 | } // quantile |
| 206 | |
| 207 | template <class RealType, class Policy> |
| 208 | inline RealType cdf(const complemented2_type<exponential_distribution<RealType, Policy>, RealType>& c) |
| 209 | { |
| 210 | BOOST_MATH_STD_USING // for ADL of std functions |
| 211 | |
| 212 | static const char* function = "boost::math::cdf(const exponential_distribution<%1%>&, %1%)" ; |
| 213 | |
| 214 | RealType result = 0; |
| 215 | RealType lambda = c.dist.lambda(); |
| 216 | if(0 == detail::verify_lambda(function, lambda, &result, Policy())) |
| 217 | return result; |
| 218 | if(0 == detail::verify_exp_x(function, c.param, &result, Policy())) |
| 219 | return result; |
| 220 | // Workaround for VC11/12 bug: |
| 221 | if (c.param >= tools::max_value<RealType>()) |
| 222 | return 0; |
| 223 | result = exp(-c.param * lambda); |
| 224 | |
| 225 | return result; |
| 226 | } |
| 227 | |
| 228 | template <class RealType, class Policy> |
| 229 | inline RealType logcdf(const complemented2_type<exponential_distribution<RealType, Policy>, RealType>& c) |
| 230 | { |
| 231 | BOOST_MATH_STD_USING // for ADL of std functions |
| 232 | |
| 233 | static const char* function = "boost::math::logcdf(const exponential_distribution<%1%>&, %1%)" ; |
| 234 | |
| 235 | RealType result = 0; |
| 236 | RealType lambda = c.dist.lambda(); |
| 237 | if(0 == detail::verify_lambda(function, lambda, &result, Policy())) |
| 238 | return result; |
| 239 | if(0 == detail::verify_exp_x(function, c.param, &result, Policy())) |
| 240 | return result; |
| 241 | // Workaround for VC11/12 bug: |
| 242 | if (c.param >= tools::max_value<RealType>()) |
| 243 | return 0; |
| 244 | result = -c.param * lambda; |
| 245 | |
| 246 | return result; |
| 247 | } |
| 248 | |
| 249 | template <class RealType, class Policy> |
| 250 | inline RealType quantile(const complemented2_type<exponential_distribution<RealType, Policy>, RealType>& c) |
| 251 | { |
| 252 | BOOST_MATH_STD_USING // for ADL of std functions |
| 253 | |
| 254 | static const char* function = "boost::math::quantile(const exponential_distribution<%1%>&, %1%)" ; |
| 255 | |
| 256 | RealType result = 0; |
| 257 | RealType lambda = c.dist.lambda(); |
| 258 | if(0 == detail::verify_lambda(function, lambda, &result, Policy())) |
| 259 | return result; |
| 260 | |
| 261 | RealType q = c.param; |
| 262 | if(0 == detail::check_probability(function, q, &result, Policy())) |
| 263 | return result; |
| 264 | |
| 265 | if(q == 1) |
| 266 | return 0; |
| 267 | if(q == 0) |
| 268 | return policies::raise_overflow_error<RealType>(function, 0, Policy()); |
| 269 | |
| 270 | result = -log(q) / lambda; |
| 271 | return result; |
| 272 | } |
| 273 | |
| 274 | template <class RealType, class Policy> |
| 275 | inline RealType mean(const exponential_distribution<RealType, Policy>& dist) |
| 276 | { |
| 277 | RealType result = 0; |
| 278 | RealType lambda = dist.lambda(); |
| 279 | if(0 == detail::verify_lambda("boost::math::mean(const exponential_distribution<%1%>&)" , lambda, &result, Policy())) |
| 280 | return result; |
| 281 | return 1 / lambda; |
| 282 | } |
| 283 | |
| 284 | template <class RealType, class Policy> |
| 285 | inline RealType standard_deviation(const exponential_distribution<RealType, Policy>& dist) |
| 286 | { |
| 287 | RealType result = 0; |
| 288 | RealType lambda = dist.lambda(); |
| 289 | if(0 == detail::verify_lambda("boost::math::standard_deviation(const exponential_distribution<%1%>&)" , lambda, &result, Policy())) |
| 290 | return result; |
| 291 | return 1 / lambda; |
| 292 | } |
| 293 | |
| 294 | template <class RealType, class Policy> |
| 295 | inline RealType mode(const exponential_distribution<RealType, Policy>& /*dist*/) |
| 296 | { |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | template <class RealType, class Policy> |
| 301 | inline RealType median(const exponential_distribution<RealType, Policy>& dist) |
| 302 | { |
| 303 | using boost::math::constants::ln_two; |
| 304 | return ln_two<RealType>() / dist.lambda(); // ln(2) / lambda |
| 305 | } |
| 306 | |
| 307 | template <class RealType, class Policy> |
| 308 | inline RealType skewness(const exponential_distribution<RealType, Policy>& /*dist*/) |
| 309 | { |
| 310 | return 2; |
| 311 | } |
| 312 | |
| 313 | template <class RealType, class Policy> |
| 314 | inline RealType kurtosis(const exponential_distribution<RealType, Policy>& /*dist*/) |
| 315 | { |
| 316 | return 9; |
| 317 | } |
| 318 | |
| 319 | template <class RealType, class Policy> |
| 320 | inline RealType kurtosis_excess(const exponential_distribution<RealType, Policy>& /*dist*/) |
| 321 | { |
| 322 | return 6; |
| 323 | } |
| 324 | |
| 325 | template <class RealType, class Policy> |
| 326 | inline RealType entropy(const exponential_distribution<RealType, Policy>& dist) |
| 327 | { |
| 328 | using std::log; |
| 329 | return 1 - log(dist.lambda()); |
| 330 | } |
| 331 | |
| 332 | } // namespace math |
| 333 | } // namespace boost |
| 334 | |
| 335 | #ifdef _MSC_VER |
| 336 | # pragma warning(pop) |
| 337 | #endif |
| 338 | |
| 339 | // This include must be at the end, *after* the accessors |
| 340 | // for this distribution have been defined, in order to |
| 341 | // keep compilers that support two-phase lookup happy. |
| 342 | #include <boost/math/distributions/detail/derived_accessors.hpp> |
| 343 | |
| 344 | #endif // BOOST_STATS_EXPONENTIAL_HPP |
| 345 | |