~twpol/dcplusplus/trunk

« back to all changes in this revision

Viewing changes to boost/boost/math/distributions/laplace.hpp

  • Committer: James Ross
  • Date: 2010-07-05 00:03:18 UTC
  • mfrom: (1524.1.650 dcplusplus)
  • Revision ID: silver@warwickcompsoc.co.uk-20100705000318-awwqm8ocpp5m47yz
Merged to trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  Copyright Thijs van den Berg, 2008.
 
2
//  Copyright John Maddock 2008.
 
3
//  Copyright Paul A. Bristow 2008.
 
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
 
 
25
namespace boost{ namespace math{
 
26
 
 
27
template <class RealType = double, class Policy = policies::policy<> >
 
28
class laplace_distribution
 
29
{
 
30
public:
 
31
   // ----------------------------------
 
32
   // public Types
 
33
   // ----------------------------------
 
34
   typedef RealType value_type;
 
35
   typedef Policy policy_type;
 
36
 
 
37
   // ----------------------------------
 
38
   // Constructor(s)
 
39
   // ----------------------------------
 
40
   laplace_distribution(RealType location = 0, RealType scale = 1)
 
41
      : m_location(location), m_scale(scale)
 
42
   {
 
43
      RealType result;
 
44
      check_parameters("boost::math::laplace_distribution<%1%>::laplace_distribution()", &result);
 
45
   }
 
46
 
 
47
 
 
48
   // ----------------------------------
 
49
   // Public functions
 
50
   // ----------------------------------
 
51
 
 
52
   RealType location() const
 
53
   {
 
54
      return m_location;
 
55
   }
 
56
 
 
57
   RealType scale() const
 
58
   {
 
59
      return m_scale;
 
60
   }
 
61
 
 
62
   bool check_parameters(const char* function, RealType* result) const
 
63
   {
 
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;
 
66
         return true;
 
67
   }
 
68
 
 
69
 
 
70
private:
 
71
   RealType m_location;
 
72
   RealType m_scale;
 
73
 
 
74
}; // class laplace_distribution
 
75
 
 
76
 
 
77
 
 
78
//
 
79
// Convenient type synonym
 
80
//
 
81
typedef laplace_distribution<double> laplace;
 
82
 
 
83
//
 
84
// Non member functions
 
85
//
 
86
template <class RealType, class Policy>
 
87
inline const std::pair<RealType, RealType> range(const laplace_distribution<RealType, Policy>&)
 
88
{
 
89
   using boost::math::tools::max_value;
 
90
   return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>());
 
91
}
 
92
 
 
93
template <class RealType, class Policy>
 
94
inline const std::pair<RealType, RealType> support(const laplace_distribution<RealType, Policy>&)
 
95
{
 
96
   using boost::math::tools::max_value;
 
97
   return std::pair<RealType, RealType>(-max_value<RealType>(),  max_value<RealType>());
 
98
}
 
99
 
 
100
template <class RealType, class Policy>
 
101
inline RealType pdf(const laplace_distribution<RealType, Policy>& dist, const RealType& x)
 
102
{
 
103
   BOOST_MATH_STD_USING // for ADL of std functions
 
104
 
 
105
   // Checking function argument
 
106
   RealType result;
 
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;
 
110
 
 
111
   // Special pdf values
 
112
   if((boost::math::isinf)(x))
 
113
      return 0; // pdf + and - infinity is zero.
 
114
 
 
115
   // General case
 
116
   RealType scale( dist.scale() );
 
117
   RealType location( dist.location() );
 
118
 
 
119
   RealType exponent = x - location;
 
120
   if (exponent>0) exponent = -exponent;
 
121
   exponent /= scale;
 
122
 
 
123
   result = exp(exponent);
 
124
   result /= 2 * scale;
 
125
 
 
126
   return result;
 
127
} // pdf
 
128
 
 
129
template <class RealType, class Policy>
 
130
inline RealType cdf(const laplace_distribution<RealType, Policy>& dist, const RealType& x)
 
131
{
 
132
   BOOST_MATH_STD_USING  // for ADL of std functions
 
133
 
 
134
   // Checking function argument
 
135
   RealType result;
 
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;
 
139
 
 
140
   // Special cdf values
 
141
   if((boost::math::isinf)(x))
 
142
   {
 
143
     if(x < 0) return 0; // -infinity
 
144
     return 1; // + infinity
 
145
   }
 
146
 
 
147
   // General cdf  values
 
148
   RealType scale( dist.scale() );
 
149
   RealType location( dist.location() );
 
150
 
 
151
   if (x < location)
 
152
      result = exp( (x-location)/scale )/2;
 
153
   else
 
154
      result = 1 - exp( (location-x)/scale )/2;
 
155
 
 
156
   return result;
 
157
} // cdf
 
158
 
 
159
 
 
160
template <class RealType, class Policy>
 
161
inline RealType quantile(const laplace_distribution<RealType, Policy>& dist, const RealType& p)
 
162
{
 
163
   BOOST_MATH_STD_USING // for ADL of std functions
 
164
 
 
165
   // Checking function argument
 
166
   RealType result;
 
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;
 
170
 
 
171
   // extreme values
 
172
   if(p == 0) return -std::numeric_limits<RealType>::infinity();
 
173
   if(p == 1) return std::numeric_limits<RealType>::infinity();
 
174
 
 
175
   // Calculate Quantile
 
176
   RealType scale( dist.scale() );
 
177
   RealType location( dist.location() );
 
178
 
 
179
   if (p - 0.5 < 0.0)
 
180
      result = location + scale*log( static_cast<RealType>(p*2) );
 
181
   else
 
182
      result = location - scale*log( static_cast<RealType>(-p*2 + 2) );
 
183
 
 
184
   return result;
 
185
} // quantile
 
186
 
 
187
 
 
188
template <class RealType, class Policy>
 
189
inline RealType cdf(const complemented2_type<laplace_distribution<RealType, Policy>, RealType>& c)
 
190
{
 
191
   BOOST_MATH_STD_USING // for ADL of std functions
 
192
 
 
193
   RealType scale = c.dist.scale();
 
194
   RealType location = c.dist.location();
 
195
   RealType x = c.param;
 
196
 
 
197
   // Checking function argument
 
198
   RealType result;
 
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;
 
201
 
 
202
   // Calculate cdf
 
203
 
 
204
   // Special cdf value
 
205
   if((boost::math::isinf)(x))
 
206
   {
 
207
     if(x < 0) return 1; // cdf complement -infinity is unity.
 
208
     return 0; // cdf complement +infinity is zero
 
209
   }
 
210
 
 
211
   // Cdf interval value
 
212
   if (-x < location)
 
213
      result = exp( (-x-location)/scale )/2;
 
214
   else
 
215
      result = 1 - exp( (location+x)/scale )/2;
 
216
 
 
217
   return result;
 
218
} // cdf complement
 
219
 
 
220
 
 
221
template <class RealType, class Policy>
 
222
inline RealType quantile(const complemented2_type<laplace_distribution<RealType, Policy>, RealType>& c)
 
223
{
 
224
   BOOST_MATH_STD_USING // for ADL of std functions
 
225
 
 
226
   // Calculate quantile
 
227
   RealType scale = c.dist.scale();
 
228
   RealType location = c.dist.location();
 
229
   RealType q = c.param;
 
230
 
 
231
   // Checking function argument
 
232
   RealType result;
 
233
   const char* function = "quantile(const complemented2_type<laplace_distribution<%1%>, %1%>&)";
 
234
   if(false == detail::check_probability(function, q, &result, Policy())) return result;
 
235
 
 
236
 
 
237
   // extreme values
 
238
   if(q == 0) return std::numeric_limits<RealType>::infinity();
 
239
   if(q == 1) return -std::numeric_limits<RealType>::infinity();
 
240
 
 
241
   if (0.5 - q < 0.0)
 
242
      result = location + scale*log( static_cast<RealType>(-q*2 + 2) );
 
243
   else
 
244
      result = location - scale*log( static_cast<RealType>(q*2) );
 
245
 
 
246
 
 
247
   return result;
 
248
} // quantile
 
249
 
 
250
template <class RealType, class Policy>
 
251
inline RealType mean(const laplace_distribution<RealType, Policy>& dist)
 
252
{
 
253
   return dist.location();
 
254
}
 
255
 
 
256
template <class RealType, class Policy>
 
257
inline RealType standard_deviation(const laplace_distribution<RealType, Policy>& dist)
 
258
{
 
259
   return constants::root_two<RealType>() * dist.scale();
 
260
}
 
261
 
 
262
template <class RealType, class Policy>
 
263
inline RealType mode(const laplace_distribution<RealType, Policy>& dist)
 
264
{
 
265
   return dist.location();
 
266
}
 
267
 
 
268
template <class RealType, class Policy>
 
269
inline RealType median(const laplace_distribution<RealType, Policy>& dist)
 
270
{
 
271
   return dist.location();
 
272
}
 
273
 
 
274
template <class RealType, class Policy>
 
275
inline RealType skewness(const laplace_distribution<RealType, Policy>& /*dist*/)
 
276
{
 
277
   return 0;
 
278
}
 
279
 
 
280
template <class RealType, class Policy>
 
281
inline RealType kurtosis(const laplace_distribution<RealType, Policy>& /*dist*/)
 
282
{
 
283
   return 6;
 
284
}
 
285
 
 
286
template <class RealType, class Policy>
 
287
inline RealType kurtosis_excess(const laplace_distribution<RealType, Policy>& /*dist*/)
 
288
{
 
289
   return 3;
 
290
}
 
291
 
 
292
} // namespace math
 
293
} // namespace boost
 
294
 
 
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>
 
299
 
 
300
#endif // BOOST_STATS_LAPLACE_HPP
 
301
 
 
302