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