test_nc_t.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. // test_nc_t.cpp
  2. // Copyright John Maddock 2008, 2012.
  3. // Copyright Paul A. Bristow 2012.
  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)
  8. #include <pch.hpp> // Need to include lib/math/test in path.
  9. #ifdef _MSC_VER
  10. #pragma warning (disable:4127 4512)
  11. #endif
  12. #if !defined(TEST_FLOAT) && !defined(TEST_DOUBLE) && !defined(TEST_LDOUBLE) && !defined(TEST_REAL_CONCEPT)
  13. # define TEST_FLOAT
  14. # define TEST_DOUBLE
  15. # define TEST_LDOUBLE
  16. # define TEST_REAL_CONCEPT
  17. #endif
  18. #include <boost/math/tools/test.hpp>
  19. #include <boost/math/concepts/real_concept.hpp> // for real_concept
  20. #include <boost/math/distributions/non_central_t.hpp> // for chi_squared_distribution.
  21. #include <boost/math/distributions/normal.hpp> // for normal distribution (for comparison).
  22. #define BOOST_TEST_MAIN
  23. #include <boost/test/unit_test.hpp> // for test_main
  24. #include <boost/test/results_collector.hpp>
  25. #include <boost/test/unit_test.hpp>
  26. #include <boost/test/tools/floating_point_comparison.hpp> // for BOOST_CHECK_CLOSE
  27. #include "functor.hpp"
  28. #include "handle_test_result.hpp"
  29. #include "table_type.hpp"
  30. #include "test_nc_t.hpp"
  31. #include <iostream>
  32. #include <iomanip>
  33. using std::cout;
  34. using std::endl;
  35. #include <limits>
  36. using std::numeric_limits;
  37. void expected_results()
  38. {
  39. //
  40. // Define the max and mean errors expected for
  41. // various compilers and platforms.
  42. //
  43. const char* largest_type;
  44. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  45. if(boost::math::policies::digits<double, boost::math::policies::policy<> >() == boost::math::policies::digits<long double, boost::math::policies::policy<> >())
  46. {
  47. largest_type = "(long\\s+)?double|real_concept";
  48. }
  49. else
  50. {
  51. largest_type = "long double|real_concept";
  52. }
  53. #else
  54. largest_type = "(long\\s+)?double|real_concept";
  55. #endif
  56. //
  57. // Catch all cases come last:
  58. //
  59. if(std::numeric_limits<long double>::digits > 54)
  60. {
  61. add_expected_result(
  62. "[^|]*", // compiler
  63. "[^|]*", // stdlib
  64. "[^|]*", // platform
  65. largest_type, // test type(s)
  66. "[^|]*large[^|]*", // test data group
  67. "[^|]*", 2000000, 200000); // test function
  68. add_expected_result(
  69. "[^|]*", // compiler
  70. "[^|]*", // stdlib
  71. "[^|]*", // platform
  72. "double", // test type(s)
  73. "[^|]*large[^|]*", // test data group
  74. "[^|]*", 500, 100); // test function
  75. }
  76. add_expected_result(
  77. "[^|]*", // compiler
  78. "[^|]*", // stdlib
  79. "[^|]*", // platform
  80. "real_concept", // test type(s)
  81. "[^|]*", // test data group
  82. "[^|]*", 300000, 100000); // test function
  83. add_expected_result(
  84. "[^|]*", // compiler
  85. "[^|]*", // stdlib
  86. "[^|]*", // platform
  87. largest_type, // test type(s)
  88. "[^|]*large[^|]*", // test data group
  89. "[^|]*", 1500, 300); // test function
  90. add_expected_result(
  91. "[^|]*", // compiler
  92. "[^|]*", // stdlib
  93. "[^|]*", // platform
  94. largest_type, // test type(s)
  95. "[^|]*small[^|]*", // test data group
  96. "[^|]*", 400, 100); // test function
  97. add_expected_result(
  98. "[^|]*", // compiler
  99. "[^|]*", // stdlib
  100. ".*Solaris.*", // platform
  101. largest_type, // test type(s)
  102. "[^|]*", // test data group
  103. "[^|]*", 400, 100); // test function
  104. add_expected_result(
  105. "[^|]*", // compiler
  106. "[^|]*", // stdlib
  107. "[^|]*", // platform
  108. largest_type, // test type(s)
  109. "[^|]*", // test data group
  110. "[^|]*", 250, 50); // test function
  111. //
  112. // Finish off by printing out the compiler/stdlib/platform names,
  113. // we do this to make it easier to mark up expected error rates.
  114. //
  115. std::cout << "Tests run with " << BOOST_COMPILER << ", "
  116. << BOOST_STDLIB << ", " << BOOST_PLATFORM << std::endl;
  117. }
  118. BOOST_AUTO_TEST_CASE( test_main )
  119. {
  120. BOOST_MATH_CONTROL_FP;
  121. // Basic sanity-check spot values.
  122. expected_results();
  123. // (Parameter value, arbitrarily zero, only communicates the floating point type).
  124. #ifdef TEST_FLOAT
  125. test_spots(0.0F); // Test float.
  126. #endif
  127. #ifdef TEST_DOUBLE
  128. test_spots(0.0); // Test double.
  129. #endif
  130. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  131. #ifdef TEST_LDOUBLE
  132. test_spots(0.0L); // Test long double.
  133. #endif
  134. #ifndef BOOST_MATH_NO_REAL_CONCEPT_TESTS
  135. #ifdef TEST_REAL_CONCEPT
  136. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  137. #endif
  138. #endif
  139. #endif
  140. #ifdef TEST_FLOAT
  141. test_accuracy(0.0F, "float"); // Test float.
  142. test_big_df(0.F); // float
  143. #endif
  144. #ifdef TEST_DOUBLE
  145. test_accuracy(0.0, "double"); // Test double.
  146. test_big_df(0.); // double
  147. test_ignore_policy(0.0);
  148. #endif
  149. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  150. #ifdef TEST_LDOUBLE
  151. test_accuracy(0.0L, "long double"); // Test long double.
  152. #endif
  153. #ifndef BOOST_MATH_NO_REAL_CONCEPT_TESTS
  154. #ifdef TEST_REAL_CONCEPT
  155. test_accuracy(boost::math::concepts::real_concept(0.), "real_concept"); // Test real concept.
  156. #endif
  157. #endif
  158. #endif
  159. /* */
  160. } // BOOST_AUTO_TEST_CASE( test_main )
  161. /*
  162. Output:
  163. Description: Autorun "J:\Cpp\MathToolkit\test\Math_test\Debug\test_nc_t.exe"
  164. Running 1 test case...
  165. Tests run with Microsoft Visual C++ version 10.0, Dinkumware standard library version 520, Win32
  166. Tolerance = 0.000596046%.
  167. Tolerance = 5e-010%.
  168. Tolerance = 5e-010%.
  169. Tolerance = 1e-008%.
  170. Testing: Non Central T
  171. CDF<float> Max = 0 RMS Mean=0
  172. CCDF<float> Max = 0 RMS Mean=0
  173. Testing: float quantile sanity check, with tests Non Central T
  174. Testing: Non Central T (small non-centrality)
  175. CDF<float> Max = 0 RMS Mean=0
  176. CCDF<float> Max = 0 RMS Mean=0
  177. Testing: float quantile sanity check, with tests Non Central T (small non-centrality)
  178. Testing: Non Central T (large parameters)
  179. CDF<float> Max = 0 RMS Mean=0
  180. CCDF<float> Max = 0 RMS Mean=0
  181. Testing: float quantile sanity check, with tests Non Central T (large parameters)
  182. Testing: Non Central T
  183. CDF<double> Max = 137.7 RMS Mean=31.5
  184. worst case at row: 181
  185. { 188.01481628417969, -282.022216796875, -298.02532958984375, 0.1552789395983287, 0.84472106040167128 }
  186. CCDF<double> Max = 150.4 RMS Mean=32.32
  187. worst case at row: 184
  188. { 191.43339538574219, 765.73358154296875, 820.14422607421875, 0.89943076553533785, 0.10056923446466212 }
  189. Testing: double quantile sanity check, with tests Non Central T
  190. Testing: Non Central T (small non-centrality)
  191. CDF<double> Max = 3.605 RMS Mean=1.031
  192. worst case at row: 42
  193. { 7376104448, 7.3761043495323975e-007, -1.3614851236343384, 0.086680099352107118, 0.91331990064789292 }
  194. CCDF<double> Max = 5.207 RMS Mean=1.432
  195. worst case at row: 38
  196. { 1524088576, 1.5240885886669275e-007, 1.3784774541854858, 0.91597201432644526, 0.084027985673554725 }
  197. Testing: double quantile sanity check, with tests Non Central T (small non-centrality)
  198. Testing: Non Central T (large parameters)
  199. CDF<double> Max = 286.4 RMS Mean=62.79
  200. worst case at row: 24
  201. { 1.3091821180254421e+019, 1309.18212890625, 1308.01171875, 0.12091797523015677, 0.87908202476984321 }
  202. CCDF<double> Max = 226.9 RMS Mean=50.41
  203. worst case at row: 23
  204. { 7.9217674231144776e+018, 792.1767578125, 793.54827880859375, 0.91489369852628, 0.085106301473719961 }
  205. Testing: double quantile sanity check, with tests Non Central T (large parameters)
  206. Testing: Non Central T
  207. CDF<long double> Max = 137.7 RMS Mean=31.5
  208. worst case at row: 181
  209. { 188.01481628417969, -282.022216796875, -298.02532958984375, 0.1552789395983287, 0.84472106040167128 }
  210. CCDF<long double> Max = 150.4 RMS Mean=32.32
  211. worst case at row: 184
  212. { 191.43339538574219, 765.73358154296875, 820.14422607421875, 0.89943076553533785, 0.10056923446466212 }
  213. Testing: long double quantile sanity check, with tests Non Central T
  214. Testing: Non Central T (small non-centrality)
  215. CDF<long double> Max = 3.605 RMS Mean=1.031
  216. worst case at row: 42
  217. { 7376104448, 7.3761043495323975e-007, -1.3614851236343384, 0.086680099352107118, 0.91331990064789292 }
  218. CCDF<long double> Max = 5.207 RMS Mean=1.432
  219. worst case at row: 38
  220. { 1524088576, 1.5240885886669275e-007, 1.3784774541854858, 0.91597201432644526, 0.084027985673554725 }
  221. Testing: long double quantile sanity check, with tests Non Central T (small non-centrality)
  222. Testing: Non Central T (large parameters)
  223. CDF<long double> Max = 286.4 RMS Mean=62.79
  224. worst case at row: 24
  225. { 1.3091821180254421e+019, 1309.18212890625, 1308.01171875, 0.12091797523015677, 0.87908202476984321 }
  226. CCDF<long double> Max = 226.9 RMS Mean=50.41
  227. worst case at row: 23
  228. { 7.9217674231144776e+018, 792.1767578125, 793.54827880859375, 0.91489369852628, 0.085106301473719961 }
  229. Testing: long double quantile sanity check, with tests Non Central T (large parameters)
  230. Testing: Non Central T
  231. CDF<real_concept> Max = 2.816e+005 RMS Mean=2.029e+004
  232. worst case at row: 185
  233. { 191.50137329101562, -957.5068359375, -1035.4078369140625, 0.072545502958829097, 0.92745449704117089 }
  234. CCDF<real_concept> Max = 1.304e+005 RMS Mean=1.529e+004
  235. worst case at row: 184
  236. { 191.43339538574219, 765.73358154296875, 820.14422607421875, 0.89943076553533785, 0.10056923446466212 }
  237. cdf(n10, 11) = 0.84134471416473389 0.15865525603294373
  238. cdf(n10, 9) = 0.15865525603294373 0.84134471416473389
  239. cdf(maxdf10, 11) = 0.84134477376937866 0.15865525603294373
  240. cdf(infdf10, 11) = 0.84134477376937866 0.15865525603294373
  241. cdf(n10, 11) = 0.84134474606854293 0.15865525393145707
  242. cdf(n10, 9) = 0.15865525393145707 0.84134474606854293
  243. cdf(maxdf10, 11) = 0.84134474606854293 0.15865525393145707
  244. cdf(infdf10, 11) = 0.84134474606854293 0.15865525393145707
  245. *** No errors detected
  246. Description: Autorun "J:\Cpp\MathToolkit\test\Math_test\Debug\test_nc_t.exe"
  247. Running 1 test case...
  248. Tests run with Microsoft Visual C++ version 10.0, Dinkumware standard library version 520, Win32
  249. Tolerance = 0.000596046%.
  250. Tolerance = 5e-010%.
  251. Tolerance = 5e-010%.
  252. Tolerance = 1e-008%.
  253. Testing: Non Central T
  254. CDF<float> Max = 0 RMS Mean=0
  255. CCDF<float> Max = 0 RMS Mean=0
  256. Testing: float quantile sanity check, with tests Non Central T
  257. Testing: Non Central T (small non-centrality)
  258. CDF<float> Max = 0 RMS Mean=0
  259. CCDF<float> Max = 0 RMS Mean=0
  260. Testing: float quantile sanity check, with tests Non Central T (small non-centrality)
  261. Testing: Non Central T (large parameters)
  262. CDF<float> Max = 0 RMS Mean=0
  263. CCDF<float> Max = 0 RMS Mean=0
  264. Testing: float quantile sanity check, with tests Non Central T (large parameters)
  265. Testing: Non Central T
  266. CDF<double> Max = 137.7 RMS Mean=31.5
  267. worst case at row: 181
  268. { 188.01481628417969, -282.022216796875, -298.02532958984375, 0.1552789395983287, 0.84472106040167128 }
  269. CCDF<double> Max = 150.4 RMS Mean=32.32
  270. worst case at row: 184
  271. { 191.43339538574219, 765.73358154296875, 820.14422607421875, 0.89943076553533785, 0.10056923446466212 }
  272. Testing: double quantile sanity check, with tests Non Central T
  273. Testing: Non Central T (small non-centrality)
  274. CDF<double> Max = 3.605 RMS Mean=1.031
  275. worst case at row: 42
  276. { 7376104448, 7.3761043495323975e-007, -1.3614851236343384, 0.086680099352107118, 0.91331990064789292 }
  277. CCDF<double> Max = 5.207 RMS Mean=1.432
  278. worst case at row: 38
  279. { 1524088576, 1.5240885886669275e-007, 1.3784774541854858, 0.91597201432644526, 0.084027985673554725 }
  280. Testing: double quantile sanity check, with tests Non Central T (small non-centrality)
  281. Testing: Non Central T (large parameters)
  282. CDF<double> Max = 286.4 RMS Mean=62.79
  283. worst case at row: 24
  284. { 1.3091821180254421e+019, 1309.18212890625, 1308.01171875, 0.12091797523015677, 0.87908202476984321 }
  285. CCDF<double> Max = 226.9 RMS Mean=50.41
  286. worst case at row: 23
  287. { 7.9217674231144776e+018, 792.1767578125, 793.54827880859375, 0.91489369852628, 0.085106301473719961 }
  288. Testing: double quantile sanity check, with tests Non Central T (large parameters)
  289. Testing: Non Central T
  290. CDF<long double> Max = 137.7 RMS Mean=31.5
  291. worst case at row: 181
  292. { 188.01481628417969, -282.022216796875, -298.02532958984375, 0.1552789395983287, 0.84472106040167128 }
  293. CCDF<long double> Max = 150.4 RMS Mean=32.32
  294. worst case at row: 184
  295. { 191.43339538574219, 765.73358154296875, 820.14422607421875, 0.89943076553533785, 0.10056923446466212 }
  296. Testing: long double quantile sanity check, with tests Non Central T
  297. Testing: Non Central T (small non-centrality)
  298. CDF<long double> Max = 3.605 RMS Mean=1.031
  299. worst case at row: 42
  300. { 7376104448, 7.3761043495323975e-007, -1.3614851236343384, 0.086680099352107118, 0.91331990064789292 }
  301. CCDF<long double> Max = 5.207 RMS Mean=1.432
  302. worst case at row: 38
  303. { 1524088576, 1.5240885886669275e-007, 1.3784774541854858, 0.91597201432644526, 0.084027985673554725 }
  304. Testing: long double quantile sanity check, with tests Non Central T (small non-centrality)
  305. Testing: Non Central T (large parameters)
  306. CDF<long double> Max = 286.4 RMS Mean=62.79
  307. worst case at row: 24
  308. { 1.3091821180254421e+019, 1309.18212890625, 1308.01171875, 0.12091797523015677, 0.87908202476984321 }
  309. CCDF<long double> Max = 226.9 RMS Mean=50.41
  310. worst case at row: 23
  311. { 7.9217674231144776e+018, 792.1767578125, 793.54827880859375, 0.91489369852628, 0.085106301473719961 }
  312. Testing: long double quantile sanity check, with tests Non Central T (large parameters)
  313. Testing: Non Central T
  314. CDF<real_concept> Max = 2.816e+005 RMS Mean=2.029e+004
  315. worst case at row: 185
  316. { 191.50137329101562, -957.5068359375, -1035.4078369140625, 0.072545502958829097, 0.92745449704117089 }
  317. CCDF<real_concept> Max = 1.304e+005 RMS Mean=1.529e+004
  318. worst case at row: 184
  319. { 191.43339538574219, 765.73358154296875, 820.14422607421875, 0.89943076553533785, 0.10056923446466212 }
  320. *** No errors detected
  321. */
  322. /*
  323. Temporary stuff from student's t version.
  324. // Calculate 1 / eps, the point where student's t should change to normal distribution.
  325. RealType limit = 1 / boost::math::tools::epsilon<RealType>();
  326. using namespace boost::math::policies;
  327. typedef policy<digits10<17> > accurate_policy; // 17 = max_digits10 where available.
  328. limit = 1 / policies::get_epsilon<RealType, accurate_policy>();
  329. BOOST_CHECK_CLOSE_FRACTION(limit, static_cast<RealType>(1) / std::numeric_limits<RealType>::epsilon(), tolerance);
  330. // Default policy to get full accuracy.
  331. // std::cout << "Switch over to normal if df > " << limit << std::endl;
  332. // float Switch over to normal if df > 8.38861e+006
  333. // double Switch over to normal if df > 4.5036e+015
  334. // Can't test real_concept - doesn't converge.
  335. boost::math::normal_distribution<RealType> n01(0, 1); //
  336. boost::math::normal_distribution<RealType> n10(10, 1); //
  337. non_central_t_distribution<RealType> nct(boost::math::tools::max_value<RealType>(), 0); // Well over the switchover point,
  338. non_central_t_distribution<RealType> nct2(limit /5, 0); // Just below the switchover point,
  339. non_central_t_distribution<RealType> nct3(limit /100, 0); // Well below the switchover point,
  340. non_central_t_distribution<RealType> nct4(limit, 10); // Well below the switchover point, and 10 non-centrality.
  341. // PDF
  342. BOOST_CHECK_CLOSE_FRACTION(pdf(nct, 0), pdf(n01, 0.), tolerance); // normal and non-central t should be nearly equal.
  343. BOOST_CHECK_CLOSE_FRACTION(pdf(nct2, 0), pdf(n01, 0.), tolerance); // should be very close to normal.
  344. BOOST_CHECK_CLOSE_FRACTION(pdf(nct3, 0), pdf(n01, 0.), tolerance * 10); // should be close to normal.
  345. // BOOST_CHECK_CLOSE_FRACTION(pdf(nct4, 10), pdf(n10, 0.), tolerance * 100); // should be fairly close to normal tolerance.
  346. RealType delta = 10; // non-centrality.
  347. RealType nu = static_cast<RealType>(limit); // df
  348. boost::math::normal_distribution<RealType> nl(delta, 1); // Normal distribution that nct tends to for big df.
  349. non_central_t_distribution<RealType> nct5(nu, delta); //
  350. RealType x = delta;
  351. // BOOST_CHECK_CLOSE_FRACTION(pdf(nct5, x), pdf(nl, x), tolerance * 10 ); // nu = 1e15
  352. // BOOST_CHECK_CLOSE_FRACTION(pdf(nct5, x), pdf(nl, x), tolerance * 1000 ); // nu = 1e14
  353. // BOOST_CHECK_CLOSE_FRACTION(pdf(nct5, x), pdf(nl, x), tolerance * 10000 ); // nu = 1e13
  354. // BOOST_CHECK_CLOSE_FRACTION(pdf(nct5, x), pdf(nl, x), tolerance * 100000 ); // nu = 1e12
  355. BOOST_CHECK_CLOSE_FRACTION(pdf(nct5, x), pdf(nl, x), tolerance * 5 ); // nu = 1/eps
  356. // Increasing the non-centrality delta increases the difference too because increases asymmetry.
  357. // For example, with non-centrality = 100, need tolerance * 500
  358. // CDF
  359. BOOST_CHECK_CLOSE_FRACTION(cdf(nct, 0), cdf(n01, 0.), tolerance); // should be exactly equal.
  360. BOOST_CHECK_CLOSE_FRACTION(cdf(nct2, 0), cdf(n01, 0.), tolerance); // should be very close to normal.
  361. BOOST_CHECK_CLOSE_FRACTION(cdf(complement(n10, 11)), 1 - cdf(n10, 11), tolerance); //
  362. // cdf(n10, 10) = 0.841345 0.158655
  363. BOOST_CHECK_CLOSE_FRACTION(cdf(complement(n10, 9)), 1 - cdf(n10, 9), tolerance); //
  364. std::cout.precision(17);
  365. std::cout << "cdf(n10, 11) = " << cdf(n10, 11) << ' ' << cdf(complement(n10, 11)) << endl;
  366. std::cout << "cdf(n10, 9) = " << cdf(n10, 9) << ' ' << cdf(complement(n10, 9)) << endl;
  367. std::cout << std::numeric_limits<double>::max_digits10 << std::endl;
  368. std::cout.precision(17);
  369. using boost::math::tools::max_value;
  370. double eps = std::numeric_limits<double>::epsilon();
  371. // Use policies so that if policy requests lower precision,
  372. // then get the normal distribution approximation earlier.
  373. //limit = static_cast<double>(1) / limit; // 1/eps
  374. double delta = 1e2;
  375. double df =
  376. delta / (4 * eps);
  377. std::cout << df << std::endl; // df = 1.125899906842624e+018
  378. {
  379. boost::math::non_central_t_distribution<double> dist(df, delta);
  380. std::cout <<"mean " << mean(dist) << std::endl; // mean 1000
  381. std::cout <<"variance " << variance(dist) << std::endl; // variance 1
  382. std::cout <<"skewness " << skewness(dist) << std::endl; // skewness 8.8817841970012523e-010
  383. std::cout <<"kurtosis_excess " << kurtosis_excess(dist) << std::endl; // kurtosis_excess 3.0001220703125
  384. //1.125899906842624e+017
  385. //mean 100
  386. //variance 1
  387. //skewness 8.8817841970012523e-012
  388. //kurtosis_excess 3
  389. }
  390. */