]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/math/distributions/exponential.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / math / distributions / exponential.hpp
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 typedef RealType value_type;
64 typedef Policy policy_type;
65
66 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 typedef exponential_distribution<double> exponential;
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 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)
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 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.
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 cdf(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::cdf(const exponential_distribution<%1%>&, %1%)";
136
137 RealType result = 0;
138 RealType lambda = dist.lambda();
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 result = -boost::math::expm1(-x * lambda, Policy());
144
145 return result;
146 } // cdf
147
148 template <class RealType, class Policy>
149 inline RealType quantile(const exponential_distribution<RealType, Policy>& dist, const RealType& p)
150 {
151 BOOST_MATH_STD_USING // for ADL of std functions
152
153 static const char* function = "boost::math::quantile(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::check_probability(function, p, &result, Policy()))
160 return result;
161
162 if(p == 0)
163 return 0;
164 if(p == 1)
165 return policies::raise_overflow_error<RealType>(function, 0, Policy());
166
167 result = -boost::math::log1p(-p, Policy()) / lambda;
168 return result;
169 } // quantile
170
171 template <class RealType, class Policy>
172 inline RealType cdf(const complemented2_type<exponential_distribution<RealType, Policy>, RealType>& c)
173 {
174 BOOST_MATH_STD_USING // for ADL of std functions
175
176 static const char* function = "boost::math::cdf(const exponential_distribution<%1%>&, %1%)";
177
178 RealType result = 0;
179 RealType lambda = c.dist.lambda();
180 if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
181 return result;
182 if(0 == detail::verify_exp_x(function, c.param, &result, Policy()))
183 return result;
184 // Workaround for VC11/12 bug:
185 if (c.param >= tools::max_value<RealType>())
186 return 0;
187 result = exp(-c.param * lambda);
188
189 return result;
190 }
191
192 template <class RealType, class Policy>
193 inline RealType quantile(const complemented2_type<exponential_distribution<RealType, Policy>, RealType>& c)
194 {
195 BOOST_MATH_STD_USING // for ADL of std functions
196
197 static const char* function = "boost::math::quantile(const exponential_distribution<%1%>&, %1%)";
198
199 RealType result = 0;
200 RealType lambda = c.dist.lambda();
201 if(0 == detail::verify_lambda(function, lambda, &result, Policy()))
202 return result;
203
204 RealType q = c.param;
205 if(0 == detail::check_probability(function, q, &result, Policy()))
206 return result;
207
208 if(q == 1)
209 return 0;
210 if(q == 0)
211 return policies::raise_overflow_error<RealType>(function, 0, Policy());
212
213 result = -log(q) / lambda;
214 return result;
215 }
216
217 template <class RealType, class Policy>
218 inline RealType mean(const exponential_distribution<RealType, Policy>& dist)
219 {
220 RealType result = 0;
221 RealType lambda = dist.lambda();
222 if(0 == detail::verify_lambda("boost::math::mean(const exponential_distribution<%1%>&)", lambda, &result, Policy()))
223 return result;
224 return 1 / lambda;
225 }
226
227 template <class RealType, class Policy>
228 inline RealType standard_deviation(const exponential_distribution<RealType, Policy>& dist)
229 {
230 RealType result = 0;
231 RealType lambda = dist.lambda();
232 if(0 == detail::verify_lambda("boost::math::standard_deviation(const exponential_distribution<%1%>&)", lambda, &result, Policy()))
233 return result;
234 return 1 / lambda;
235 }
236
237 template <class RealType, class Policy>
238 inline RealType mode(const exponential_distribution<RealType, Policy>& /*dist*/)
239 {
240 return 0;
241 }
242
243 template <class RealType, class Policy>
244 inline RealType median(const exponential_distribution<RealType, Policy>& dist)
245 {
246 using boost::math::constants::ln_two;
247 return ln_two<RealType>() / dist.lambda(); // ln(2) / lambda
248 }
249
250 template <class RealType, class Policy>
251 inline RealType skewness(const exponential_distribution<RealType, Policy>& /*dist*/)
252 {
253 return 2;
254 }
255
256 template <class RealType, class Policy>
257 inline RealType kurtosis(const exponential_distribution<RealType, Policy>& /*dist*/)
258 {
259 return 9;
260 }
261
262 template <class RealType, class Policy>
263 inline RealType kurtosis_excess(const exponential_distribution<RealType, Policy>& /*dist*/)
264 {
265 return 6;
266 }
267
268 template <class RealType, class Policy>
269 inline RealType entropy(const exponential_distribution<RealType, Policy>& dist)
270 {
271 using std::log;
272 return 1 - log(dist.lambda());
273 }
274
275 } // namespace math
276 } // namespace boost
277
278 #ifdef _MSC_VER
279 # pragma warning(pop)
280 #endif
281
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>
286
287 #endif // BOOST_STATS_EXPONENTIAL_HPP