1
// Copyright Thijs van den Berg, 2008.
2
// Copyright John Maddock 2008.
3
// Copyright Paul A. Bristow 2008.
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)
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
14
// Abramowitz and Stegun 1972, p 930
15
// http://www.math.sfu.ca/~cbm/aands/page_930.htm
17
#ifndef BOOST_STATS_LAPLACE_HPP
18
#define BOOST_STATS_LAPLACE_HPP
20
#include <boost/math/distributions/detail/common_error_handling.hpp>
21
#include <boost/math/distributions/complement.hpp>
22
#include <boost/math/constants/constants.hpp>
25
namespace boost{ namespace math{
27
template <class RealType = double, class Policy = policies::policy<> >
28
class laplace_distribution
31
// ----------------------------------
33
// ----------------------------------
34
typedef RealType value_type;
35
typedef Policy policy_type;
37
// ----------------------------------
39
// ----------------------------------
40
laplace_distribution(RealType location = 0, RealType scale = 1)
41
: m_location(location), m_scale(scale)
44
check_parameters("boost::math::laplace_distribution<%1%>::laplace_distribution()", &result);
48
// ----------------------------------
50
// ----------------------------------
52
RealType location() const
57
RealType scale() const
62
bool check_parameters(const char* function, RealType* result) const
64
if(false == detail::check_scale(function, m_scale, result, Policy())) return false;
65
if(false == detail::check_location(function, m_location, result, Policy())) return false;
74
}; // class laplace_distribution
79
// Convenient type synonym
81
typedef laplace_distribution<double> laplace;
84
// Non member functions
86
template <class RealType, class Policy>
87
inline const std::pair<RealType, RealType> range(const laplace_distribution<RealType, Policy>&)
89
using boost::math::tools::max_value;
90
return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>());
93
template <class RealType, class Policy>
94
inline const std::pair<RealType, RealType> support(const laplace_distribution<RealType, Policy>&)
96
using boost::math::tools::max_value;
97
return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>());
100
template <class RealType, class Policy>
101
inline RealType pdf(const laplace_distribution<RealType, Policy>& dist, const RealType& x)
103
BOOST_MATH_STD_USING // for ADL of std functions
105
// Checking function argument
107
const char* function = "boost::math::pdf(const laplace_distribution<%1%>&, %1%))";
108
if (false == dist.check_parameters(function, &result)) return result;
109
if (false == detail::check_x(function, x, &result, Policy())) return result;
111
// Special pdf values
112
if((boost::math::isinf)(x))
113
return 0; // pdf + and - infinity is zero.
116
RealType scale( dist.scale() );
117
RealType location( dist.location() );
119
RealType exponent = x - location;
120
if (exponent>0) exponent = -exponent;
123
result = exp(exponent);
129
template <class RealType, class Policy>
130
inline RealType cdf(const laplace_distribution<RealType, Policy>& dist, const RealType& x)
132
BOOST_MATH_STD_USING // for ADL of std functions
134
// Checking function argument
136
const char* function = "boost::math::cdf(const laplace_distribution<%1%>&, %1%)";
137
if (false == dist.check_parameters(function, &result)) return result;
138
if (false == detail::check_x(function, x, &result, Policy())) return result;
140
// Special cdf values
141
if((boost::math::isinf)(x))
143
if(x < 0) return 0; // -infinity
144
return 1; // + infinity
147
// General cdf values
148
RealType scale( dist.scale() );
149
RealType location( dist.location() );
152
result = exp( (x-location)/scale )/2;
154
result = 1 - exp( (location-x)/scale )/2;
160
template <class RealType, class Policy>
161
inline RealType quantile(const laplace_distribution<RealType, Policy>& dist, const RealType& p)
163
BOOST_MATH_STD_USING // for ADL of std functions
165
// Checking function argument
167
const char* function = "boost::math::quantile(const laplace_distribution<%1%>&, %1%)";
168
if (false == dist.check_parameters(function, &result)) return result;
169
if(false == detail::check_probability(function, p, &result, Policy())) return result;
172
if(p == 0) return -std::numeric_limits<RealType>::infinity();
173
if(p == 1) return std::numeric_limits<RealType>::infinity();
175
// Calculate Quantile
176
RealType scale( dist.scale() );
177
RealType location( dist.location() );
180
result = location + scale*log( static_cast<RealType>(p*2) );
182
result = location - scale*log( static_cast<RealType>(-p*2 + 2) );
188
template <class RealType, class Policy>
189
inline RealType cdf(const complemented2_type<laplace_distribution<RealType, Policy>, RealType>& c)
191
BOOST_MATH_STD_USING // for ADL of std functions
193
RealType scale = c.dist.scale();
194
RealType location = c.dist.location();
195
RealType x = c.param;
197
// Checking function argument
199
const char* function = "boost::math::cdf(const complemented2_type<laplace_distribution<%1%>, %1%>&)";
200
if(false == detail::check_x(function, x, &result, Policy()))return result;
205
if((boost::math::isinf)(x))
207
if(x < 0) return 1; // cdf complement -infinity is unity.
208
return 0; // cdf complement +infinity is zero
211
// Cdf interval value
213
result = exp( (-x-location)/scale )/2;
215
result = 1 - exp( (location+x)/scale )/2;
221
template <class RealType, class Policy>
222
inline RealType quantile(const complemented2_type<laplace_distribution<RealType, Policy>, RealType>& c)
224
BOOST_MATH_STD_USING // for ADL of std functions
226
// Calculate quantile
227
RealType scale = c.dist.scale();
228
RealType location = c.dist.location();
229
RealType q = c.param;
231
// Checking function argument
233
const char* function = "quantile(const complemented2_type<laplace_distribution<%1%>, %1%>&)";
234
if(false == detail::check_probability(function, q, &result, Policy())) return result;
238
if(q == 0) return std::numeric_limits<RealType>::infinity();
239
if(q == 1) return -std::numeric_limits<RealType>::infinity();
242
result = location + scale*log( static_cast<RealType>(-q*2 + 2) );
244
result = location - scale*log( static_cast<RealType>(q*2) );
250
template <class RealType, class Policy>
251
inline RealType mean(const laplace_distribution<RealType, Policy>& dist)
253
return dist.location();
256
template <class RealType, class Policy>
257
inline RealType standard_deviation(const laplace_distribution<RealType, Policy>& dist)
259
return constants::root_two<RealType>() * dist.scale();
262
template <class RealType, class Policy>
263
inline RealType mode(const laplace_distribution<RealType, Policy>& dist)
265
return dist.location();
268
template <class RealType, class Policy>
269
inline RealType median(const laplace_distribution<RealType, Policy>& dist)
271
return dist.location();
274
template <class RealType, class Policy>
275
inline RealType skewness(const laplace_distribution<RealType, Policy>& /*dist*/)
280
template <class RealType, class Policy>
281
inline RealType kurtosis(const laplace_distribution<RealType, Policy>& /*dist*/)
286
template <class RealType, class Policy>
287
inline RealType kurtosis_excess(const laplace_distribution<RealType, Policy>& /*dist*/)
295
// This include must be at the end, *after* the accessors
296
// for this distribution have been defined, in order to
297
// keep compilers that support two-phase lookup happy.
298
#include <boost/math/distributions/detail/derived_accessors.hpp>
300
#endif // BOOST_STATS_LAPLACE_HPP