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_EXPONENTIAL_HPP
7 #define BOOST_STATS_EXPONENTIAL_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>
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).
25 namespace boost{ namespace math{
31 template <class RealType, class Policy>
32 inline bool verify_lambda(const char* function, RealType l, RealType* presult, const Policy& pol)
34 if((l <= 0) || !(boost::math::isfinite)(l))
36 *presult = policies::raise_domain_error<RealType>(
38 "The scale parameter \"lambda\" must be > 0, but was: %1%.", l, pol);
44 template <class RealType, class Policy>
45 inline bool verify_exp_x(const char* function, RealType x, RealType* presult, const Policy& pol)
47 if((x < 0) || (boost::math::isnan)(x))
49 *presult = policies::raise_domain_error<RealType>(
51 "The random variable must be >= 0, but was: %1%.", x, pol);
59 template <class RealType = double, class Policy = policies::policy<> >
60 class exponential_distribution
63 typedef RealType value_type;
64 typedef Policy policy_type;
66 exponential_distribution(RealType l_lambda = 1)
70 detail::verify_lambda("boost::math::exponential_distribution<%1%>::exponential_distribution", l_lambda, &err, Policy());
71 } // exponential_distribution
73 RealType lambda()const { return m_lambda; }
79 typedef exponential_distribution<double> exponential;
81 #ifdef __cpp_deduction_guides
82 template <class RealType>
83 exponential_distribution(RealType)->exponential_distribution<typename boost::math::tools::promote_args<RealType>::type>;
86 template <class RealType, class Policy>
87 inline const 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)
91 return std::pair<RealType, RealType>(static_cast<RealType>(0), std::numeric_limits<RealType>::infinity()); // 0 to + infinity.
95 using boost::math::tools::max_value;
96 return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>()); // 0 to + max
100 template <class RealType, class Policy>
101 inline const 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.
110 template <class RealType, class Policy>
111 inline RealType pdf(const exponential_distribution<RealType, Policy>& dist, const RealType& x)
113 BOOST_MATH_STD_USING // for ADL of std functions
115 static const char* function = "boost::math::pdf(const exponential_distribution<%1%>&, %1%)";
117 RealType lambda = dist.lambda();
119 if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
121 if(0 == detail::verify_exp_x(function, x, &result, Policy()))
123 // Workaround for VC11/12 bug:
124 if ((boost::math::isinf)(x))
126 result = lambda * exp(-lambda * x);
130 template <class RealType, class Policy>
131 inline RealType cdf(const exponential_distribution<RealType, Policy>& dist, const RealType& x)
133 BOOST_MATH_STD_USING // for ADL of std functions
135 static const char* function = "boost::math::cdf(const exponential_distribution<%1%>&, %1%)";
138 RealType lambda = dist.lambda();
139 if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
141 if(0 == detail::verify_exp_x(function, x, &result, Policy()))
143 result = -boost::math::expm1(-x * lambda, Policy());
148 template <class RealType, class Policy>
149 inline RealType quantile(const exponential_distribution<RealType, Policy>& dist, const RealType& p)
151 BOOST_MATH_STD_USING // for ADL of std functions
153 static const char* function = "boost::math::quantile(const exponential_distribution<%1%>&, %1%)";
156 RealType lambda = dist.lambda();
157 if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
159 if(0 == detail::check_probability(function, p, &result, Policy()))
165 return policies::raise_overflow_error<RealType>(function, 0, Policy());
167 result = -boost::math::log1p(-p, Policy()) / lambda;
171 template <class RealType, class Policy>
172 inline RealType cdf(const complemented2_type<exponential_distribution<RealType, Policy>, RealType>& c)
174 BOOST_MATH_STD_USING // for ADL of std functions
176 static const char* function = "boost::math::cdf(const exponential_distribution<%1%>&, %1%)";
179 RealType lambda = c.dist.lambda();
180 if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
182 if(0 == detail::verify_exp_x(function, c.param, &result, Policy()))
184 // Workaround for VC11/12 bug:
185 if (c.param >= tools::max_value<RealType>())
187 result = exp(-c.param * lambda);
192 template <class RealType, class Policy>
193 inline RealType quantile(const complemented2_type<exponential_distribution<RealType, Policy>, RealType>& c)
195 BOOST_MATH_STD_USING // for ADL of std functions
197 static const char* function = "boost::math::quantile(const exponential_distribution<%1%>&, %1%)";
200 RealType lambda = c.dist.lambda();
201 if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
204 RealType q = c.param;
205 if(0 == detail::check_probability(function, q, &result, Policy()))
211 return policies::raise_overflow_error<RealType>(function, 0, Policy());
213 result = -log(q) / lambda;
217 template <class RealType, class Policy>
218 inline RealType mean(const exponential_distribution<RealType, Policy>& dist)
221 RealType lambda = dist.lambda();
222 if(0 == detail::verify_lambda("boost::math::mean(const exponential_distribution<%1%>&)", lambda, &result, Policy()))
227 template <class RealType, class Policy>
228 inline RealType standard_deviation(const exponential_distribution<RealType, Policy>& dist)
231 RealType lambda = dist.lambda();
232 if(0 == detail::verify_lambda("boost::math::standard_deviation(const exponential_distribution<%1%>&)", lambda, &result, Policy()))
237 template <class RealType, class Policy>
238 inline RealType mode(const exponential_distribution<RealType, Policy>& /*dist*/)
243 template <class RealType, class Policy>
244 inline RealType median(const exponential_distribution<RealType, Policy>& dist)
246 using boost::math::constants::ln_two;
247 return ln_two<RealType>() / dist.lambda(); // ln(2) / lambda
250 template <class RealType, class Policy>
251 inline RealType skewness(const exponential_distribution<RealType, Policy>& /*dist*/)
256 template <class RealType, class Policy>
257 inline RealType kurtosis(const exponential_distribution<RealType, Policy>& /*dist*/)
262 template <class RealType, class Policy>
263 inline RealType kurtosis_excess(const exponential_distribution<RealType, Policy>& /*dist*/)
268 template <class RealType, class Policy>
269 inline RealType entropy(const exponential_distribution<RealType, Policy>& dist)
272 return 1 - log(dist.lambda());
279 # pragma warning(pop)
282 // This include must be at the end, *after* the accessors
283 // for this distribution have been defined, in order to
284 // keep compilers that support two-phase lookup happy.
285 #include <boost/math/distributions/detail/derived_accessors.hpp>
287 #endif // BOOST_STATS_EXPONENTIAL_HPP