3 // Copyright John Maddock 2006.
4 // Copyright Paul A. Bristow 2007, 2009, 2010, 2012.
6 // Use, modification and distribution are subject to the
7 // Boost Software License, Version 1.0.
8 // (See accompanying file LICENSE_1_0.txt
9 // or copy at http://www.boost.org/LICENSE_1_0.txt)
11 // Basic sanity tests for the beta Distribution.
13 // http://members.aol.com/iandjmsmith/BETAEX.HTM beta distribution calculator
14 // Appreas to be a 64-bit calculator showing 17 decimal digit (last is noisy).
15 // Similar to mathCAD?
17 // http://www.nuhertz.com/statmat/distributions.html#Beta
18 // Pretty graphs and explanations for most distributions.
20 // http://functions.wolfram.com/webMathematica/FunctionEvaluation.jsp
21 // provided 40 decimal digits accuracy incomplete beta aka beta regularized == cdf
23 // http://www.ausvet.com.au/pprev/content.php?page=PPscript
24 // mode 0.75 5/95% 0.9 alpha 7.39 beta 3.13
25 // http://www.epi.ucdavis.edu/diagnostictests/betabuster.html
26 // Beta Buster also calculates alpha and beta from mode & percentile estimates.
27 // This is NOT (yet) implemented.
30 # pragma warning(disable: 4127) // conditional expression is constant.
31 # pragma warning (disable : 4996) // POSIX name for this item is deprecated.
32 # pragma warning (disable : 4224) // nonstandard extension used : formal parameter 'arg' was previously defined as a type.
35 #include <boost/math/concepts/real_concept.hpp> // for real_concept
36 using ::boost::math::concepts::real_concept;
38 #include <boost/math/distributions/beta.hpp> // for beta_distribution
39 using boost::math::beta_distribution;
40 using boost::math::beta;
42 #define BOOST_TEST_MAIN
43 #include <boost/test/unit_test.hpp> // for test_main
44 #include <boost/test/floating_point_comparison.hpp> // for BOOST_CHECK_CLOSE_FRACTION
46 #include "test_out_of_range.hpp"
52 using std::numeric_limits;
54 template <class RealType>
56 RealType a, // alpha a
58 RealType x, // Probability
59 RealType P, // CDF of beta(a, b)
60 RealType Q, // Complement of CDF
61 RealType tol) // Test tolerance.
63 boost::math::beta_distribution<RealType> abeta(a, b);
64 BOOST_CHECK_CLOSE_FRACTION(cdf(abeta, x), P, tol);
65 if((P < 0.99) && (Q < 0.99))
66 { // We can only check this if P is not too close to 1,
67 // so that we can guarantee that Q is free of error,
68 // (and similarly for Q)
69 BOOST_CHECK_CLOSE_FRACTION(
70 cdf(complement(abeta, x)), Q, tol);
73 BOOST_CHECK_CLOSE_FRACTION(
74 quantile(abeta, P), x, tol);
78 // Just check quantile is very small:
79 if((std::numeric_limits<RealType>::max_exponent <= std::numeric_limits<double>::max_exponent)
80 && (boost::is_floating_point<RealType>::value))
82 // Limit where this is checked: if exponent range is very large we may
83 // run out of iterations in our root finding algorithm.
84 BOOST_CHECK(quantile(abeta, P) < boost::math::tools::epsilon<RealType>() * 10);
89 BOOST_CHECK_CLOSE_FRACTION(quantile(complement(abeta, Q)), x, tol);
92 { // Just check quantile is very small:
93 if((std::numeric_limits<RealType>::max_exponent <= std::numeric_limits<double>::max_exponent) && (boost::is_floating_point<RealType>::value))
94 { // Limit where this is checked: if exponent range is very large we may
95 // run out of iterations in our root finding algorithm.
96 BOOST_CHECK(quantile(complement(abeta, Q)) < boost::math::tools::epsilon<RealType>() * 10);
99 // Estimate alpha & beta from mean and variance:
101 BOOST_CHECK_CLOSE_FRACTION(
102 beta_distribution<RealType>::find_alpha(mean(abeta), variance(abeta)),
104 BOOST_CHECK_CLOSE_FRACTION(
105 beta_distribution<RealType>::find_beta(mean(abeta), variance(abeta)),
108 // Estimate sample alpha and beta from others:
109 BOOST_CHECK_CLOSE_FRACTION(
110 beta_distribution<RealType>::find_alpha(abeta.beta(), x, P),
112 BOOST_CHECK_CLOSE_FRACTION(
113 beta_distribution<RealType>::find_beta(abeta.alpha(), x, P),
115 } // if((P < 0.99) && (Q < 0.99)
117 } // template <class RealType> void test_spot
119 template <class RealType> // Any floating-point type RealType.
120 void test_spots(RealType)
122 // Basic sanity checks with 'known good' values.
123 // MathCAD test data is to double precision only,
124 // so set tolerance to 100 eps expressed as a fraction, or
125 // 100 eps of type double expressed as a fraction,
126 // whichever is the larger.
128 RealType tolerance = (std::max)
129 (boost::math::tools::epsilon<RealType>(),
130 static_cast<RealType>(std::numeric_limits<double>::epsilon())); // 0 if real_concept.
132 cout << "Boost::math::tools::epsilon = " << boost::math::tools::epsilon<RealType>() <<endl;
133 cout << "std::numeric_limits::epsilon = " << std::numeric_limits<RealType>::epsilon() <<endl;
134 cout << "epsilon = " << tolerance;
136 tolerance *= 100000; // Note: NO * 100 because is fraction, NOT %.
137 cout << ", Tolerance = " << tolerance * 100 << "%." << endl;
139 // RealType teneps = boost::math::tools::epsilon<RealType>() * 10;
141 // Sources of spot test values:
143 // MathCAD defines dbeta(x, s1, s2) pdf, s1 == alpha, s2 = beta, x = x in Wolfram
144 // pbeta(x, s1, s2) cdf and qbeta(x, s1, s2) inverse of cdf
145 // returns pr(X ,= x) when random variable X
146 // has the beta distribution with parameters s1)alpha) and s2(beta).
147 // s1 > 0 and s2 >0 and 0 < x < 1 (but allows x == 0! and x == 1!)
149 // dbeta(0.5,1,1) = 1
151 using boost::math::beta_distribution;
152 using ::boost::math::cdf;
153 using ::boost::math::pdf;
155 // Tests that should throw:
156 BOOST_CHECK_THROW(mode(beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1))), std::domain_error);
157 // mode is undefined, and throws domain_error!
159 // BOOST_CHECK_THROW(median(beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1))), std::domain_error);
160 // median is undefined, and throws domain_error!
161 // But now median IS provided via derived accessor as quantile(half).
164 BOOST_CHECK_THROW( // For various bad arguments.
166 beta_distribution<RealType>(static_cast<RealType>(-1), static_cast<RealType>(1)), // bad alpha < 0.
167 static_cast<RealType>(1)), std::domain_error);
171 beta_distribution<RealType>(static_cast<RealType>(0), static_cast<RealType>(1)), // bad alpha == 0.
172 static_cast<RealType>(1)), std::domain_error);
176 beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(0)), // bad beta == 0.
177 static_cast<RealType>(1)), std::domain_error);
181 beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(-1)), // bad beta < 0.
182 static_cast<RealType>(1)), std::domain_error);
186 beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1)), // bad x < 0.
187 static_cast<RealType>(-1)), std::domain_error);
191 beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1)), // bad x > 1.
192 static_cast<RealType>(999)), std::domain_error);
194 // Some exact pdf values.
196 BOOST_CHECK_EQUAL( // a = b = 1 is uniform distribution.
197 pdf(beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1)),
198 static_cast<RealType>(1)), // x
199 static_cast<RealType>(1));
201 pdf(beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1)),
202 static_cast<RealType>(0)), // x
203 static_cast<RealType>(1));
204 BOOST_CHECK_CLOSE_FRACTION(
205 pdf(beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1)),
206 static_cast<RealType>(0.5)), // x
207 static_cast<RealType>(1),
211 beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1)).alpha(),
212 static_cast<RealType>(1) ); //
215 mean(beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1))),
216 static_cast<RealType>(0.5) ); // Exact one half.
218 BOOST_CHECK_CLOSE_FRACTION(
219 pdf(beta_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(2)),
220 static_cast<RealType>(0.5)), // x
221 static_cast<RealType>(1.5), // Exactly 3/2
224 BOOST_CHECK_CLOSE_FRACTION(
225 pdf(beta_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(2)),
226 static_cast<RealType>(0.5)), // x
227 static_cast<RealType>(1.5), // Exactly 3/2
231 BOOST_CHECK_CLOSE_FRACTION(
232 cdf(beta_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(2)),
233 static_cast<RealType>(0.1)), // x
234 static_cast<RealType>(0.02800000000000000000000000000000000000000L), // Seems exact.
235 // http://functions.wolfram.com/webMathematica/FunctionEvaluation.jsp?name=BetaRegularized&ptype=0&z=0.1&a=2&b=2&digits=40
238 BOOST_CHECK_CLOSE_FRACTION(
239 cdf(beta_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(2)),
240 static_cast<RealType>(0.0001)), // x
241 static_cast<RealType>(2.999800000000000000000000000000000000000e-8L),
242 // http://members.aol.com/iandjmsmith/BETAEX.HTM 2.9998000000004
243 // http://functions.wolfram.com/webMathematica/FunctionEvaluation.jsp?name=BetaRegularized&ptype=0&z=0.0001&a=2&b=2&digits=40
247 BOOST_CHECK_CLOSE_FRACTION(
248 pdf(beta_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(2)),
249 static_cast<RealType>(0.0001)), // x
250 static_cast<RealType>(0.0005999400000000004L), // http://members.aol.com/iandjmsmith/BETAEX.HTM
251 // Slightly higher tolerance for real concept:
252 (std::numeric_limits<RealType>::is_specialized ? 1 : 10) * tolerance);
255 BOOST_CHECK_CLOSE_FRACTION(
256 cdf(beta_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(2)),
257 static_cast<RealType>(0.9999)), // x
258 static_cast<RealType>(0.999999970002L), // http://members.aol.com/iandjmsmith/BETAEX.HTM
259 // Wolfram 0.9999999700020000000000000000000000000000
262 BOOST_CHECK_CLOSE_FRACTION(
263 cdf(beta_distribution<RealType>(static_cast<RealType>(0.5), static_cast<RealType>(2)),
264 static_cast<RealType>(0.9)), // x
265 static_cast<RealType>(0.9961174629530394895796514664963063381217L),
269 BOOST_CHECK_CLOSE_FRACTION(
270 cdf(beta_distribution<RealType>(static_cast<RealType>(0.5), static_cast<RealType>(0.5)),
271 static_cast<RealType>(0.1)), // x
272 static_cast<RealType>(0.2048327646991334516491978475505189480977L),
276 BOOST_CHECK_CLOSE_FRACTION(
277 cdf(beta_distribution<RealType>(static_cast<RealType>(0.5), static_cast<RealType>(0.5)),
278 static_cast<RealType>(0.9)), // x
279 static_cast<RealType>(0.7951672353008665483508021524494810519023L),
283 BOOST_CHECK_CLOSE_FRACTION(
284 quantile(beta_distribution<RealType>(static_cast<RealType>(0.5), static_cast<RealType>(0.5)),
285 static_cast<RealType>(0.7951672353008665483508021524494810519023L)), // x
286 static_cast<RealType>(0.9),
290 BOOST_CHECK_CLOSE_FRACTION(
291 cdf(beta_distribution<RealType>(static_cast<RealType>(0.5), static_cast<RealType>(0.5)),
292 static_cast<RealType>(0.6)), // x
293 static_cast<RealType>(0.5640942168489749316118742861695149357858L),
297 BOOST_CHECK_CLOSE_FRACTION(
298 quantile(beta_distribution<RealType>(static_cast<RealType>(0.5), static_cast<RealType>(0.5)),
299 static_cast<RealType>(0.5640942168489749316118742861695149357858L)), // x
300 static_cast<RealType>(0.6),
305 BOOST_CHECK_CLOSE_FRACTION(
306 cdf(beta_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(0.5)),
307 static_cast<RealType>(0.6)), // x
308 static_cast<RealType>(0.1778078083562213736802876784474931812329L),
312 BOOST_CHECK_CLOSE_FRACTION(
313 quantile(beta_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(0.5)),
314 static_cast<RealType>(0.1778078083562213736802876784474931812329L)), // x
315 static_cast<RealType>(0.6),
319 BOOST_CHECK_CLOSE_FRACTION(
320 cdf(beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1)),
321 static_cast<RealType>(0.1)), // x
322 static_cast<RealType>(0.1), // 0.1000000000000000000000000000000000000000
326 BOOST_CHECK_CLOSE_FRACTION(
327 quantile(beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1)),
328 static_cast<RealType>(0.1)), // x
329 static_cast<RealType>(0.1), // 0.1000000000000000000000000000000000000000
333 BOOST_CHECK_CLOSE_FRACTION(
334 cdf(complement(beta_distribution<RealType>(static_cast<RealType>(0.5), static_cast<RealType>(0.5)),
335 static_cast<RealType>(0.1))), // complement of x
336 static_cast<RealType>(0.7951672353008665483508021524494810519023L),
340 BOOST_CHECK_CLOSE_FRACTION(
341 quantile(beta_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(2)),
342 static_cast<RealType>(0.0280000000000000000000000000000000000L)), // x
343 static_cast<RealType>(0.1),
348 BOOST_CHECK_CLOSE_FRACTION(
349 cdf(complement(beta_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(2)),
350 static_cast<RealType>(0.1))), // x
351 static_cast<RealType>(0.9720000000000000000000000000000000000000L), // Exact.
355 BOOST_CHECK_CLOSE_FRACTION(
356 pdf(beta_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(2)),
357 static_cast<RealType>(0.9999)), // x
358 static_cast<RealType>(0.0005999399999999344L), // http://members.aol.com/iandjmsmith/BETAEX.HTM
359 tolerance*10); // Note loss of precision calculating 1-p test value.
362 // RealType a, // alpha a
363 // RealType b, // beta b
364 // RealType x, // Probability
365 // RealType P, // CDF of beta(a, b)
366 // RealType Q, // Complement of CDF
367 // RealType tol) // Test tolerance.
369 // These test quantiles and complements, and parameter estimates as well.
370 // Spot values using, for example:
371 // http://functions.wolfram.com/webMathematica/FunctionEvaluation.jsp?name=BetaRegularized&ptype=0&z=0.1&a=0.5&b=3&digits=40
374 static_cast<RealType>(1), // alpha a
375 static_cast<RealType>(1), // beta b
376 static_cast<RealType>(0.1), // Probability p
377 static_cast<RealType>(0.1), // Probability of result (CDF of beta), P
378 static_cast<RealType>(0.9), // Complement of CDF Q = 1 - P
379 tolerance); // Test tolerance.
381 static_cast<RealType>(2), // alpha a
382 static_cast<RealType>(2), // beta b
383 static_cast<RealType>(0.1), // Probability p
384 static_cast<RealType>(0.0280000000000000000000000000000000000L), // Probability of result (CDF of beta), P
385 static_cast<RealType>(1 - 0.0280000000000000000000000000000000000L), // Complement of CDF Q = 1 - P
386 tolerance); // Test tolerance.
390 static_cast<RealType>(2), // alpha a
391 static_cast<RealType>(2), // beta b
392 static_cast<RealType>(0.5), // Probability p
393 static_cast<RealType>(0.5), // Probability of result (CDF of beta), P
394 static_cast<RealType>(0.5), // Complement of CDF Q = 1 - P
395 tolerance); // Test tolerance.
398 static_cast<RealType>(2), // alpha a
399 static_cast<RealType>(2), // beta b
400 static_cast<RealType>(0.9), // Probability p
401 static_cast<RealType>(0.972000000000000), // Probability of result (CDF of beta), P
402 static_cast<RealType>(1-0.972000000000000), // Complement of CDF Q = 1 - P
403 tolerance); // Test tolerance.
406 static_cast<RealType>(2), // alpha a
407 static_cast<RealType>(2), // beta b
408 static_cast<RealType>(0.01), // Probability p
409 static_cast<RealType>(0.0002980000000000000000000000000000000000000L), // Probability of result (CDF of beta), P
410 static_cast<RealType>(1-0.0002980000000000000000000000000000000000000L), // Complement of CDF Q = 1 - P
411 tolerance); // Test tolerance.
414 static_cast<RealType>(2), // alpha a
415 static_cast<RealType>(2), // beta b
416 static_cast<RealType>(0.001), // Probability p
417 static_cast<RealType>(2.998000000000000000000000000000000000000E-6L), // Probability of result (CDF of beta), P
418 static_cast<RealType>(1-2.998000000000000000000000000000000000000E-6L), // Complement of CDF Q = 1 - P
419 tolerance); // Test tolerance.
422 static_cast<RealType>(2), // alpha a
423 static_cast<RealType>(2), // beta b
424 static_cast<RealType>(0.0001), // Probability p
425 static_cast<RealType>(2.999800000000000000000000000000000000000E-8L), // Probability of result (CDF of beta), P
426 static_cast<RealType>(1-2.999800000000000000000000000000000000000E-8L), // Complement of CDF Q = 1 - P
427 tolerance); // Test tolerance.
430 static_cast<RealType>(2), // alpha a
431 static_cast<RealType>(2), // beta b
432 static_cast<RealType>(0.99), // Probability p
433 static_cast<RealType>(0.9997020000000000000000000000000000000000L), // Probability of result (CDF of beta), P
434 static_cast<RealType>(1-0.9997020000000000000000000000000000000000L), // Complement of CDF Q = 1 - P
435 tolerance); // Test tolerance.
438 static_cast<RealType>(0.5), // alpha a
439 static_cast<RealType>(2), // beta b
440 static_cast<RealType>(0.5), // Probability p
441 static_cast<RealType>(0.8838834764831844055010554526310612991060L), // Probability of result (CDF of beta), P
442 static_cast<RealType>(1-0.8838834764831844055010554526310612991060L), // Complement of CDF Q = 1 - P
443 tolerance); // Test tolerance.
446 static_cast<RealType>(0.5), // alpha a
447 static_cast<RealType>(3.), // beta b
448 static_cast<RealType>(0.7), // Probability p
449 static_cast<RealType>(0.9903963064097119299191611355232156905687L), // Probability of result (CDF of beta), P
450 static_cast<RealType>(1-0.9903963064097119299191611355232156905687L), // Complement of CDF Q = 1 - P
451 tolerance); // Test tolerance.
454 static_cast<RealType>(0.5), // alpha a
455 static_cast<RealType>(3.), // beta b
456 static_cast<RealType>(0.1), // Probability p
457 static_cast<RealType>(0.5545844446520295253493059553548880128511L), // Probability of result (CDF of beta), P
458 static_cast<RealType>(1-0.5545844446520295253493059553548880128511L), // Complement of CDF Q = 1 - P
459 tolerance); // Test tolerance.
463 // Construction with 'bad' parameters.
464 BOOST_CHECK_THROW(beta_distribution<RealType>(1, -1), std::domain_error);
465 BOOST_CHECK_THROW(beta_distribution<RealType>(-1, 1), std::domain_error);
466 BOOST_CHECK_THROW(beta_distribution<RealType>(1, 0), std::domain_error);
467 BOOST_CHECK_THROW(beta_distribution<RealType>(0, 1), std::domain_error);
469 beta_distribution<> dist;
470 BOOST_CHECK_THROW(pdf(dist, -1), std::domain_error);
471 BOOST_CHECK_THROW(cdf(dist, -1), std::domain_error);
472 BOOST_CHECK_THROW(cdf(complement(dist, -1)), std::domain_error);
473 BOOST_CHECK_THROW(quantile(dist, -1), std::domain_error);
474 BOOST_CHECK_THROW(quantile(complement(dist, -1)), std::domain_error);
475 BOOST_CHECK_THROW(quantile(dist, -1), std::domain_error);
476 BOOST_CHECK_THROW(quantile(complement(dist, -1)), std::domain_error);
478 // No longer allow any parameter to be NaN or inf, so all these tests should throw.
479 if (std::numeric_limits<RealType>::has_quiet_NaN)
481 // Attempt to construct from non-finite should throw.
482 RealType nan = std::numeric_limits<RealType>::quiet_NaN();
483 BOOST_CHECK_THROW(beta_distribution<RealType> w(nan), std::domain_error);
484 BOOST_CHECK_THROW(beta_distribution<RealType> w(1, nan), std::domain_error);
486 // Non-finite parameters should throw.
487 beta_distribution<RealType> w(RealType(1));
488 BOOST_CHECK_THROW(pdf(w, +nan), std::domain_error); // x = NaN
489 BOOST_CHECK_THROW(cdf(w, +nan), std::domain_error); // x = NaN
490 BOOST_CHECK_THROW(cdf(complement(w, +nan)), std::domain_error); // x = + nan
491 BOOST_CHECK_THROW(quantile(w, +nan), std::domain_error); // p = + nan
492 BOOST_CHECK_THROW(quantile(complement(w, +nan)), std::domain_error); // p = + nan
495 if (std::numeric_limits<RealType>::has_infinity)
497 // Attempt to construct from non-finite should throw.
498 RealType inf = std::numeric_limits<RealType>::infinity();
500 BOOST_CHECK_THROW(beta_distribution<RealType> w(inf), std::domain_error);
501 BOOST_CHECK_THROW(beta_distribution<RealType> w(1, inf), std::domain_error);
503 // Non-finite parameters should throw.
504 beta_distribution<RealType> w(RealType(1));
505 BOOST_CHECK_THROW(beta_distribution<RealType> w(inf), std::domain_error);
506 BOOST_CHECK_THROW(beta_distribution<RealType> w(1, inf), std::domain_error);
507 BOOST_CHECK_THROW(pdf(w, +inf), std::domain_error); // x = inf
508 BOOST_CHECK_THROW(cdf(w, +inf), std::domain_error); // x = inf
509 BOOST_CHECK_THROW(cdf(complement(w, +inf)), std::domain_error); // x = + inf
510 BOOST_CHECK_THROW(quantile(w, +inf), std::domain_error); // p = + inf
511 BOOST_CHECK_THROW(quantile(complement(w, +inf)), std::domain_error); // p = + inf
514 // Error handling checks:
515 check_out_of_range<boost::math::beta_distribution<RealType> >(1, 1); // (All) valid constructor parameter values.
516 // and range and non-finite.
519 BOOST_CHECK_THROW(pdf(boost::math::beta_distribution<RealType>(0, 1), 0), std::domain_error);
520 BOOST_CHECK_THROW(pdf(boost::math::beta_distribution<RealType>(-1, 1), 0), std::domain_error);
521 BOOST_CHECK_THROW(quantile(boost::math::beta_distribution<RealType>(1, 1), -1), std::domain_error);
522 BOOST_CHECK_THROW(quantile(boost::math::beta_distribution<RealType>(1, 1), 2), std::domain_error);
525 } // template <class RealType>void test_spots(RealType)
527 BOOST_AUTO_TEST_CASE( test_main )
529 BOOST_MATH_CONTROL_FP;
530 // Check that can generate beta distribution using one convenience methods:
531 beta_distribution<> mybeta11(1., 1.); // Using default RealType double.
533 // boost::math::beta mybeta1(1., 1.); // Using typedef fails.
534 // error C2039: 'beta' : is not a member of 'boost::math'
536 // Basic sanity-check spot values.
538 // Some simple checks using double only.
539 BOOST_CHECK_EQUAL(mybeta11.alpha(), 1); //
540 BOOST_CHECK_EQUAL(mybeta11.beta(), 1);
541 BOOST_CHECK_EQUAL(mean(mybeta11), 0.5); // 1 / (1 + 1) = 1/2 exactly
542 BOOST_CHECK_THROW(mode(mybeta11), std::domain_error);
543 beta_distribution<> mybeta22(2., 2.); // pdf is dome shape.
544 BOOST_CHECK_EQUAL(mode(mybeta22), 0.5); // 2-1 / (2+2-2) = 1/2 exactly.
545 beta_distribution<> mybetaH2(0.5, 2.); //
546 beta_distribution<> mybetaH3(0.5, 3.); //
548 // Check a few values using double.
549 BOOST_CHECK_EQUAL(pdf(mybeta11, 1), 1); // is uniform unity over 0 to 1,
550 BOOST_CHECK_EQUAL(pdf(mybeta11, 0), 1); // including zero and unity.
551 // Although these next three have an exact result, internally they're
552 // *not* treated as special cases, and may be out by a couple of eps:
553 BOOST_CHECK_CLOSE_FRACTION(pdf(mybeta11, 0.5), 1.0, 5*std::numeric_limits<double>::epsilon());
554 BOOST_CHECK_CLOSE_FRACTION(pdf(mybeta11, 0.0001), 1.0, 5*std::numeric_limits<double>::epsilon());
555 BOOST_CHECK_CLOSE_FRACTION(pdf(mybeta11, 0.9999), 1.0, 5*std::numeric_limits<double>::epsilon());
556 BOOST_CHECK_CLOSE_FRACTION(cdf(mybeta11, 0.1), 0.1, 2 * std::numeric_limits<double>::epsilon());
557 BOOST_CHECK_CLOSE_FRACTION(cdf(mybeta11, 0.5), 0.5, 2 * std::numeric_limits<double>::epsilon());
558 BOOST_CHECK_CLOSE_FRACTION(cdf(mybeta11, 0.9), 0.9, 2 * std::numeric_limits<double>::epsilon());
559 BOOST_CHECK_EQUAL(cdf(mybeta11, 1), 1.); // Exact unity expected.
561 double tol = std::numeric_limits<double>::epsilon() * 10;
562 BOOST_CHECK_EQUAL(pdf(mybeta22, 1), 0); // is dome shape.
563 BOOST_CHECK_EQUAL(pdf(mybeta22, 0), 0);
564 BOOST_CHECK_CLOSE_FRACTION(pdf(mybeta22, 0.5), 1.5, tol); // top of dome, expect exactly 3/2.
565 BOOST_CHECK_CLOSE_FRACTION(pdf(mybeta22, 0.0001), 5.9994000000000E-4, tol);
566 BOOST_CHECK_CLOSE_FRACTION(pdf(mybeta22, 0.9999), 5.9994000000000E-4, tol*50);
568 BOOST_CHECK_EQUAL(cdf(mybeta22, 0.), 0); // cdf is a curved line from 0 to 1.
569 BOOST_CHECK_CLOSE_FRACTION(cdf(mybeta22, 0.1), 0.028000000000000, tol);
570 BOOST_CHECK_CLOSE_FRACTION(cdf(mybeta22, 0.5), 0.5, tol);
571 BOOST_CHECK_CLOSE_FRACTION(cdf(mybeta22, 0.9), 0.972000000000000, tol);
572 BOOST_CHECK_CLOSE_FRACTION(cdf(mybeta22, 0.0001), 2.999800000000000000000000000000000000000E-8, tol);
573 BOOST_CHECK_CLOSE_FRACTION(cdf(mybeta22, 0.001), 2.998000000000000000000000000000000000000E-6, tol);
574 BOOST_CHECK_CLOSE_FRACTION(cdf(mybeta22, 0.01), 0.0002980000000000000000000000000000000000000, tol);
575 BOOST_CHECK_CLOSE_FRACTION(cdf(mybeta22, 0.1), 0.02800000000000000000000000000000000000000, tol); // exact
576 BOOST_CHECK_CLOSE_FRACTION(cdf(mybeta22, 0.99), 0.9997020000000000000000000000000000000000, tol);
578 BOOST_CHECK_EQUAL(cdf(mybeta22, 1), 1.); // Exact unity expected.
582 BOOST_CHECK_CLOSE_FRACTION(cdf(complement(mybeta22, 0.9)), 0.028000000000000, tol);
585 BOOST_CHECK_CLOSE_FRACTION(quantile(mybeta22, 0.028), 0.1, tol);
586 BOOST_CHECK_CLOSE_FRACTION(quantile(complement(mybeta22, 1 - 0.028)), 0.1, tol);
587 BOOST_CHECK_EQUAL(kurtosis(mybeta11), 3+ kurtosis_excess(mybeta11)); // Check kurtosis_excess = kurtosis - 3;
588 BOOST_CHECK_CLOSE_FRACTION(variance(mybeta22), 0.05, tol);
589 BOOST_CHECK_CLOSE_FRACTION(mean(mybeta22), 0.5, tol);
590 BOOST_CHECK_CLOSE_FRACTION(mode(mybeta22), 0.5, tol);
591 BOOST_CHECK_CLOSE_FRACTION(median(mybeta22), 0.5, sqrt(tol)); // Theoretical maximum accuracy using Brent is sqrt(epsilon).
593 BOOST_CHECK_CLOSE_FRACTION(skewness(mybeta22), 0.0, tol);
594 BOOST_CHECK_CLOSE_FRACTION(kurtosis_excess(mybeta22), -144.0 / 168, tol);
595 BOOST_CHECK_CLOSE_FRACTION(skewness(beta_distribution<>(3, 5)), 0.30983866769659335081434123198259, tol);
597 BOOST_CHECK_CLOSE_FRACTION(beta_distribution<double>::find_alpha(mean(mybeta22), variance(mybeta22)), mybeta22.alpha(), tol); // mean, variance, probability.
598 BOOST_CHECK_CLOSE_FRACTION(beta_distribution<double>::find_beta(mean(mybeta22), variance(mybeta22)), mybeta22.beta(), tol);// mean, variance, probability.
600 BOOST_CHECK_CLOSE_FRACTION(mybeta22.find_alpha(mybeta22.beta(), 0.8, cdf(mybeta22, 0.8)), mybeta22.alpha(), tol);
601 BOOST_CHECK_CLOSE_FRACTION(mybeta22.find_beta(mybeta22.alpha(), 0.8, cdf(mybeta22, 0.8)), mybeta22.beta(), tol);
604 beta_distribution<real_concept> rcbeta22(2, 2); // Using RealType real_concept.
605 cout << "numeric_limits<real_concept>::is_specialized " << numeric_limits<real_concept>::is_specialized << endl;
606 cout << "numeric_limits<real_concept>::digits " << numeric_limits<real_concept>::digits << endl;
607 cout << "numeric_limits<real_concept>::digits10 " << numeric_limits<real_concept>::digits10 << endl;
608 cout << "numeric_limits<real_concept>::epsilon " << numeric_limits<real_concept>::epsilon() << endl;
610 // (Parameter value, arbitrarily zero, only communicates the floating point type).
611 test_spots(0.0F); // Test float.
612 test_spots(0.0); // Test double.
613 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
614 test_spots(0.0L); // Test long double.
615 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
616 test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
619 } // BOOST_AUTO_TEST_CASE( test_main )
625 -Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_beta_dist.exe"
626 Running 1 test case...
627 numeric_limits<real_concept>::is_specialized 0
628 numeric_limits<real_concept>::digits 0
629 numeric_limits<real_concept>::digits10 0
630 numeric_limits<real_concept>::epsilon 0
631 Boost::math::tools::epsilon = 1.19209e-007
632 std::numeric_limits::epsilon = 1.19209e-007
633 epsilon = 1.19209e-007, Tolerance = 0.0119209%.
634 Boost::math::tools::epsilon = 2.22045e-016
635 std::numeric_limits::epsilon = 2.22045e-016
636 epsilon = 2.22045e-016, Tolerance = 2.22045e-011%.
637 Boost::math::tools::epsilon = 2.22045e-016
638 std::numeric_limits::epsilon = 2.22045e-016
639 epsilon = 2.22045e-016, Tolerance = 2.22045e-011%.
640 Boost::math::tools::epsilon = 2.22045e-016
641 std::numeric_limits::epsilon = 0
642 epsilon = 2.22045e-016, Tolerance = 2.22045e-011%.
643 *** No errors detected