1 // Copyright John Maddock 2006, 2007.
2 // Copyright Paul A. Bristow 2007
4 // Use, modification and distribution are subject to the
5 // Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt
7 // or copy at http://www.boost.org/LICENSE_1_0.txt)
9 // test_cauchy.cpp Test Cauchy distribution
12 # pragma warning(disable: 4100) // unreferenced formal parameter.
13 // Seems an entirely spurious warning - formal parameter T IS used - get error if /* T */
14 //# pragma warning(disable: 4535) // calling _set_se_translator() requires /EHa (in Boost.test)
15 // Enable C++ Exceptions Yes With SEH Exceptions (/EHa) prevents warning 4535.
16 # pragma warning(disable: 4127) // conditional expression is constant
19 // #define BOOST_MATH_ASSERT_UNDEFINED_POLICY false
20 // To compile even if Cauchy mean is used.
21 #include <boost/math/tools/test.hpp>
22 #include <boost/math/concepts/real_concept.hpp> // for real_concept
23 #include <boost/math/distributions/cauchy.hpp>
24 using boost::math::cauchy_distribution;
26 #include "test_out_of_range.hpp"
28 #define BOOST_TEST_MAIN
29 #include <boost/test/unit_test.hpp> // Boost.Test
30 #include <boost/test/tools/floating_point_comparison.hpp>
36 template <class RealType>
37 void test_spots(RealType T)
39 // Check some bad parameters to construct the distribution,
40 #ifndef BOOST_NO_EXCEPTIONS
41 BOOST_MATH_CHECK_THROW(boost::math::cauchy_distribution<RealType> nbad1(0, 0), std::domain_error); // zero scale.
42 BOOST_MATH_CHECK_THROW(boost::math::cauchy_distribution<RealType> nbad1(0, -1), std::domain_error); // negative scale (shape).
44 BOOST_MATH_CHECK_THROW(boost::math::cauchy_distribution<RealType>(0, 0), std::domain_error); // zero scale.
45 BOOST_MATH_CHECK_THROW(boost::math::cauchy_distribution<RealType>(0, -1), std::domain_error); // negative scale (shape).
47 cauchy_distribution<RealType> C01;
49 BOOST_CHECK_EQUAL(C01.location(), 0); // Check standard values.
50 BOOST_CHECK_EQUAL(C01.scale(), 1);
52 // Basic sanity checks.
53 // 50eps as a percentage, up to a maximum of double precision
54 // (that's the limit of our test data).
55 RealType tolerance = (std::max)(
56 static_cast<RealType>(boost::math::tools::epsilon<double>()),
57 boost::math::tools::epsilon<RealType>());
58 tolerance *= 50 * 100;
60 cout << "Tolerance for type " << typeid(T).name() << " is " << tolerance << " %" << endl;
62 // These first sets of test values were calculated by punching numbers
63 // into a calculator, and using the formulas on the Mathworld website:
64 // http://mathworld.wolfram.com/CauchyDistribution.html
65 // and values from MathCAD 200 Professional,
70 cauchy_distribution<RealType>(),
71 static_cast<RealType>(0.125)), // x
72 static_cast<RealType>(0.53958342416056554201085167134004L), // probability.
76 cauchy_distribution<RealType>(),
77 static_cast<RealType>(-0.125)), // x
78 static_cast<RealType>(0.46041657583943445798914832865996L), // probability.
82 cauchy_distribution<RealType>(),
83 static_cast<RealType>(0.5)), // x
84 static_cast<RealType>(0.64758361765043327417540107622474L), // probability.
88 cauchy_distribution<RealType>(),
89 static_cast<RealType>(-0.5)), // x
90 static_cast<RealType>(0.35241638234956672582459892377526L), // probability.
94 cauchy_distribution<RealType>(),
95 static_cast<RealType>(1.0)), // x
96 static_cast<RealType>(0.75), // probability.
100 cauchy_distribution<RealType>(),
101 static_cast<RealType>(-1.0)), // x
102 static_cast<RealType>(0.25), // probability.
106 cauchy_distribution<RealType>(),
107 static_cast<RealType>(2.0)), // x
108 static_cast<RealType>(0.85241638234956672582459892377526L), // probability.
112 cauchy_distribution<RealType>(),
113 static_cast<RealType>(-2.0)), // x
114 static_cast<RealType>(0.14758361765043327417540107622474L), // probability.
118 cauchy_distribution<RealType>(),
119 static_cast<RealType>(10.0)), // x
120 static_cast<RealType>(0.9682744825694464304850228813987L), // probability.
124 cauchy_distribution<RealType>(),
125 static_cast<RealType>(-10.0)), // x
126 static_cast<RealType>(0.031725517430553569514977118601302L), // probability.
134 complement(cauchy_distribution<RealType>(),
135 static_cast<RealType>(0.125))), // x
136 static_cast<RealType>(0.46041657583943445798914832865996L), // probability.
140 complement(cauchy_distribution<RealType>(),
141 static_cast<RealType>(-0.125))), // x
142 static_cast<RealType>(0.53958342416056554201085167134004L), // probability.
146 complement(cauchy_distribution<RealType>(),
147 static_cast<RealType>(0.5))), // x
148 static_cast<RealType>(0.35241638234956672582459892377526L), // probability.
152 complement(cauchy_distribution<RealType>(),
153 static_cast<RealType>(-0.5))), // x
154 static_cast<RealType>(0.64758361765043327417540107622474L), // probability.
158 complement(cauchy_distribution<RealType>(),
159 static_cast<RealType>(1.0))), // x
160 static_cast<RealType>(0.25), // probability.
164 complement(cauchy_distribution<RealType>(),
165 static_cast<RealType>(-1.0))), // x
166 static_cast<RealType>(0.75), // probability.
170 complement(cauchy_distribution<RealType>(),
171 static_cast<RealType>(2.0))), // x
172 static_cast<RealType>(0.14758361765043327417540107622474L), // probability.
176 complement(cauchy_distribution<RealType>(),
177 static_cast<RealType>(-2.0))), // x
178 static_cast<RealType>(0.85241638234956672582459892377526L), // probability.
182 complement(cauchy_distribution<RealType>(),
183 static_cast<RealType>(10.0))), // x
184 static_cast<RealType>(0.031725517430553569514977118601302L), // probability.
188 complement(cauchy_distribution<RealType>(),
189 static_cast<RealType>(-10.0))), // x
190 static_cast<RealType>(0.9682744825694464304850228813987L), // probability.
197 ::boost::math::quantile(
198 cauchy_distribution<RealType>(),
199 static_cast<RealType>(0.53958342416056554201085167134004L)),
200 static_cast<RealType>(0.125),
203 ::boost::math::quantile(
204 cauchy_distribution<RealType>(),
205 static_cast<RealType>(0.46041657583943445798914832865996L)),
206 static_cast<RealType>(-0.125),
209 ::boost::math::quantile(
210 cauchy_distribution<RealType>(),
211 static_cast<RealType>(0.64758361765043327417540107622474L)),
212 static_cast<RealType>(0.5),
215 ::boost::math::quantile(
216 cauchy_distribution<RealType>(),
217 static_cast<RealType>(0.35241638234956672582459892377526)),
218 static_cast<RealType>(-0.5),
221 ::boost::math::quantile(
222 cauchy_distribution<RealType>(),
223 static_cast<RealType>(0.75)),
224 static_cast<RealType>(1.0),
227 ::boost::math::quantile(
228 cauchy_distribution<RealType>(),
229 static_cast<RealType>(0.25)),
230 static_cast<RealType>(-1.0),
233 ::boost::math::quantile(
234 cauchy_distribution<RealType>(),
235 static_cast<RealType>(0.85241638234956672582459892377526L)),
236 static_cast<RealType>(2.0),
239 ::boost::math::quantile(
240 cauchy_distribution<RealType>(),
241 static_cast<RealType>(0.14758361765043327417540107622474L)),
242 static_cast<RealType>(-2.0),
245 ::boost::math::quantile(
246 cauchy_distribution<RealType>(),
247 static_cast<RealType>(0.9682744825694464304850228813987L)),
248 static_cast<RealType>(10.0),
251 ::boost::math::quantile(
252 cauchy_distribution<RealType>(),
253 static_cast<RealType>(0.031725517430553569514977118601302L)),
254 static_cast<RealType>(-10.0),
258 // Quantile from complement:
261 ::boost::math::quantile(
262 complement(cauchy_distribution<RealType>(),
263 static_cast<RealType>(0.46041657583943445798914832865996L))),
264 static_cast<RealType>(0.125),
267 ::boost::math::quantile(
268 complement(cauchy_distribution<RealType>(),
269 static_cast<RealType>(0.53958342416056554201085167134004L))),
270 static_cast<RealType>(-0.125),
273 ::boost::math::quantile(
274 complement(cauchy_distribution<RealType>(),
275 static_cast<RealType>(0.35241638234956672582459892377526L))),
276 static_cast<RealType>(0.5),
279 ::boost::math::quantile(
280 complement(cauchy_distribution<RealType>(),
281 static_cast<RealType>(0.64758361765043327417540107622474L))),
282 static_cast<RealType>(-0.5),
285 ::boost::math::quantile(
286 complement(cauchy_distribution<RealType>(),
287 static_cast<RealType>(0.25))),
288 static_cast<RealType>(1.0),
291 ::boost::math::quantile(
292 complement(cauchy_distribution<RealType>(),
293 static_cast<RealType>(0.75))),
294 static_cast<RealType>(-1.0),
297 ::boost::math::quantile(
298 complement(cauchy_distribution<RealType>(),
299 static_cast<RealType>(0.14758361765043327417540107622474L))),
300 static_cast<RealType>(2.0),
303 ::boost::math::quantile(
304 complement(cauchy_distribution<RealType>(),
305 static_cast<RealType>(0.85241638234956672582459892377526L))),
306 static_cast<RealType>(-2.0),
309 ::boost::math::quantile(
310 complement(cauchy_distribution<RealType>(),
311 static_cast<RealType>(0.031725517430553569514977118601302L))),
312 static_cast<RealType>(10.0),
315 ::boost::math::quantile(
316 complement(cauchy_distribution<RealType>(),
317 static_cast<RealType>(0.9682744825694464304850228813987L))),
318 static_cast<RealType>(-10.0),
326 cauchy_distribution<RealType>(),
327 static_cast<RealType>(0.125)), // x
328 static_cast<RealType>(0.31341281101173235351410956479511L), // probability.
332 cauchy_distribution<RealType>(),
333 static_cast<RealType>(-0.125)), // x
334 static_cast<RealType>(0.31341281101173235351410956479511L), // probability.
338 cauchy_distribution<RealType>(),
339 static_cast<RealType>(0.5)), // x
340 static_cast<RealType>(0.25464790894703253723021402139602L), // probability.
344 cauchy_distribution<RealType>(),
345 static_cast<RealType>(-0.5)), // x
346 static_cast<RealType>(0.25464790894703253723021402139602L), // probability.
350 cauchy_distribution<RealType>(),
351 static_cast<RealType>(1.0)), // x
352 static_cast<RealType>(0.15915494309189533576888376337251L), // probability.
356 cauchy_distribution<RealType>(),
357 static_cast<RealType>(-1.0)), // x
358 static_cast<RealType>(0.15915494309189533576888376337251L), // probability.
362 cauchy_distribution<RealType>(),
363 static_cast<RealType>(2.0)), // x
364 static_cast<RealType>(0.063661977236758134307553505349006L), // probability.
368 cauchy_distribution<RealType>(),
369 static_cast<RealType>(-2.0)), // x
370 static_cast<RealType>(0.063661977236758134307553505349006L), // probability.
374 cauchy_distribution<RealType>(),
375 static_cast<RealType>(10.0)), // x
376 static_cast<RealType>(0.0031515830315226799162155200667825L), // probability.
380 cauchy_distribution<RealType>(),
381 static_cast<RealType>(-10.0)), // x
382 static_cast<RealType>(0.0031515830315226799162155200667825L), // probability.
386 cauchy_distribution<RealType>(2, 5),
387 static_cast<RealType>(1)), // x
388 static_cast<RealType>(0.061213439650728975295724524374044L), // probability.
392 cauchy_distribution<RealType>(-2, 0.25),
393 static_cast<RealType>(1)), // x
394 static_cast<RealType>(0.0087809623774838805941453110826215L), // probability.
398 // The following test values were calculated using MathCad,
399 // precision seems to be about 10^-13.
401 tolerance = (std::max)(tolerance, static_cast<RealType>(1e-11));
404 cauchy_distribution<RealType>(1, 1),
405 static_cast<RealType>(0.125)), // x
406 static_cast<RealType>(0.271189304634946L), // probability.
410 complement(cauchy_distribution<RealType>(1, 1),
411 static_cast<RealType>(0.125))), // x
412 static_cast<RealType>(1 - 0.271189304634946L), // probability.
415 ::boost::math::quantile(
416 cauchy_distribution<RealType>(1, 1),
417 static_cast<RealType>(0.271189304634946L)), // x
418 static_cast<RealType>(0.125), // probability.
421 ::boost::math::quantile(
422 complement(cauchy_distribution<RealType>(1, 1),
423 static_cast<RealType>(1 - 0.271189304634946L))), // x
424 static_cast<RealType>(0.125), // probability.
428 cauchy_distribution<RealType>(0, 1),
429 static_cast<RealType>(0.125)), // x
430 static_cast<RealType>(0.539583424160566L), // probability.
434 cauchy_distribution<RealType>(0, 1),
435 static_cast<RealType>(0.5)), // x
436 static_cast<RealType>(0.647583617650433L), // probability.
440 cauchy_distribution<RealType>(0, 1),
441 static_cast<RealType>(1)), // x
442 static_cast<RealType>(0.750000000000000), // probability.
446 cauchy_distribution<RealType>(0, 1),
447 static_cast<RealType>(2)), // x
448 static_cast<RealType>(0.852416382349567), // probability.
452 cauchy_distribution<RealType>(0, 1),
453 static_cast<RealType>(10)), // x
454 static_cast<RealType>(0.968274482569447), // probability.
458 cauchy_distribution<RealType>(0, 1),
459 static_cast<RealType>(100)), // x
460 static_cast<RealType>(0.996817007235092), // probability.
465 cauchy_distribution<RealType>(0, 1),
466 static_cast<RealType>(-0.125)), // x
467 static_cast<RealType>(0.460416575839434), // probability.
471 cauchy_distribution<RealType>(0, 1),
472 static_cast<RealType>(-0.5)), // x
473 static_cast<RealType>(0.352416382349567), // probability.
477 cauchy_distribution<RealType>(0, 1),
478 static_cast<RealType>(-1)), // x
479 static_cast<RealType>(0.2500000000000000), // probability.
483 cauchy_distribution<RealType>(0, 1),
484 static_cast<RealType>(-2)), // x
485 static_cast<RealType>(0.147583617650433), // probability.
489 cauchy_distribution<RealType>(0, 1),
490 static_cast<RealType>(-10)), // x
491 static_cast<RealType>(0.031725517430554), // probability.
495 cauchy_distribution<RealType>(0, 1),
496 static_cast<RealType>(-100)), // x
497 static_cast<RealType>(3.18299276490824E-3), // probability.
502 cauchy_distribution<RealType>(1, 5),
503 static_cast<RealType>(1.25)), // x
504 static_cast<RealType>(0.515902251256176), // probability.
508 cauchy_distribution<RealType>(2, 2),
509 static_cast<RealType>(1.25)), // x
510 static_cast<RealType>(0.385799748780092), // probability.
514 cauchy_distribution<RealType>(4, 0.125),
515 static_cast<RealType>(3)), // x
516 static_cast<RealType>(0.039583424160566), // probability.
520 cauchy_distribution<RealType>(-2, static_cast<RealType>(0.0001)),
521 static_cast<RealType>(-3)), // x
522 static_cast<RealType>(3.1830988512275777e-5), // probability.
526 cauchy_distribution<RealType>(4, 50),
527 static_cast<RealType>(-3)), // x
528 static_cast<RealType>(0.455724386698215), // probability.
532 cauchy_distribution<RealType>(-4, 50),
533 static_cast<RealType>(-3)), // x
534 static_cast<RealType>(0.506365349100973), // probability.
539 complement(cauchy_distribution<RealType>(1, 5),
540 static_cast<RealType>(1.25))), // x
541 static_cast<RealType>(1-0.515902251256176), // probability.
545 complement(cauchy_distribution<RealType>(2, 2),
546 static_cast<RealType>(1.25))), // x
547 static_cast<RealType>(1-0.385799748780092), // probability.
551 complement(cauchy_distribution<RealType>(4, 0.125),
552 static_cast<RealType>(3))), // x
553 static_cast<RealType>(1-0.039583424160566), // probability.
557 cauchy_distribution<RealType>(-2, static_cast<RealType>(0.001)),
558 static_cast<RealType>(-3)), // x
559 static_cast<RealType>(0.000318309780080539), // probability.
563 complement(cauchy_distribution<RealType>(4, 50),
564 static_cast<RealType>(-3))), // x
565 static_cast<RealType>(1-0.455724386698215), // probability.
569 complement(cauchy_distribution<RealType>(-4, 50),
570 static_cast<RealType>(-3))), // x
571 static_cast<RealType>(1-0.506365349100973), // probability.
575 ::boost::math::quantile(
576 cauchy_distribution<RealType>(1, 5),
577 static_cast<RealType>(0.515902251256176)), // x
578 static_cast<RealType>(1.25), // probability.
581 ::boost::math::quantile(
582 cauchy_distribution<RealType>(2, 2),
583 static_cast<RealType>(0.385799748780092)), // x
584 static_cast<RealType>(1.25), // probability.
587 ::boost::math::quantile(
588 cauchy_distribution<RealType>(4, 0.125),
589 static_cast<RealType>(0.039583424160566)), // x
590 static_cast<RealType>(3), // probability.
594 ::boost::math::quantile(
595 cauchy_distribution<RealType>(-2, 0.0001),
596 static_cast<RealType>(-3)), // x
597 static_cast<RealType>(0.000015915494296), // probability.
601 ::boost::math::quantile(
602 cauchy_distribution<RealType>(4, 50),
603 static_cast<RealType>(0.455724386698215)), // x
604 static_cast<RealType>(-3), // probability.
607 ::boost::math::quantile(
608 cauchy_distribution<RealType>(-4, 50),
609 static_cast<RealType>(0.506365349100973)), // x
610 static_cast<RealType>(-3), // probability.
614 ::boost::math::quantile(
615 complement(cauchy_distribution<RealType>(1, 5),
616 static_cast<RealType>(1-0.515902251256176))), // x
617 static_cast<RealType>(1.25), // probability.
620 ::boost::math::quantile(
621 complement(cauchy_distribution<RealType>(2, 2),
622 static_cast<RealType>(1-0.385799748780092))), // x
623 static_cast<RealType>(1.25), // probability.
626 ::boost::math::quantile(
627 complement(cauchy_distribution<RealType>(4, 0.125),
628 static_cast<RealType>(1-0.039583424160566))), // x
629 static_cast<RealType>(3), // probability.
633 ::boost::math::quantile(
634 cauchy_distribution<RealType>(-2, 0.0001),
635 static_cast<RealType>(-3)), // x
636 static_cast<RealType>(0.000015915494296), // probability.
640 ::boost::math::quantile(
641 complement(cauchy_distribution<RealType>(4, 50),
642 static_cast<RealType>(1-0.455724386698215))), // x
643 static_cast<RealType>(-3), // probability.
646 ::boost::math::quantile(
647 complement(cauchy_distribution<RealType>(-4, 50),
648 static_cast<RealType>(1-0.506365349100973))), // x
649 static_cast<RealType>(-3), // probability.
652 cauchy_distribution<RealType> dist; // default (0, 1)
655 static_cast<RealType>(0));
658 static_cast<RealType>(0));
660 // Things that now don't compile (BOOST-STATIC_ASSERT_FAILURE) by default.
661 // #define BOOST_MATH_ASSERT_UNDEFINED_POLICY false
662 // To compile even if Cauchy mean is used.
663 // See policy reference, mathematically undefined function policies
665 //BOOST_MATH_CHECK_THROW(
667 // std::domain_error);
668 //BOOST_MATH_CHECK_THROW(
670 // std::domain_error);
671 //BOOST_MATH_CHECK_THROW(
672 // standard_deviation(dist),
673 // std::domain_error);
674 //BOOST_MATH_CHECK_THROW(
676 // std::domain_error);
677 //BOOST_MATH_CHECK_THROW(
678 // kurtosis_excess(dist),
679 // std::domain_error);
680 //BOOST_MATH_CHECK_THROW(
682 // std::domain_error);
684 BOOST_MATH_CHECK_THROW(
685 quantile(dist, RealType(0.0)),
686 std::overflow_error);
687 BOOST_MATH_CHECK_THROW(
688 quantile(dist, RealType(1.0)),
689 std::overflow_error);
690 BOOST_MATH_CHECK_THROW(
691 quantile(complement(dist, RealType(0.0))),
692 std::overflow_error);
693 BOOST_MATH_CHECK_THROW(
694 quantile(complement(dist, RealType(1.0))),
695 std::overflow_error);
697 check_out_of_range<boost::math::cauchy_distribution<RealType> >(0, 1); // (All) valid constructor parameter values.
701 } // template <class RealType>void test_spots(RealType)
703 BOOST_AUTO_TEST_CASE( test_main )
705 BOOST_MATH_CONTROL_FP;
706 // Check that can generate cauchy distribution using the two convenience methods:
707 boost::math::cauchy mycd1(1.); // Using typedef
708 cauchy_distribution<> mycd2(1.); // Using default RealType double.
709 cauchy_distribution<> C01; // Using default RealType double for Standard Cauchy.
710 BOOST_CHECK_EQUAL(C01.location(), 0); // Check standard values.
711 BOOST_CHECK_EQUAL(C01.scale(), 1);
713 // Basic sanity-check spot values.
714 // (Parameter value, arbitrarily zero, only communicates the floating point type).
715 test_spots(0.0F); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
716 test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 %
717 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
718 test_spots(0.0L); // Test long double.
719 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
720 test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
723 std::cout << "<note>The long double tests have been disabled on this platform "
724 "either because the long double overloads of the usual math functions are "
725 "not available at all, or because they are too inaccurate for these tests "
726 "to pass.</note>" << std::endl;
729 } // BOOST_AUTO_TEST_CASE( test_main )
734 Running 1 test case...
735 Tolerance for type float is 0.000596046 %
736 Tolerance for type double is 1.11022e-012 %
737 Tolerance for type long double is 1.11022e-012 %
738 Tolerance for type class boost::math::concepts::real_concept is 1.11022e-012 %
739 *** No errors detected