Chris@16
|
1 // Copyright John Maddock 2006.
|
Chris@16
|
2 // Copyright Paul A. Bristow 2006.
|
Chris@16
|
3 // Use, modification and distribution are subject to the
|
Chris@16
|
4 // Boost Software License, Version 1.0. (See accompanying file
|
Chris@16
|
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6
|
Chris@16
|
7 // TODO deal with infinity as special better - or remove.
|
Chris@16
|
8 //
|
Chris@16
|
9
|
Chris@16
|
10 #ifndef BOOST_STATS_UNIFORM_HPP
|
Chris@16
|
11 #define BOOST_STATS_UNIFORM_HPP
|
Chris@16
|
12
|
Chris@16
|
13 // http://www.itl.nist.gov/div898/handbook/eda/section3/eda3668.htm
|
Chris@16
|
14 // http://mathworld.wolfram.com/UniformDistribution.html
|
Chris@16
|
15 // http://documents.wolfram.com/calculationcenter/v2/Functions/ListsMatrices/Statistics/UniformDistribution.html
|
Chris@16
|
16 // http://en.wikipedia.org/wiki/Uniform_distribution_%28continuous%29
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/math/distributions/fwd.hpp>
|
Chris@16
|
19 #include <boost/math/distributions/detail/common_error_handling.hpp>
|
Chris@16
|
20 #include <boost/math/distributions/complement.hpp>
|
Chris@16
|
21
|
Chris@16
|
22 #include <utility>
|
Chris@16
|
23
|
Chris@16
|
24 namespace boost{ namespace math
|
Chris@16
|
25 {
|
Chris@16
|
26 namespace detail
|
Chris@16
|
27 {
|
Chris@16
|
28 template <class RealType, class Policy>
|
Chris@16
|
29 inline bool check_uniform_lower(
|
Chris@16
|
30 const char* function,
|
Chris@16
|
31 RealType lower,
|
Chris@16
|
32 RealType* result, const Policy& pol)
|
Chris@16
|
33 {
|
Chris@16
|
34 if((boost::math::isfinite)(lower))
|
Chris@16
|
35 { // any finite value is OK.
|
Chris@16
|
36 return true;
|
Chris@16
|
37 }
|
Chris@16
|
38 else
|
Chris@16
|
39 { // Not finite.
|
Chris@16
|
40 *result = policies::raise_domain_error<RealType>(
|
Chris@16
|
41 function,
|
Chris@16
|
42 "Lower parameter is %1%, but must be finite!", lower, pol);
|
Chris@16
|
43 return false;
|
Chris@16
|
44 }
|
Chris@16
|
45 } // bool check_uniform_lower(
|
Chris@16
|
46
|
Chris@16
|
47 template <class RealType, class Policy>
|
Chris@16
|
48 inline bool check_uniform_upper(
|
Chris@16
|
49 const char* function,
|
Chris@16
|
50 RealType upper,
|
Chris@16
|
51 RealType* result, const Policy& pol)
|
Chris@16
|
52 {
|
Chris@16
|
53 if((boost::math::isfinite)(upper))
|
Chris@16
|
54 { // Any finite value is OK.
|
Chris@16
|
55 return true;
|
Chris@16
|
56 }
|
Chris@16
|
57 else
|
Chris@16
|
58 { // Not finite.
|
Chris@16
|
59 *result = policies::raise_domain_error<RealType>(
|
Chris@16
|
60 function,
|
Chris@16
|
61 "Upper parameter is %1%, but must be finite!", upper, pol);
|
Chris@16
|
62 return false;
|
Chris@16
|
63 }
|
Chris@16
|
64 } // bool check_uniform_upper(
|
Chris@16
|
65
|
Chris@16
|
66 template <class RealType, class Policy>
|
Chris@16
|
67 inline bool check_uniform_x(
|
Chris@16
|
68 const char* function,
|
Chris@16
|
69 RealType const& x,
|
Chris@16
|
70 RealType* result, const Policy& pol)
|
Chris@16
|
71 {
|
Chris@16
|
72 if((boost::math::isfinite)(x))
|
Chris@16
|
73 { // Any finite value is OK
|
Chris@16
|
74 return true;
|
Chris@16
|
75 }
|
Chris@16
|
76 else
|
Chris@16
|
77 { // Not finite..
|
Chris@16
|
78 *result = policies::raise_domain_error<RealType>(
|
Chris@16
|
79 function,
|
Chris@16
|
80 "x parameter is %1%, but must be finite!", x, pol);
|
Chris@16
|
81 return false;
|
Chris@16
|
82 }
|
Chris@16
|
83 } // bool check_uniform_x
|
Chris@16
|
84
|
Chris@16
|
85 template <class RealType, class Policy>
|
Chris@16
|
86 inline bool check_uniform(
|
Chris@16
|
87 const char* function,
|
Chris@16
|
88 RealType lower,
|
Chris@16
|
89 RealType upper,
|
Chris@16
|
90 RealType* result, const Policy& pol)
|
Chris@16
|
91 {
|
Chris@16
|
92 if((check_uniform_lower(function, lower, result, pol) == false)
|
Chris@16
|
93 || (check_uniform_upper(function, upper, result, pol) == false))
|
Chris@16
|
94 {
|
Chris@16
|
95 return false;
|
Chris@16
|
96 }
|
Chris@16
|
97 else if (lower >= upper) // If lower == upper then 1 / (upper-lower) = 1/0 = +infinity!
|
Chris@16
|
98 { // upper and lower have been checked before, so must be lower >= upper.
|
Chris@16
|
99 *result = policies::raise_domain_error<RealType>(
|
Chris@16
|
100 function,
|
Chris@16
|
101 "lower parameter is %1%, but must be less than upper!", lower, pol);
|
Chris@16
|
102 return false;
|
Chris@16
|
103 }
|
Chris@16
|
104 else
|
Chris@16
|
105 { // All OK,
|
Chris@16
|
106 return true;
|
Chris@16
|
107 }
|
Chris@16
|
108 } // bool check_uniform(
|
Chris@16
|
109
|
Chris@16
|
110 } // namespace detail
|
Chris@16
|
111
|
Chris@16
|
112 template <class RealType = double, class Policy = policies::policy<> >
|
Chris@16
|
113 class uniform_distribution
|
Chris@16
|
114 {
|
Chris@16
|
115 public:
|
Chris@16
|
116 typedef RealType value_type;
|
Chris@16
|
117 typedef Policy policy_type;
|
Chris@16
|
118
|
Chris@16
|
119 uniform_distribution(RealType l_lower = 0, RealType l_upper = 1) // Constructor.
|
Chris@16
|
120 : m_lower(l_lower), m_upper(l_upper) // Default is standard uniform distribution.
|
Chris@16
|
121 {
|
Chris@16
|
122 RealType result;
|
Chris@16
|
123 detail::check_uniform("boost::math::uniform_distribution<%1%>::uniform_distribution", l_lower, l_upper, &result, Policy());
|
Chris@16
|
124 }
|
Chris@16
|
125 // Accessor functions.
|
Chris@16
|
126 RealType lower()const
|
Chris@16
|
127 {
|
Chris@16
|
128 return m_lower;
|
Chris@16
|
129 }
|
Chris@16
|
130
|
Chris@16
|
131 RealType upper()const
|
Chris@16
|
132 {
|
Chris@16
|
133 return m_upper;
|
Chris@16
|
134 }
|
Chris@16
|
135 private:
|
Chris@16
|
136 // Data members:
|
Chris@16
|
137 RealType m_lower; // distribution lower aka a.
|
Chris@16
|
138 RealType m_upper; // distribution upper aka b.
|
Chris@16
|
139 }; // class uniform_distribution
|
Chris@16
|
140
|
Chris@16
|
141 typedef uniform_distribution<double> uniform;
|
Chris@16
|
142
|
Chris@16
|
143 template <class RealType, class Policy>
|
Chris@16
|
144 inline const std::pair<RealType, RealType> range(const uniform_distribution<RealType, Policy>& /* dist */)
|
Chris@16
|
145 { // Range of permissible values for random variable x.
|
Chris@16
|
146 using boost::math::tools::max_value;
|
Chris@16
|
147 return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + 'infinity'.
|
Chris@16
|
148 // Note RealType infinity is NOT permitted, only max_value.
|
Chris@16
|
149 }
|
Chris@16
|
150
|
Chris@16
|
151 template <class RealType, class Policy>
|
Chris@16
|
152 inline const std::pair<RealType, RealType> support(const uniform_distribution<RealType, Policy>& dist)
|
Chris@16
|
153 { // Range of supported values for random variable x.
|
Chris@16
|
154 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
|
Chris@16
|
155 using boost::math::tools::max_value;
|
Chris@16
|
156 return std::pair<RealType, RealType>(dist.lower(), dist.upper());
|
Chris@16
|
157 }
|
Chris@16
|
158
|
Chris@16
|
159 template <class RealType, class Policy>
|
Chris@16
|
160 inline RealType pdf(const uniform_distribution<RealType, Policy>& dist, const RealType& x)
|
Chris@16
|
161 {
|
Chris@16
|
162 RealType lower = dist.lower();
|
Chris@16
|
163 RealType upper = dist.upper();
|
Chris@16
|
164 RealType result = 0; // of checks.
|
Chris@16
|
165 if(false == detail::check_uniform("boost::math::pdf(const uniform_distribution<%1%>&, %1%)", lower, upper, &result, Policy()))
|
Chris@16
|
166 {
|
Chris@16
|
167 return result;
|
Chris@16
|
168 }
|
Chris@16
|
169 if(false == detail::check_uniform_x("boost::math::pdf(const uniform_distribution<%1%>&, %1%)", x, &result, Policy()))
|
Chris@16
|
170 {
|
Chris@16
|
171 return result;
|
Chris@16
|
172 }
|
Chris@16
|
173
|
Chris@16
|
174 if((x < lower) || (x > upper) )
|
Chris@16
|
175 {
|
Chris@16
|
176 return 0;
|
Chris@16
|
177 }
|
Chris@16
|
178 else
|
Chris@16
|
179 {
|
Chris@16
|
180 return 1 / (upper - lower);
|
Chris@16
|
181 }
|
Chris@16
|
182 } // RealType pdf(const uniform_distribution<RealType, Policy>& dist, const RealType& x)
|
Chris@16
|
183
|
Chris@16
|
184 template <class RealType, class Policy>
|
Chris@16
|
185 inline RealType cdf(const uniform_distribution<RealType, Policy>& dist, const RealType& x)
|
Chris@16
|
186 {
|
Chris@16
|
187 RealType lower = dist.lower();
|
Chris@16
|
188 RealType upper = dist.upper();
|
Chris@16
|
189 RealType result = 0; // of checks.
|
Chris@16
|
190 if(false == detail::check_uniform("boost::math::cdf(const uniform_distribution<%1%>&, %1%)",lower, upper, &result, Policy()))
|
Chris@16
|
191 {
|
Chris@16
|
192 return result;
|
Chris@16
|
193 }
|
Chris@16
|
194 if(false == detail::check_uniform_x("boost::math::cdf(const uniform_distribution<%1%>&, %1%)", x, &result, Policy()))
|
Chris@16
|
195 {
|
Chris@16
|
196 return result;
|
Chris@16
|
197 }
|
Chris@16
|
198 if (x < lower)
|
Chris@16
|
199 {
|
Chris@16
|
200 return 0;
|
Chris@16
|
201 }
|
Chris@16
|
202 if (x > upper)
|
Chris@16
|
203 {
|
Chris@16
|
204 return 1;
|
Chris@16
|
205 }
|
Chris@16
|
206 return (x - lower) / (upper - lower); // lower <= x <= upper
|
Chris@16
|
207 } // RealType cdf(const uniform_distribution<RealType, Policy>& dist, const RealType& x)
|
Chris@16
|
208
|
Chris@16
|
209 template <class RealType, class Policy>
|
Chris@16
|
210 inline RealType quantile(const uniform_distribution<RealType, Policy>& dist, const RealType& p)
|
Chris@16
|
211 {
|
Chris@16
|
212 RealType lower = dist.lower();
|
Chris@16
|
213 RealType upper = dist.upper();
|
Chris@16
|
214 RealType result = 0; // of checks
|
Chris@16
|
215 if(false == detail::check_uniform("boost::math::quantile(const uniform_distribution<%1%>&, %1%)",lower, upper, &result, Policy()))
|
Chris@16
|
216 {
|
Chris@16
|
217 return result;
|
Chris@16
|
218 }
|
Chris@16
|
219 if(false == detail::check_probability("boost::math::quantile(const uniform_distribution<%1%>&, %1%)", p, &result, Policy()))
|
Chris@16
|
220 {
|
Chris@16
|
221 return result;
|
Chris@16
|
222 }
|
Chris@16
|
223 if(p == 0)
|
Chris@16
|
224 {
|
Chris@16
|
225 return lower;
|
Chris@16
|
226 }
|
Chris@16
|
227 if(p == 1)
|
Chris@16
|
228 {
|
Chris@16
|
229 return upper;
|
Chris@16
|
230 }
|
Chris@16
|
231 return p * (upper - lower) + lower;
|
Chris@16
|
232 } // RealType quantile(const uniform_distribution<RealType, Policy>& dist, const RealType& p)
|
Chris@16
|
233
|
Chris@16
|
234 template <class RealType, class Policy>
|
Chris@16
|
235 inline RealType cdf(const complemented2_type<uniform_distribution<RealType, Policy>, RealType>& c)
|
Chris@16
|
236 {
|
Chris@16
|
237 RealType lower = c.dist.lower();
|
Chris@16
|
238 RealType upper = c.dist.upper();
|
Chris@16
|
239 RealType x = c.param;
|
Chris@16
|
240 RealType result = 0; // of checks.
|
Chris@16
|
241 if(false == detail::check_uniform("boost::math::cdf(const uniform_distribution<%1%>&, %1%)", lower, upper, &result, Policy()))
|
Chris@16
|
242 {
|
Chris@16
|
243 return result;
|
Chris@16
|
244 }
|
Chris@16
|
245 if(false == detail::check_uniform_x("boost::math::cdf(const uniform_distribution<%1%>&, %1%)", x, &result, Policy()))
|
Chris@16
|
246 {
|
Chris@16
|
247 return result;
|
Chris@16
|
248 }
|
Chris@16
|
249 if (x < lower)
|
Chris@16
|
250 {
|
Chris@16
|
251 return 1;
|
Chris@16
|
252 }
|
Chris@16
|
253 if (x > upper)
|
Chris@16
|
254 {
|
Chris@16
|
255 return 0;
|
Chris@16
|
256 }
|
Chris@16
|
257 return (upper - x) / (upper - lower);
|
Chris@16
|
258 } // RealType cdf(const complemented2_type<uniform_distribution<RealType, Policy>, RealType>& c)
|
Chris@16
|
259
|
Chris@16
|
260 template <class RealType, class Policy>
|
Chris@16
|
261 inline RealType quantile(const complemented2_type<uniform_distribution<RealType, Policy>, RealType>& c)
|
Chris@16
|
262 {
|
Chris@16
|
263 RealType lower = c.dist.lower();
|
Chris@16
|
264 RealType upper = c.dist.upper();
|
Chris@16
|
265 RealType q = c.param;
|
Chris@16
|
266 RealType result = 0; // of checks.
|
Chris@16
|
267 if(false == detail::check_uniform("boost::math::quantile(const uniform_distribution<%1%>&, %1%)", lower, upper, &result, Policy()))
|
Chris@16
|
268 {
|
Chris@16
|
269 return result;
|
Chris@16
|
270 }
|
Chris@16
|
271 if(false == detail::check_probability("boost::math::quantile(const uniform_distribution<%1%>&, %1%)", q, &result, Policy()))
|
Chris@16
|
272 if(q == 0)
|
Chris@16
|
273 {
|
Chris@16
|
274 return lower;
|
Chris@16
|
275 }
|
Chris@16
|
276 if(q == 1)
|
Chris@16
|
277 {
|
Chris@16
|
278 return upper;
|
Chris@16
|
279 }
|
Chris@16
|
280 return -q * (upper - lower) + upper;
|
Chris@16
|
281 } // RealType quantile(const complemented2_type<uniform_distribution<RealType, Policy>, RealType>& c)
|
Chris@16
|
282
|
Chris@16
|
283 template <class RealType, class Policy>
|
Chris@16
|
284 inline RealType mean(const uniform_distribution<RealType, Policy>& dist)
|
Chris@16
|
285 {
|
Chris@16
|
286 RealType lower = dist.lower();
|
Chris@16
|
287 RealType upper = dist.upper();
|
Chris@16
|
288 RealType result = 0; // of checks.
|
Chris@16
|
289 if(false == detail::check_uniform("boost::math::mean(const uniform_distribution<%1%>&)", lower, upper, &result, Policy()))
|
Chris@16
|
290 {
|
Chris@16
|
291 return result;
|
Chris@16
|
292 }
|
Chris@16
|
293 return (lower + upper ) / 2;
|
Chris@16
|
294 } // RealType mean(const uniform_distribution<RealType, Policy>& dist)
|
Chris@16
|
295
|
Chris@16
|
296 template <class RealType, class Policy>
|
Chris@16
|
297 inline RealType variance(const uniform_distribution<RealType, Policy>& dist)
|
Chris@16
|
298 {
|
Chris@16
|
299 RealType lower = dist.lower();
|
Chris@16
|
300 RealType upper = dist.upper();
|
Chris@16
|
301 RealType result = 0; // of checks.
|
Chris@16
|
302 if(false == detail::check_uniform("boost::math::variance(const uniform_distribution<%1%>&)", lower, upper, &result, Policy()))
|
Chris@16
|
303 {
|
Chris@16
|
304 return result;
|
Chris@16
|
305 }
|
Chris@16
|
306 return (upper - lower) * ( upper - lower) / 12;
|
Chris@16
|
307 // for standard uniform = 0.833333333333333333333333333333333333333333;
|
Chris@16
|
308 } // RealType variance(const uniform_distribution<RealType, Policy>& dist)
|
Chris@16
|
309
|
Chris@16
|
310 template <class RealType, class Policy>
|
Chris@16
|
311 inline RealType mode(const uniform_distribution<RealType, Policy>& dist)
|
Chris@16
|
312 {
|
Chris@16
|
313 RealType lower = dist.lower();
|
Chris@16
|
314 RealType upper = dist.upper();
|
Chris@16
|
315 RealType result = 0; // of checks.
|
Chris@16
|
316 if(false == detail::check_uniform("boost::math::mode(const uniform_distribution<%1%>&)", lower, upper, &result, Policy()))
|
Chris@16
|
317 {
|
Chris@16
|
318 return result;
|
Chris@16
|
319 }
|
Chris@16
|
320 result = lower; // Any value [lower, upper] but arbitrarily choose lower.
|
Chris@16
|
321 return result;
|
Chris@16
|
322 }
|
Chris@16
|
323
|
Chris@16
|
324 template <class RealType, class Policy>
|
Chris@16
|
325 inline RealType median(const uniform_distribution<RealType, Policy>& dist)
|
Chris@16
|
326 {
|
Chris@16
|
327 RealType lower = dist.lower();
|
Chris@16
|
328 RealType upper = dist.upper();
|
Chris@16
|
329 RealType result = 0; // of checks.
|
Chris@16
|
330 if(false == detail::check_uniform("boost::math::median(const uniform_distribution<%1%>&)", lower, upper, &result, Policy()))
|
Chris@16
|
331 {
|
Chris@16
|
332 return result;
|
Chris@16
|
333 }
|
Chris@16
|
334 return (lower + upper) / 2; //
|
Chris@16
|
335 }
|
Chris@16
|
336 template <class RealType, class Policy>
|
Chris@16
|
337 inline RealType skewness(const uniform_distribution<RealType, Policy>& dist)
|
Chris@16
|
338 {
|
Chris@16
|
339 RealType lower = dist.lower();
|
Chris@16
|
340 RealType upper = dist.upper();
|
Chris@16
|
341 RealType result = 0; // of checks.
|
Chris@16
|
342 if(false == detail::check_uniform("boost::math::skewness(const uniform_distribution<%1%>&)",lower, upper, &result, Policy()))
|
Chris@16
|
343 {
|
Chris@16
|
344 return result;
|
Chris@16
|
345 }
|
Chris@16
|
346 return 0;
|
Chris@16
|
347 } // RealType skewness(const uniform_distribution<RealType, Policy>& dist)
|
Chris@16
|
348
|
Chris@16
|
349 template <class RealType, class Policy>
|
Chris@16
|
350 inline RealType kurtosis_excess(const uniform_distribution<RealType, Policy>& dist)
|
Chris@16
|
351 {
|
Chris@16
|
352 RealType lower = dist.lower();
|
Chris@16
|
353 RealType upper = dist.upper();
|
Chris@16
|
354 RealType result = 0; // of checks.
|
Chris@16
|
355 if(false == detail::check_uniform("boost::math::kurtosis_execess(const uniform_distribution<%1%>&)", lower, upper, &result, Policy()))
|
Chris@16
|
356 {
|
Chris@16
|
357 return result;
|
Chris@16
|
358 }
|
Chris@16
|
359 return static_cast<RealType>(-6)/5; // -6/5 = -1.2;
|
Chris@16
|
360 } // RealType kurtosis_excess(const uniform_distribution<RealType, Policy>& dist)
|
Chris@16
|
361
|
Chris@16
|
362 template <class RealType, class Policy>
|
Chris@16
|
363 inline RealType kurtosis(const uniform_distribution<RealType, Policy>& dist)
|
Chris@16
|
364 {
|
Chris@16
|
365 return kurtosis_excess(dist) + 3;
|
Chris@16
|
366 }
|
Chris@16
|
367
|
Chris@16
|
368 } // namespace math
|
Chris@16
|
369 } // namespace boost
|
Chris@16
|
370
|
Chris@16
|
371 // This include must be at the end, *after* the accessors
|
Chris@16
|
372 // for this distribution have been defined, in order to
|
Chris@16
|
373 // keep compilers that support two-phase lookup happy.
|
Chris@16
|
374 #include <boost/math/distributions/detail/derived_accessors.hpp>
|
Chris@16
|
375
|
Chris@16
|
376 #endif // BOOST_STATS_UNIFORM_HPP
|
Chris@16
|
377
|
Chris@16
|
378
|
Chris@16
|
379
|