]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/math/distributions/laplace.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / math / distributions / laplace.hpp
CommitLineData
7c673cae
FG
1// Copyright Thijs van den Berg, 2008.
2// Copyright John Maddock 2008.
3// Copyright Paul A. Bristow 2008, 2014.
4
5// Use, modification and distribution are subject to the
6// Boost Software License, Version 1.0. (See accompanying file
7// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8
9// This module implements the Laplace distribution.
10// Weisstein, Eric W. "Laplace Distribution." From MathWorld--A Wolfram Web Resource.
11// http://mathworld.wolfram.com/LaplaceDistribution.html
12// http://en.wikipedia.org/wiki/Laplace_distribution
13//
14// Abramowitz and Stegun 1972, p 930
15// http://www.math.sfu.ca/~cbm/aands/page_930.htm
16
17#ifndef BOOST_STATS_LAPLACE_HPP
18#define BOOST_STATS_LAPLACE_HPP
19
20#include <boost/math/distributions/detail/common_error_handling.hpp>
21#include <boost/math/distributions/complement.hpp>
22#include <boost/math/constants/constants.hpp>
23#include <limits>
24
25namespace boost{ namespace math{
26
1e59de90 27#ifdef _MSC_VER
7c673cae
FG
28# pragma warning(push)
29# pragma warning(disable:4127) // conditional expression is constant
30#endif
31
32template <class RealType = double, class Policy = policies::policy<> >
33class laplace_distribution
34{
35public:
36 // ----------------------------------
37 // public Types
38 // ----------------------------------
39 typedef RealType value_type;
40 typedef Policy policy_type;
41
42 // ----------------------------------
43 // Constructor(s)
44 // ----------------------------------
45 laplace_distribution(RealType l_location = 0, RealType l_scale = 1)
46 : m_location(l_location), m_scale(l_scale)
47 {
48 RealType result;
49 check_parameters("boost::math::laplace_distribution<%1%>::laplace_distribution()", &result);
50 }
51
52
53 // ----------------------------------
54 // Public functions
55 // ----------------------------------
56
57 RealType location() const
58 {
59 return m_location;
60 }
61
62 RealType scale() const
63 {
64 return m_scale;
65 }
66
67 bool check_parameters(const char* function, RealType* result) const
68 {
69 if(false == detail::check_scale(function, m_scale, result, Policy())) return false;
70 if(false == detail::check_location(function, m_location, result, Policy())) return false;
71 return true;
72 }
73
74private:
75 RealType m_location;
76 RealType m_scale;
77}; // class laplace_distribution
78
79//
80// Convenient type synonym for double.
81typedef laplace_distribution<double> laplace;
82
1e59de90
TL
83#ifdef __cpp_deduction_guides
84template <class RealType>
85laplace_distribution(RealType)->laplace_distribution<typename boost::math::tools::promote_args<RealType>::type>;
86template <class RealType>
87laplace_distribution(RealType,RealType)->laplace_distribution<typename boost::math::tools::promote_args<RealType>::type>;
88#endif
89
7c673cae
FG
90//
91// Non-member functions.
92template <class RealType, class Policy>
93inline const std::pair<RealType, RealType> range(const laplace_distribution<RealType, Policy>&)
94{
95 if (std::numeric_limits<RealType>::has_infinity)
96 { // Can use infinity.
97 return std::pair<RealType, RealType>(-std::numeric_limits<RealType>::infinity(), std::numeric_limits<RealType>::infinity()); // - to + infinity.
98 }
99 else
100 { // Can only use max_value.
101 using boost::math::tools::max_value;
102 return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + max value.
103 }
104
105}
106
107template <class RealType, class Policy>
108inline const std::pair<RealType, RealType> support(const laplace_distribution<RealType, Policy>&)
109{
110 if (std::numeric_limits<RealType>::has_infinity)
111 { // Can Use infinity.
112 return std::pair<RealType, RealType>(-std::numeric_limits<RealType>::infinity(), std::numeric_limits<RealType>::infinity()); // - to + infinity.
113 }
114 else
115 { // Can only use max_value.
116 using boost::math::tools::max_value;
117 return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + max value.
118 }
119}
120
121template <class RealType, class Policy>
122inline RealType pdf(const laplace_distribution<RealType, Policy>& dist, const RealType& x)
123{
124 BOOST_MATH_STD_USING // for ADL of std functions
125
126 // Checking function argument
127 RealType result = 0;
128 const char* function = "boost::math::pdf(const laplace_distribution<%1%>&, %1%))";
129
130 // Check scale and location.
131 if (false == dist.check_parameters(function, &result)) return result;
132 // Special pdf values.
133 if((boost::math::isinf)(x))
134 {
135 return 0; // pdf + and - infinity is zero.
136 }
137 if (false == detail::check_x(function, x, &result, Policy())) return result;
138
139 // General case
140 RealType scale( dist.scale() );
141 RealType location( dist.location() );
142
143 RealType exponent = x - location;
144 if (exponent>0) exponent = -exponent;
145 exponent /= scale;
146
147 result = exp(exponent);
148 result /= 2 * scale;
149
150 return result;
151} // pdf
152
153template <class RealType, class Policy>
154inline RealType cdf(const laplace_distribution<RealType, Policy>& dist, const RealType& x)
155{
156 BOOST_MATH_STD_USING // For ADL of std functions.
157
158 RealType result = 0;
159 // Checking function argument.
160 const char* function = "boost::math::cdf(const laplace_distribution<%1%>&, %1%)";
161 // Check scale and location.
162 if (false == dist.check_parameters(function, &result)) return result;
163
164 // Special cdf values:
165 if((boost::math::isinf)(x))
166 {
167 if(x < 0) return 0; // -infinity.
168 return 1; // + infinity.
169 }
170 if (false == detail::check_x(function, x, &result, Policy())) return result;
171
172 // General cdf values
173 RealType scale( dist.scale() );
174 RealType location( dist.location() );
175
176 if (x < location)
177 {
178 result = exp( (x-location)/scale )/2;
179 }
180 else
181 {
182 result = 1 - exp( (location-x)/scale )/2;
183 }
184 return result;
185} // cdf
186
187
188template <class RealType, class Policy>
189inline RealType quantile(const laplace_distribution<RealType, Policy>& dist, const RealType& p)
190{
191 BOOST_MATH_STD_USING // for ADL of std functions.
192
193 // Checking function argument
194 RealType result = 0;
195 const char* function = "boost::math::quantile(const laplace_distribution<%1%>&, %1%)";
196 if (false == dist.check_parameters(function, &result)) return result;
197 if(false == detail::check_probability(function, p, &result, Policy())) return result;
198
199 // Extreme values of p:
200 if(p == 0)
201 {
202 result = policies::raise_overflow_error<RealType>(function,
203 "probability parameter is 0, but must be > 0!", Policy());
204 return -result; // -std::numeric_limits<RealType>::infinity();
205 }
206
207 if(p == 1)
208 {
209 result = policies::raise_overflow_error<RealType>(function,
210 "probability parameter is 1, but must be < 1!", Policy());
211 return result; // std::numeric_limits<RealType>::infinity();
212 }
213 // Calculate Quantile
214 RealType scale( dist.scale() );
215 RealType location( dist.location() );
216
217 if (p - 0.5 < 0.0)
218 result = location + scale*log( static_cast<RealType>(p*2) );
219 else
220 result = location - scale*log( static_cast<RealType>(-p*2 + 2) );
221
222 return result;
223} // quantile
224
225
226template <class RealType, class Policy>
227inline RealType cdf(const complemented2_type<laplace_distribution<RealType, Policy>, RealType>& c)
228{
229 // Calculate complement of cdf.
230 BOOST_MATH_STD_USING // for ADL of std functions
231
232 RealType scale = c.dist.scale();
233 RealType location = c.dist.location();
234 RealType x = c.param;
235 RealType result = 0;
236
237 // Checking function argument.
238 const char* function = "boost::math::cdf(const complemented2_type<laplace_distribution<%1%>, %1%>&)";
239
240 // Check scale and location.
241 //if(false == detail::check_scale(function, scale, result, Policy())) return false;
242 //if(false == detail::check_location(function, location, result, Policy())) return false;
243 if (false == c.dist.check_parameters(function, &result)) return result;
244
245 // Special cdf values.
246 if((boost::math::isinf)(x))
247 {
248 if(x < 0) return 1; // cdf complement -infinity is unity.
249 return 0; // cdf complement +infinity is zero.
250 }
251 if(false == detail::check_x(function, x, &result, Policy()))return result;
252
253 // Cdf interval value.
254 if (-x < -location)
255 {
256 result = exp( (-x+location)/scale )/2;
257 }
258 else
259 {
260 result = 1 - exp( (-location+x)/scale )/2;
261 }
262 return result;
263} // cdf complement
264
265
266template <class RealType, class Policy>
267inline RealType quantile(const complemented2_type<laplace_distribution<RealType, Policy>, RealType>& c)
268{
269 BOOST_MATH_STD_USING // for ADL of std functions.
270
271 // Calculate quantile.
272 RealType scale = c.dist.scale();
273 RealType location = c.dist.location();
274 RealType q = c.param;
275 RealType result = 0;
276
277 // Checking function argument.
278 const char* function = "quantile(const complemented2_type<laplace_distribution<%1%>, %1%>&)";
279 if (false == c.dist.check_parameters(function, &result)) return result;
280
281 // Extreme values.
282 if(q == 0)
283 {
284 return std::numeric_limits<RealType>::infinity();
285 }
286 if(q == 1)
287 {
288 return -std::numeric_limits<RealType>::infinity();
289 }
290 if(false == detail::check_probability(function, q, &result, Policy())) return result;
291
292 if (0.5 - q < 0.0)
293 result = location + scale*log( static_cast<RealType>(-q*2 + 2) );
294 else
295 result = location - scale*log( static_cast<RealType>(q*2) );
296
297
298 return result;
299} // quantile
300
301template <class RealType, class Policy>
302inline RealType mean(const laplace_distribution<RealType, Policy>& dist)
303{
304 return dist.location();
305}
306
307template <class RealType, class Policy>
308inline RealType standard_deviation(const laplace_distribution<RealType, Policy>& dist)
309{
310 return constants::root_two<RealType>() * dist.scale();
311}
312
313template <class RealType, class Policy>
314inline RealType mode(const laplace_distribution<RealType, Policy>& dist)
315{
316 return dist.location();
317}
318
319template <class RealType, class Policy>
320inline RealType median(const laplace_distribution<RealType, Policy>& dist)
321{
322 return dist.location();
323}
324
325template <class RealType, class Policy>
326inline RealType skewness(const laplace_distribution<RealType, Policy>& /*dist*/)
327{
328 return 0;
329}
330
331template <class RealType, class Policy>
332inline RealType kurtosis(const laplace_distribution<RealType, Policy>& /*dist*/)
333{
334 return 6;
335}
336
337template <class RealType, class Policy>
338inline RealType kurtosis_excess(const laplace_distribution<RealType, Policy>& /*dist*/)
339{
340 return 3;
341}
342
f67539c2
TL
343template <class RealType, class Policy>
344inline RealType entropy(const laplace_distribution<RealType, Policy> & dist)
345{
346 using std::log;
347 return log(2*dist.scale()*constants::e<RealType>());
348}
349
1e59de90 350#ifdef _MSC_VER
7c673cae
FG
351# pragma warning(pop)
352#endif
353
354} // namespace math
355} // namespace boost
356
357// This include must be at the end, *after* the accessors
358// for this distribution have been defined, in order to
359// keep compilers that support two-phase lookup happy.
360#include <boost/math/distributions/detail/derived_accessors.hpp>
361
362#endif // BOOST_STATS_LAPLACE_HPP
363
364