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>
15 #include <boost/config/no_tr1/cmath.hpp>
18 # pragma warning(push)
19 # pragma warning(disable: 4127) // conditional expression is constant
20 # 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 template <class RealType, class Policy>
82 inline const std::pair<RealType, RealType> range(const exponential_distribution<RealType, Policy>& /*dist*/)
83 { // Range of permissible values for random variable x.
84 if (std::numeric_limits<RealType>::has_infinity)
86 return std::pair<RealType, RealType>(static_cast<RealType>(0), std::numeric_limits<RealType>::infinity()); // 0 to + infinity.
90 using boost::math::tools::max_value;
91 return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>()); // 0 to + max
95 template <class RealType, class Policy>
96 inline const std::pair<RealType, RealType> support(const exponential_distribution<RealType, Policy>& /*dist*/)
97 { // Range of supported values for random variable x.
98 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
99 using boost::math::tools::max_value;
100 using boost::math::tools::min_value;
101 return std::pair<RealType, RealType>(min_value<RealType>(), max_value<RealType>());
102 // min_value<RealType>() to avoid a discontinuity at x = 0.
105 template <class RealType, class Policy>
106 inline RealType pdf(const exponential_distribution<RealType, Policy>& dist, const RealType& x)
108 BOOST_MATH_STD_USING // for ADL of std functions
110 static const char* function = "boost::math::pdf(const exponential_distribution<%1%>&, %1%)";
112 RealType lambda = dist.lambda();
114 if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
116 if(0 == detail::verify_exp_x(function, x, &result, Policy()))
118 // Workaround for VC11/12 bug:
119 if ((boost::math::isinf)(x))
121 result = lambda * exp(-lambda * x);
125 template <class RealType, class Policy>
126 inline RealType cdf(const exponential_distribution<RealType, Policy>& dist, const RealType& x)
128 BOOST_MATH_STD_USING // for ADL of std functions
130 static const char* function = "boost::math::cdf(const exponential_distribution<%1%>&, %1%)";
133 RealType lambda = dist.lambda();
134 if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
136 if(0 == detail::verify_exp_x(function, x, &result, Policy()))
138 result = -boost::math::expm1(-x * lambda, Policy());
143 template <class RealType, class Policy>
144 inline RealType quantile(const exponential_distribution<RealType, Policy>& dist, const RealType& p)
146 BOOST_MATH_STD_USING // for ADL of std functions
148 static const char* function = "boost::math::quantile(const exponential_distribution<%1%>&, %1%)";
151 RealType lambda = dist.lambda();
152 if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
154 if(0 == detail::check_probability(function, p, &result, Policy()))
160 return policies::raise_overflow_error<RealType>(function, 0, Policy());
162 result = -boost::math::log1p(-p, Policy()) / lambda;
166 template <class RealType, class Policy>
167 inline RealType cdf(const complemented2_type<exponential_distribution<RealType, Policy>, RealType>& c)
169 BOOST_MATH_STD_USING // for ADL of std functions
171 static const char* function = "boost::math::cdf(const exponential_distribution<%1%>&, %1%)";
174 RealType lambda = c.dist.lambda();
175 if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
177 if(0 == detail::verify_exp_x(function, c.param, &result, Policy()))
179 // Workaround for VC11/12 bug:
180 if (c.param >= tools::max_value<RealType>())
182 result = exp(-c.param * lambda);
187 template <class RealType, class Policy>
188 inline RealType quantile(const complemented2_type<exponential_distribution<RealType, Policy>, RealType>& c)
190 BOOST_MATH_STD_USING // for ADL of std functions
192 static const char* function = "boost::math::quantile(const exponential_distribution<%1%>&, %1%)";
195 RealType lambda = c.dist.lambda();
196 if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
199 RealType q = c.param;
200 if(0 == detail::check_probability(function, q, &result, Policy()))
206 return policies::raise_overflow_error<RealType>(function, 0, Policy());
208 result = -log(q) / lambda;
212 template <class RealType, class Policy>
213 inline RealType mean(const exponential_distribution<RealType, Policy>& dist)
216 RealType lambda = dist.lambda();
217 if(0 == detail::verify_lambda("boost::math::mean(const exponential_distribution<%1%>&)", lambda, &result, Policy()))
222 template <class RealType, class Policy>
223 inline RealType standard_deviation(const exponential_distribution<RealType, Policy>& dist)
226 RealType lambda = dist.lambda();
227 if(0 == detail::verify_lambda("boost::math::standard_deviation(const exponential_distribution<%1%>&)", lambda, &result, Policy()))
232 template <class RealType, class Policy>
233 inline RealType mode(const exponential_distribution<RealType, Policy>& /*dist*/)
238 template <class RealType, class Policy>
239 inline RealType median(const exponential_distribution<RealType, Policy>& dist)
241 using boost::math::constants::ln_two;
242 return ln_two<RealType>() / dist.lambda(); // ln(2) / lambda
245 template <class RealType, class Policy>
246 inline RealType skewness(const exponential_distribution<RealType, Policy>& /*dist*/)
251 template <class RealType, class Policy>
252 inline RealType kurtosis(const exponential_distribution<RealType, Policy>& /*dist*/)
257 template <class RealType, class Policy>
258 inline RealType kurtosis_excess(const exponential_distribution<RealType, Policy>& /*dist*/)
267 # pragma warning(pop)
270 // This include must be at the end, *after* the accessors
271 // for this distribution have been defined, in order to
272 // keep compilers that support two-phase lookup happy.
273 #include <boost/math/distributions/detail/derived_accessors.hpp>
275 #endif // BOOST_STATS_EXPONENTIAL_HPP