1 // Copyright Thijs van den Berg, 2008.
2 // Copyright John Maddock 2008.
3 // Copyright Paul A. Bristow 2008, 2014.
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{
28 # pragma warning(push)
29 # pragma warning(disable:4127) // conditional expression is constant
32 template <class RealType = double, class Policy = policies::policy<> >
33 class laplace_distribution
36 // ----------------------------------
38 // ----------------------------------
39 typedef RealType value_type;
40 typedef Policy policy_type;
42 // ----------------------------------
44 // ----------------------------------
45 laplace_distribution(RealType l_location = 0, RealType l_scale = 1)
46 : m_location(l_location), m_scale(l_scale)
49 check_parameters("boost::math::laplace_distribution<%1%>::laplace_distribution()", &result);
53 // ----------------------------------
55 // ----------------------------------
57 RealType location() const
62 RealType scale() const
67 bool check_parameters(const char* function, RealType* result) const
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;
77 }; // class laplace_distribution
80 // Convenient type synonym for double.
81 typedef laplace_distribution<double> laplace;
84 // Non-member functions.
85 template <class RealType, class Policy>
86 inline const std::pair<RealType, RealType> range(const laplace_distribution<RealType, Policy>&)
88 if (std::numeric_limits<RealType>::has_infinity)
89 { // Can use infinity.
90 return std::pair<RealType, RealType>(-std::numeric_limits<RealType>::infinity(), std::numeric_limits<RealType>::infinity()); // - to + infinity.
93 { // Can only use max_value.
94 using boost::math::tools::max_value;
95 return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + max value.
100 template <class RealType, class Policy>
101 inline const std::pair<RealType, RealType> support(const laplace_distribution<RealType, Policy>&)
103 if (std::numeric_limits<RealType>::has_infinity)
104 { // Can Use infinity.
105 return std::pair<RealType, RealType>(-std::numeric_limits<RealType>::infinity(), std::numeric_limits<RealType>::infinity()); // - to + infinity.
108 { // Can only use max_value.
109 using boost::math::tools::max_value;
110 return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + max value.
114 template <class RealType, class Policy>
115 inline RealType pdf(const laplace_distribution<RealType, Policy>& dist, const RealType& x)
117 BOOST_MATH_STD_USING // for ADL of std functions
119 // Checking function argument
121 const char* function = "boost::math::pdf(const laplace_distribution<%1%>&, %1%))";
123 // Check scale and location.
124 if (false == dist.check_parameters(function, &result)) return result;
125 // Special pdf values.
126 if((boost::math::isinf)(x))
128 return 0; // pdf + and - infinity is zero.
130 if (false == detail::check_x(function, x, &result, Policy())) return result;
133 RealType scale( dist.scale() );
134 RealType location( dist.location() );
136 RealType exponent = x - location;
137 if (exponent>0) exponent = -exponent;
140 result = exp(exponent);
146 template <class RealType, class Policy>
147 inline RealType cdf(const laplace_distribution<RealType, Policy>& dist, const RealType& x)
149 BOOST_MATH_STD_USING // For ADL of std functions.
152 // Checking function argument.
153 const char* function = "boost::math::cdf(const laplace_distribution<%1%>&, %1%)";
154 // Check scale and location.
155 if (false == dist.check_parameters(function, &result)) return result;
157 // Special cdf values:
158 if((boost::math::isinf)(x))
160 if(x < 0) return 0; // -infinity.
161 return 1; // + infinity.
163 if (false == detail::check_x(function, x, &result, Policy())) return result;
165 // General cdf values
166 RealType scale( dist.scale() );
167 RealType location( dist.location() );
171 result = exp( (x-location)/scale )/2;
175 result = 1 - exp( (location-x)/scale )/2;
181 template <class RealType, class Policy>
182 inline RealType quantile(const laplace_distribution<RealType, Policy>& dist, const RealType& p)
184 BOOST_MATH_STD_USING // for ADL of std functions.
186 // Checking function argument
188 const char* function = "boost::math::quantile(const laplace_distribution<%1%>&, %1%)";
189 if (false == dist.check_parameters(function, &result)) return result;
190 if(false == detail::check_probability(function, p, &result, Policy())) return result;
192 // Extreme values of p:
195 result = policies::raise_overflow_error<RealType>(function,
196 "probability parameter is 0, but must be > 0!", Policy());
197 return -result; // -std::numeric_limits<RealType>::infinity();
202 result = policies::raise_overflow_error<RealType>(function,
203 "probability parameter is 1, but must be < 1!", Policy());
204 return result; // std::numeric_limits<RealType>::infinity();
206 // Calculate Quantile
207 RealType scale( dist.scale() );
208 RealType location( dist.location() );
211 result = location + scale*log( static_cast<RealType>(p*2) );
213 result = location - scale*log( static_cast<RealType>(-p*2 + 2) );
219 template <class RealType, class Policy>
220 inline RealType cdf(const complemented2_type<laplace_distribution<RealType, Policy>, RealType>& c)
222 // Calculate complement of cdf.
223 BOOST_MATH_STD_USING // for ADL of std functions
225 RealType scale = c.dist.scale();
226 RealType location = c.dist.location();
227 RealType x = c.param;
230 // Checking function argument.
231 const char* function = "boost::math::cdf(const complemented2_type<laplace_distribution<%1%>, %1%>&)";
233 // Check scale and location.
234 //if(false == detail::check_scale(function, scale, result, Policy())) return false;
235 //if(false == detail::check_location(function, location, result, Policy())) return false;
236 if (false == c.dist.check_parameters(function, &result)) return result;
238 // Special cdf values.
239 if((boost::math::isinf)(x))
241 if(x < 0) return 1; // cdf complement -infinity is unity.
242 return 0; // cdf complement +infinity is zero.
244 if(false == detail::check_x(function, x, &result, Policy()))return result;
246 // Cdf interval value.
249 result = exp( (-x+location)/scale )/2;
253 result = 1 - exp( (-location+x)/scale )/2;
259 template <class RealType, class Policy>
260 inline RealType quantile(const complemented2_type<laplace_distribution<RealType, Policy>, RealType>& c)
262 BOOST_MATH_STD_USING // for ADL of std functions.
264 // Calculate quantile.
265 RealType scale = c.dist.scale();
266 RealType location = c.dist.location();
267 RealType q = c.param;
270 // Checking function argument.
271 const char* function = "quantile(const complemented2_type<laplace_distribution<%1%>, %1%>&)";
272 if (false == c.dist.check_parameters(function, &result)) return result;
277 return std::numeric_limits<RealType>::infinity();
281 return -std::numeric_limits<RealType>::infinity();
283 if(false == detail::check_probability(function, q, &result, Policy())) return result;
286 result = location + scale*log( static_cast<RealType>(-q*2 + 2) );
288 result = location - scale*log( static_cast<RealType>(q*2) );
294 template <class RealType, class Policy>
295 inline RealType mean(const laplace_distribution<RealType, Policy>& dist)
297 return dist.location();
300 template <class RealType, class Policy>
301 inline RealType standard_deviation(const laplace_distribution<RealType, Policy>& dist)
303 return constants::root_two<RealType>() * dist.scale();
306 template <class RealType, class Policy>
307 inline RealType mode(const laplace_distribution<RealType, Policy>& dist)
309 return dist.location();
312 template <class RealType, class Policy>
313 inline RealType median(const laplace_distribution<RealType, Policy>& dist)
315 return dist.location();
318 template <class RealType, class Policy>
319 inline RealType skewness(const laplace_distribution<RealType, Policy>& /*dist*/)
324 template <class RealType, class Policy>
325 inline RealType kurtosis(const laplace_distribution<RealType, Policy>& /*dist*/)
330 template <class RealType, class Policy>
331 inline RealType kurtosis_excess(const laplace_distribution<RealType, Policy>& /*dist*/)
337 # pragma warning(pop)
343 // This include must be at the end, *after* the accessors
344 // for this distribution have been defined, in order to
345 // keep compilers that support two-phase lookup happy.
346 #include <boost/math/distributions/detail/derived_accessors.hpp>
348 #endif // BOOST_STATS_LAPLACE_HPP