test_nc_t.hpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. // (C) Copyright John Maddock 2007.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #define BOOST_MATH_OVERFLOW_ERROR_POLICY ignore_error
  6. #include <boost/math/concepts/real_concept.hpp>
  7. #define BOOST_TEST_MAIN
  8. #include <boost/test/unit_test.hpp>
  9. #include <boost/test/tools/floating_point_comparison.hpp>
  10. #include <boost/math/distributions/non_central_t.hpp>
  11. #include <boost/type_traits/is_floating_point.hpp>
  12. #include <boost/array.hpp>
  13. #include "functor.hpp"
  14. #include "test_out_of_range.hpp"
  15. #include "handle_test_result.hpp"
  16. #include "table_type.hpp"
  17. #define BOOST_CHECK_CLOSE_EX(a, b, prec, i) \
  18. {\
  19. unsigned int failures = boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed;\
  20. BOOST_CHECK_CLOSE(a, b, prec); \
  21. if(failures != boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed)\
  22. {\
  23. std::cerr << "Failure was at row " << i << std::endl;\
  24. std::cerr << std::setprecision(35); \
  25. std::cerr << "{ " << data[i][0] << " , " << data[i][1] << " , " << data[i][2];\
  26. std::cerr << " , " << data[i][3] << " , " << data[i][4] << " } " << std::endl;\
  27. }\
  28. }
  29. #define BOOST_CHECK_EX(a, i) \
  30. {\
  31. unsigned int failures = boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed;\
  32. BOOST_CHECK(a); \
  33. if(failures != boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed)\
  34. {\
  35. std::cerr << "Failure was at row " << i << std::endl;\
  36. std::cerr << std::setprecision(35); \
  37. std::cerr << "{ " << data[i][0] << " , " << data[i][1] << " , " << data[i][2];\
  38. std::cerr << " , " << data[i][3] << " , " << data[i][4] << " } " << std::endl;\
  39. }\
  40. }
  41. template <class RealType>
  42. RealType naive_pdf(RealType v, RealType delta, RealType x)
  43. {
  44. }
  45. template <class RealType>
  46. RealType naive_mean(RealType v, RealType delta)
  47. {
  48. using boost::math::tgamma;
  49. return delta * sqrt(v / 2) * tgamma((v - 1) / 2) / tgamma(v / 2);
  50. }
  51. float naive_mean(float v, float delta)
  52. {
  53. return (float)naive_mean((double)v, (double)delta);
  54. }
  55. template <class RealType>
  56. RealType naive_variance(RealType v, RealType delta)
  57. {
  58. using boost::math::tgamma;
  59. RealType r = tgamma((v - 1) / 2) / tgamma(v / 2);
  60. r *= r;
  61. r *= -delta * delta * v / 2;
  62. r += (1 + delta * delta) * v / (v - 2);
  63. return r;
  64. }
  65. float naive_variance(float v, float delta)
  66. {
  67. return (float)naive_variance((double)v, (double)delta);
  68. }
  69. template <class RealType>
  70. RealType naive_skewness(RealType v, RealType delta)
  71. {
  72. using boost::math::tgamma;
  73. RealType tgr = tgamma((v - 1) / 2) / tgamma(v / 2);
  74. RealType r = delta * sqrt(v) * tgamma((v - 1) / 2)
  75. * (v * (-3 + delta * delta + 2 * v) / ((-3 + v) * (-2 + v))
  76. - 2 * ((1 + delta * delta) * v / (-2 + v) - delta * delta * v * tgr * tgr / 2));
  77. r /= boost::math::constants::root_two<RealType>()
  78. * pow(((1 + delta*delta) * v / (-2 + v) - delta*delta*v*tgr*tgr / 2), RealType(1.5f))
  79. * tgamma(v / 2);
  80. return r;
  81. }
  82. float naive_skewness(float v, float delta)
  83. {
  84. return (float)naive_skewness((double)v, (double)delta);
  85. }
  86. template <class RealType>
  87. RealType naive_kurtosis_excess(RealType v, RealType delta)
  88. {
  89. using boost::math::tgamma;
  90. RealType tgr = tgamma((v - 1) / 2) / tgamma(v / 2);
  91. RealType r = -delta * delta * v * tgr * tgr / 2;
  92. r *= v * (delta * delta * (1 + v) + 3 * (-5 + 3 * v)) / ((-3 + v)*(-2 + v))
  93. - 3 * ((1 + delta * delta) * v / (-2 + v) - delta * delta * v * tgr * tgr / 2);
  94. r += (3 + 6 * delta * delta + delta * delta * delta * delta)* v * v
  95. / ((-4 + v) * (-2 + v));
  96. r /= (1 + delta*delta)*v / (-2 + v) - delta*delta*v *tgr*tgr / 2;
  97. r /= (1 + delta*delta)*v / (-2 + v) - delta*delta*v *tgr*tgr / 2;
  98. return r;
  99. }
  100. float naive_kurtosis_excess(float v, float delta)
  101. {
  102. return (float)naive_kurtosis_excess((double)v, (double)delta);
  103. }
  104. template <class RealType>
  105. void test_spot(
  106. RealType df, // Degrees of freedom
  107. RealType ncp, // non-centrality param
  108. RealType t, // T statistic
  109. RealType P, // CDF
  110. RealType Q, // Complement of CDF
  111. RealType tol) // Test tolerance
  112. {
  113. // An extra fudge factor for real_concept which has a less accurate tgamma:
  114. RealType tolerance_tgamma_extra = std::numeric_limits<RealType>::is_specialized ? 1 : 5;
  115. boost::math::non_central_t_distribution<RealType> dist(df, ncp);
  116. BOOST_CHECK_CLOSE(
  117. cdf(dist, t), P, tol);
  118. #ifndef BOOST_NO_EXCEPTIONS
  119. try{
  120. BOOST_CHECK_CLOSE(
  121. mean(dist), naive_mean(df, ncp), tol);
  122. BOOST_CHECK_CLOSE(
  123. variance(dist), naive_variance(df, ncp), tol);
  124. BOOST_CHECK_CLOSE(
  125. skewness(dist), naive_skewness(df, ncp), tol * 10 * tolerance_tgamma_extra);
  126. BOOST_CHECK_CLOSE(
  127. kurtosis_excess(dist), naive_kurtosis_excess(df, ncp), tol * 50 * tolerance_tgamma_extra);
  128. BOOST_CHECK_CLOSE(
  129. kurtosis(dist), 3 + naive_kurtosis_excess(df, ncp), tol * 50 * tolerance_tgamma_extra);
  130. }
  131. catch(const std::domain_error&)
  132. {
  133. }
  134. #endif
  135. /*
  136. BOOST_CHECK_CLOSE(
  137. pdf(dist, t), naive_pdf(dist.degrees_of_freedom(), ncp, t), tol * 50);
  138. */
  139. if((P < 0.99) && (Q < 0.99))
  140. {
  141. //
  142. // We can only check this if P is not too close to 1,
  143. // so that we can guarantee Q is reasonably free of error:
  144. //
  145. BOOST_CHECK_CLOSE(
  146. cdf(complement(dist, t)), Q, tol);
  147. BOOST_CHECK_CLOSE(
  148. quantile(dist, P), t, tol * 10);
  149. BOOST_CHECK_CLOSE(
  150. quantile(complement(dist, Q)), t, tol * 10);
  151. /* Removed because can give more than one solution.
  152. BOOST_CHECK_CLOSE(
  153. dist.find_degrees_of_freedom(ncp, t, P), df, tol * 10);
  154. BOOST_CHECK_CLOSE(
  155. dist.find_degrees_of_freedom(boost::math::complement(ncp, t, Q)), df, tol * 10);
  156. BOOST_CHECK_CLOSE(
  157. dist.find_non_centrality(df, t, P), ncp, tol * 10);
  158. BOOST_CHECK_CLOSE(
  159. dist.find_non_centrality(boost::math::complement(df, t, Q)), ncp, tol * 10);
  160. */
  161. }
  162. }
  163. template <class RealType> // Any floating-point type RealType.
  164. void test_spots(RealType)
  165. {
  166. using namespace std;
  167. //
  168. // Approx limit of test data is 12 digits expressed here as a percentage:
  169. //
  170. RealType tolerance = (std::max)(
  171. boost::math::tools::epsilon<RealType>(),
  172. (RealType)5e-12f) * 100;
  173. //
  174. // At float precision we need to up the tolerance, since
  175. // the input values are rounded off to inexact quantities
  176. // the results get thrown off by a noticeable amount.
  177. //
  178. if(boost::math::tools::digits<RealType>() < 50)
  179. tolerance *= 50;
  180. if(boost::is_floating_point<RealType>::value != 1)
  181. tolerance *= 20; // real_concept special functions are less accurate
  182. cout << "Tolerance = " << tolerance << "%." << endl;
  183. //
  184. // Test data is taken from:
  185. //
  186. // Computing discrete mixtures of continuous
  187. // distributions: noncentral chisquare, noncentral t
  188. // and the distribution of the square of the sample
  189. // multiple correlation coeficient.
  190. // Denise Benton, K. Krishnamoorthy.
  191. // Computational Statistics & Data Analysis 43 (2003) 249 - 267
  192. //
  193. test_spot(
  194. static_cast<RealType>(3), // degrees of freedom
  195. static_cast<RealType>(1), // non centrality
  196. static_cast<RealType>(2.34), // T
  197. static_cast<RealType>(0.801888999613917), // Probability of result (CDF), P
  198. static_cast<RealType>(1 - 0.801888999613917), // Q = 1 - P
  199. tolerance);
  200. test_spot(
  201. static_cast<RealType>(126), // degrees of freedom
  202. static_cast<RealType>(-2), // non centrality
  203. static_cast<RealType>(-4.33), // T
  204. static_cast<RealType>(1.252846196792878e-2), // Probability of result (CDF), P
  205. static_cast<RealType>(1 - 1.252846196792878e-2), // Q = 1 - P
  206. tolerance);
  207. test_spot(
  208. static_cast<RealType>(20), // degrees of freedom
  209. static_cast<RealType>(23), // non centrality
  210. static_cast<RealType>(23), // T
  211. static_cast<RealType>(0.460134400391924), // Probability of result (CDF), P
  212. static_cast<RealType>(1 - 0.460134400391924), // Q = 1 - P
  213. tolerance);
  214. test_spot(
  215. static_cast<RealType>(20), // degrees of freedom
  216. static_cast<RealType>(33), // non centrality
  217. static_cast<RealType>(34), // T
  218. static_cast<RealType>(0.532008386378725), // Probability of result (CDF), P
  219. static_cast<RealType>(1 - 0.532008386378725), // Q = 1 - P
  220. tolerance);
  221. test_spot(
  222. static_cast<RealType>(12), // degrees of freedom
  223. static_cast<RealType>(38), // non centrality
  224. static_cast<RealType>(39), // T
  225. static_cast<RealType>(0.495868184917805), // Probability of result (CDF), P
  226. static_cast<RealType>(1 - 0.495868184917805), // Q = 1 - P
  227. tolerance);
  228. test_spot(
  229. static_cast<RealType>(12), // degrees of freedom
  230. static_cast<RealType>(39), // non centrality
  231. static_cast<RealType>(39), // T
  232. static_cast<RealType>(0.446304024668836), // Probability of result (CDF), P
  233. static_cast<RealType>(1 - 0.446304024668836), // Q = 1 - P
  234. tolerance);
  235. test_spot(
  236. static_cast<RealType>(200), // degrees of freedom
  237. static_cast<RealType>(38), // non centrality
  238. static_cast<RealType>(39), // T
  239. static_cast<RealType>(0.666194209961795), // Probability of result (CDF), P
  240. static_cast<RealType>(1 - 0.666194209961795), // Q = 1 - P
  241. tolerance);
  242. test_spot(
  243. static_cast<RealType>(200), // degrees of freedom
  244. static_cast<RealType>(42), // non centrality
  245. static_cast<RealType>(40), // T
  246. static_cast<RealType>(0.179292265426085), // Probability of result (CDF), P
  247. static_cast<RealType>(1 - 0.179292265426085), // Q = 1 - P
  248. tolerance);
  249. // From https://svn.boost.org/trac/boost/ticket/10480.
  250. // Test value from Mathematica N[CDF[NoncentralStudentTDistribution[2, 4], 5], 35]:
  251. test_spot(
  252. static_cast<RealType>(2), // degrees of freedom
  253. static_cast<RealType>(4), // non centrality
  254. static_cast<RealType>(5), // T
  255. static_cast<RealType>(0.53202069866995310466912357978934321L), // Probability of result (CDF), P
  256. static_cast<RealType>(1 - 0.53202069866995310466912357978934321L), // Q = 1 - P
  257. tolerance);
  258. /* This test fails
  259. "Result of tgamma is too large to represent" at naive_mean check for max and infinity.
  260. if (std::numeric_limits<RealType>::has_infinity)
  261. {
  262. test_spot(
  263. //static_cast<RealType>(std::numeric_limits<RealType>::infinity()), // degrees of freedom
  264. static_cast<RealType>((std::numeric_limits<RealType>::max)()), // degrees of freedom
  265. static_cast<RealType>(10), // non centrality
  266. static_cast<RealType>(11), // T
  267. static_cast<RealType>(0.84134474606854293), // Probability of result (CDF), P
  268. static_cast<RealType>(0.15865525393145707), // Q = 1 - P
  269. tolerance);
  270. }
  271. */
  272. boost::math::non_central_t_distribution<RealType> dist(static_cast<RealType>(8), static_cast<RealType>(12));
  273. BOOST_CHECK_CLOSE(pdf(dist, 12), static_cast<RealType>(1.235329715425894935157684607751972713457e-1L), tolerance);
  274. BOOST_CHECK_CLOSE(pdf(boost::math::non_central_t_distribution<RealType>(126, -2), -4), static_cast<RealType>(5.797932289365814702402873546466798025787e-2L), tolerance);
  275. BOOST_CHECK_CLOSE(pdf(boost::math::non_central_t_distribution<RealType>(126, 2), 4), static_cast<RealType>(5.797932289365814702402873546466798025787e-2L), tolerance);
  276. BOOST_CHECK_CLOSE(pdf(boost::math::non_central_t_distribution<RealType>(126, 2), 0), static_cast<RealType>(5.388394890639957139696546086044839573749e-2L), tolerance);
  277. // Error handling checks:
  278. //check_out_of_range<boost::math::non_central_t_distribution<RealType> >(1, 1); // Fails one check because df for this distribution *can* be infinity.
  279. BOOST_MATH_CHECK_THROW(pdf(boost::math::non_central_t_distribution<RealType>(0, 1), 0), std::domain_error);
  280. BOOST_MATH_CHECK_THROW(pdf(boost::math::non_central_t_distribution<RealType>(-1, 1), 0), std::domain_error);
  281. BOOST_MATH_CHECK_THROW(quantile(boost::math::non_central_t_distribution<RealType>(1, 1), -1), std::domain_error);
  282. BOOST_MATH_CHECK_THROW(quantile(boost::math::non_central_t_distribution<RealType>(1, 1), 2), std::domain_error);
  283. } // template <class RealType>void test_spots(RealType)
  284. template <class T>
  285. T nct_cdf(T df, T nc, T x)
  286. {
  287. return cdf(boost::math::non_central_t_distribution<T>(df, nc), x);
  288. }
  289. template <class T>
  290. T nct_ccdf(T df, T nc, T x)
  291. {
  292. return cdf(complement(boost::math::non_central_t_distribution<T>(df, nc), x));
  293. }
  294. template <typename Real, typename T>
  295. void do_test_nc_t(T& data, const char* type_name, const char* test)
  296. {
  297. typedef Real value_type;
  298. std::cout << "Testing: " << test << std::endl;
  299. #ifdef NC_T_CDF_FUNCTION_TO_TEST
  300. value_type(*fp1)(value_type, value_type, value_type) = NC_T_CDF_FUNCTION_TO_TEST;
  301. #else
  302. value_type(*fp1)(value_type, value_type, value_type) = nct_cdf;
  303. #endif
  304. boost::math::tools::test_result<value_type> result;
  305. #if !(defined(ERROR_REPORTING_MODE) && !defined(NC_T_CDF_FUNCTION_TO_TEST))
  306. result = boost::math::tools::test_hetero<Real>(
  307. data,
  308. bind_func<Real>(fp1, 0, 1, 2),
  309. extract_result<Real>(3));
  310. handle_test_result(result, data[result.worst()], result.worst(),
  311. type_name, "non central t CDF", test);
  312. #endif
  313. #if !(defined(ERROR_REPORTING_MODE) && !defined(NC_T_CCDF_FUNCTION_TO_TEST))
  314. #ifdef NC_T_CCDF_FUNCTION_TO_TEST
  315. fp1 = NC_T_CCDF_FUNCTION_TO_TEST;
  316. #else
  317. fp1 = nct_ccdf;
  318. #endif
  319. result = boost::math::tools::test_hetero<Real>(
  320. data,
  321. bind_func<Real>(fp1, 0, 1, 2),
  322. extract_result<Real>(4));
  323. handle_test_result(result, data[result.worst()], result.worst(),
  324. type_name, "non central t CDF complement", test);
  325. std::cout << std::endl;
  326. #endif
  327. }
  328. template <typename Real, typename T>
  329. void quantile_sanity_check(T& data, const char* type_name, const char* test)
  330. {
  331. #ifndef ERROR_REPORTING_MODE
  332. typedef Real value_type;
  333. //
  334. // Tests with type real_concept take rather too long to run, so
  335. // for now we'll disable them:
  336. //
  337. if(!boost::is_floating_point<value_type>::value)
  338. return;
  339. std::cout << "Testing: " << type_name << " quantile sanity check, with tests " << test << std::endl;
  340. //
  341. // These sanity checks test for a round trip accuracy of one half
  342. // of the bits in T, unless T is type float, in which case we check
  343. // for just one decimal digit. The problem here is the sensitivity
  344. // of the functions, not their accuracy. This test data was generated
  345. // for the forward functions, which means that when it is used as
  346. // the input to the inverses then it is necessarily inexact. This rounding
  347. // of the input is what makes the data unsuitable for use as an accuracy check,
  348. // and also demonstrates that you can't in general round-trip these functions.
  349. // It is however a useful sanity check.
  350. //
  351. value_type precision = static_cast<value_type>(ldexp(1.0, 1 - boost::math::policies::digits<value_type, boost::math::policies::policy<> >() / 2)) * 100;
  352. if(boost::math::policies::digits<value_type, boost::math::policies::policy<> >() < 50)
  353. precision = 1; // 1% or two decimal digits, all we can hope for when the input is truncated to float
  354. for(unsigned i = 0; i < data.size(); ++i)
  355. {
  356. if(data[i][3] == 0)
  357. {
  358. BOOST_CHECK(0 == quantile(boost::math::non_central_t_distribution<value_type>(data[i][0], data[i][1]), data[i][3]));
  359. }
  360. else if(data[i][3] < 0.9999f)
  361. {
  362. value_type p = quantile(boost::math::non_central_t_distribution<value_type>(data[i][0], data[i][1]), data[i][3]);
  363. value_type pt = data[i][2];
  364. BOOST_CHECK_CLOSE_EX(pt, p, precision, i);
  365. }
  366. if(data[i][4] == 0)
  367. {
  368. BOOST_CHECK(0 == quantile(complement(boost::math::non_central_t_distribution<value_type>(data[i][0], data[i][1]), data[i][3])));
  369. }
  370. else if(data[i][4] < 0.9999f)
  371. {
  372. value_type p = quantile(complement(boost::math::non_central_t_distribution<value_type>(data[i][0], data[i][1]), data[i][4]));
  373. value_type pt = data[i][2];
  374. BOOST_CHECK_CLOSE_EX(pt, p, precision, i);
  375. }
  376. if(boost::math::tools::digits<value_type>() > 50)
  377. {
  378. //
  379. // Sanity check mode, the accuracy of
  380. // the mode is at *best* the square root of the accuracy of the PDF:
  381. //
  382. #ifndef BOOST_NO_EXCEPTIONS
  383. try{
  384. value_type m = mode(boost::math::non_central_t_distribution<value_type>(data[i][0], data[i][1]));
  385. value_type p = pdf(boost::math::non_central_t_distribution<value_type>(data[i][0], data[i][1]), m);
  386. value_type delta = (std::max)(fabs(m * sqrt(precision) * 50), sqrt(precision) * 50);
  387. BOOST_CHECK_EX(pdf(boost::math::non_central_t_distribution<value_type>(data[i][0], data[i][1]), m + delta) <= p, i);
  388. BOOST_CHECK_EX(pdf(boost::math::non_central_t_distribution<value_type>(data[i][0], data[i][1]), m - delta) <= p, i);
  389. }
  390. catch(const boost::math::evaluation_error&) {}
  391. #endif
  392. #if 0
  393. //
  394. // Sanity check degrees-of-freedom finder, don't bother at float
  395. // precision though as there's not enough data in the probability
  396. // values to get back to the correct degrees of freedom or
  397. // non-centrality parameter:
  398. //
  399. try{
  400. if((data[i][3] < 0.99) && (data[i][3] != 0))
  401. {
  402. BOOST_CHECK_CLOSE_EX(
  403. boost::math::non_central_t_distribution<value_type>::find_degrees_of_freedom(data[i][1], data[i][2], data[i][3]),
  404. data[i][0], precision, i);
  405. BOOST_CHECK_CLOSE_EX(
  406. boost::math::non_central_t_distribution<value_type>::find_non_centrality(data[i][0], data[i][2], data[i][3]),
  407. data[i][1], precision, i);
  408. }
  409. if((data[i][4] < 0.99) && (data[i][4] != 0))
  410. {
  411. BOOST_CHECK_CLOSE_EX(
  412. boost::math::non_central_t_distribution<value_type>::find_degrees_of_freedom(boost::math::complement(data[i][1], data[i][2], data[i][4])),
  413. data[i][0], precision, i);
  414. BOOST_CHECK_CLOSE_EX(
  415. boost::math::non_central_t_distribution<value_type>::find_non_centrality(boost::math::complement(data[i][0], data[i][2], data[i][4])),
  416. data[i][1], precision, i);
  417. }
  418. }
  419. catch(const std::exception& e)
  420. {
  421. BOOST_ERROR(e.what());
  422. }
  423. #endif
  424. }
  425. }
  426. #endif
  427. }
  428. template <typename T>
  429. void test_accuracy(T, const char* type_name)
  430. {
  431. #include "nct.ipp"
  432. do_test_nc_t<T>(nct, type_name, "Non Central T");
  433. quantile_sanity_check<T>(nct, type_name, "Non Central T");
  434. if(std::numeric_limits<T>::is_specialized)
  435. {
  436. //
  437. // Don't run these tests for real_concept: they take too long and don't converge
  438. // without numeric_limits and lanczos support:
  439. //
  440. #include "nct_small_delta.ipp"
  441. do_test_nc_t<T>(nct_small_delta, type_name, "Non Central T (small non-centrality)");
  442. quantile_sanity_check<T>(nct_small_delta, type_name, "Non Central T (small non-centrality)");
  443. #include "nct_asym.ipp"
  444. do_test_nc_t<T>(nct_asym, type_name, "Non Central T (large parameters)");
  445. quantile_sanity_check<T>(nct_asym, type_name, "Non Central T (large parameters)");
  446. }
  447. }
  448. template <class RealType>
  449. void test_big_df(RealType)
  450. {
  451. using namespace boost::math;
  452. if(typeid(RealType) != typeid(boost::math::concepts::real_concept))
  453. { // Ordinary floats only.
  454. // Could also test if (std::numeric_limits<RealType>::is_specialized);
  455. RealType tolerance = 10 * boost::math::tools::epsilon<RealType>(); // static_cast<RealType>(1e-14); //
  456. std::cout.precision(17); // Note: need to reset after calling BOOST_CHECK_s
  457. // due to buglet in Boost.test that fails to restore precision corrrectly.
  458. // Test for large degrees of freedom when should be same as normal.
  459. RealType inf =
  460. (std::numeric_limits<RealType>::has_infinity) ?
  461. std::numeric_limits<RealType>::infinity()
  462. :
  463. boost::math::tools::max_value<RealType>();
  464. RealType nan = std::numeric_limits<RealType>::quiet_NaN();
  465. // Tests for df = max_value and infinity.
  466. RealType max_val = boost::math::tools::max_value<RealType>();
  467. non_central_t_distribution<RealType> maxdf(max_val, 0);
  468. BOOST_CHECK_EQUAL(maxdf.degrees_of_freedom(), max_val);
  469. non_central_t_distribution<RealType> infdf(inf, 0);
  470. BOOST_CHECK_EQUAL(infdf.degrees_of_freedom(), inf);
  471. BOOST_CHECK_EQUAL(mean(infdf), 0);
  472. BOOST_CHECK_EQUAL(mean(maxdf), 0);
  473. BOOST_CHECK_EQUAL(variance(infdf), 1);
  474. BOOST_CHECK_EQUAL(variance(maxdf), 1);
  475. BOOST_CHECK_EQUAL(skewness(infdf), 0);
  476. BOOST_CHECK_EQUAL(skewness(maxdf), 0);
  477. BOOST_CHECK_EQUAL(kurtosis_excess(infdf), 3);
  478. BOOST_CHECK_CLOSE_FRACTION(kurtosis_excess(maxdf), static_cast<RealType>(3), tolerance);
  479. // Bad df examples.
  480. #ifndef BOOST_NO_EXCEPTIONS
  481. BOOST_MATH_CHECK_THROW(non_central_t_distribution<RealType> minfdf(-inf, 0), std::domain_error);
  482. BOOST_MATH_CHECK_THROW(non_central_t_distribution<RealType> minfdf(nan, 0), std::domain_error);
  483. BOOST_MATH_CHECK_THROW(non_central_t_distribution<RealType> minfdf(-nan, 0), std::domain_error);
  484. #else
  485. BOOST_MATH_CHECK_THROW(non_central_t_distribution<RealType>(-inf, 0), std::domain_error);
  486. BOOST_MATH_CHECK_THROW(non_central_t_distribution<RealType>(nan, 0), std::domain_error);
  487. BOOST_MATH_CHECK_THROW(non_central_t_distribution<RealType>(-nan, 0), std::domain_error);
  488. #endif
  489. // BOOST_CHECK_CLOSE_FRACTION(pdf(infdf, 0), static_cast<RealType>(0.3989422804014326779399460599343818684759L), tolerance);
  490. BOOST_CHECK_CLOSE_FRACTION(pdf(maxdf, 0), boost::math::constants::one_div_root_two_pi<RealType>(), tolerance);
  491. BOOST_CHECK_CLOSE_FRACTION(pdf(infdf, 0), boost::math::constants::one_div_root_two_pi<RealType>(), tolerance);
  492. BOOST_CHECK_CLOSE_FRACTION(cdf(infdf, 0), boost::math::constants::half<RealType>(), tolerance);
  493. BOOST_CHECK_CLOSE_FRACTION(cdf(maxdf, 0), boost::math::constants::half<RealType>(), tolerance);
  494. // non-centrality delta = 10
  495. // Degrees of freedom = Max value and = infinity should be very close.
  496. non_central_t_distribution<RealType> maxdf10(max_val, 10);
  497. non_central_t_distribution<RealType> infdf10(inf, 10);
  498. BOOST_CHECK_EQUAL(infdf10.degrees_of_freedom(), inf);
  499. BOOST_CHECK_EQUAL(infdf10.non_centrality(), 10);
  500. BOOST_CHECK_EQUAL(mean(infdf10), 10);
  501. BOOST_CHECK_CLOSE_FRACTION(mean(maxdf10), static_cast<RealType>(10), tolerance);
  502. BOOST_CHECK_CLOSE_FRACTION(pdf(infdf10, 11), pdf(maxdf10, 11), tolerance); //
  503. BOOST_CHECK_CLOSE_FRACTION(cdf(complement(infdf10, 11)), 1 - cdf(infdf10, 11), tolerance); //
  504. BOOST_CHECK_CLOSE_FRACTION(cdf(complement(maxdf10, 11)), 1 - cdf(maxdf10, 11), tolerance); //
  505. BOOST_CHECK_CLOSE_FRACTION(cdf(complement(infdf10, 11)), 1 - cdf(maxdf10, 11), tolerance); //
  506. std::cout.precision(17);
  507. //std::cout << "cdf(maxdf10, 11) = " << cdf(maxdf10, 11) << ' ' << cdf(complement(maxdf10, 11)) << endl;
  508. //std::cout << "cdf(infdf10, 11) = " << cdf(infdf10, 11) << ' ' << cdf(complement(infdf10, 11)) << endl;
  509. //std::cout << "quantile(maxdf10, 0.5) = " << quantile(maxdf10, 0.5) << std::endl; // quantile(maxdf10, 0.5) = 10.000000000000004
  510. //std::cout << "quantile(infdf10, 0.5) = " << ' ' << quantile(infdf10, 0.5) << std::endl; // quantile(infdf10, 0.5) = 10
  511. BOOST_CHECK_CLOSE_FRACTION(quantile(infdf10, 0.5), static_cast<RealType>(10), tolerance);
  512. BOOST_CHECK_CLOSE_FRACTION(quantile(maxdf10, 0.5), static_cast<RealType>(10), tolerance);
  513. BOOST_TEST_MESSAGE("non_central_t_distribution<RealType> infdf100(inf, 100);");
  514. non_central_t_distribution<RealType> infdf100(inf, 100);
  515. BOOST_TEST_MESSAGE("non_central_t_distribution<RealType> maxdf100(max_val, 100);");
  516. non_central_t_distribution<RealType> maxdf100(max_val, 100);
  517. BOOST_TEST_MESSAGE("BOOST_CHECK_CLOSE_FRACTION(quantile(infdf100, 0.5), static_cast<RealType>(100), tolerance);");
  518. BOOST_CHECK_CLOSE_FRACTION(quantile(infdf100, 0.5), static_cast<RealType>(100), tolerance);
  519. BOOST_TEST_MESSAGE("BOOST_CHECK_CLOSE_FRACTION(quantile(maxdf100, 0.5), static_cast<RealType>(100), tolerance);");
  520. BOOST_CHECK_CLOSE_FRACTION(quantile(maxdf100, 0.5), static_cast<RealType>(100), tolerance);
  521. { // Loop back.
  522. RealType p = static_cast<RealType>(0.01);
  523. RealType x = quantile(infdf10, p);
  524. RealType c = cdf(infdf10, x);
  525. BOOST_CHECK_CLOSE_FRACTION(c, p, tolerance);
  526. }
  527. {
  528. RealType q = static_cast<RealType>(0.99);
  529. RealType x = quantile(complement(infdf10, q));
  530. RealType c = cdf(complement(infdf10, x));
  531. BOOST_CHECK_CLOSE_FRACTION(c, q, tolerance);
  532. }
  533. { // Loop back.
  534. RealType p = static_cast<RealType>(0.99);
  535. RealType x = quantile(infdf10, p);
  536. RealType c = cdf(infdf10, x);
  537. BOOST_CHECK_CLOSE_FRACTION(c, p, tolerance);
  538. }
  539. {
  540. RealType q = static_cast<RealType>(0.01);
  541. RealType x = quantile(complement(infdf10, q));
  542. RealType c = cdf(complement(infdf10, x));
  543. BOOST_CHECK_CLOSE_FRACTION(c, q, tolerance * 2); // c{0.0100000128} and q{0.00999999978}
  544. }
  545. //RealType cinf = quantile(infdf10, 0.25);
  546. //std::cout << cinf << ' ' << cdf(infdf10, cinf) << std::endl; // 9.32551 0.25
  547. //RealType cmax = quantile(maxdf10, 0.25);
  548. //std::cout << cmax << ' ' << cdf(maxdf10, cmax) << std::endl; // 9.32551 0.25
  549. //RealType cinfc = quantile(complement(infdf10, 0.75));
  550. //std::cout << cinfc << ' ' << cdf(infdf10, cinfc) << std::endl; // 9.32551 0.25
  551. //RealType cmaxc = quantile(complement(maxdf10, 0.75));
  552. //std::cout << cmaxc << ' ' << cdf(maxdf10, cmaxc) << std::endl; // 9.32551 0.25
  553. BOOST_CHECK_CLOSE_FRACTION(quantile(infdf10, 0.5), quantile(maxdf10, 0.5), tolerance); //
  554. BOOST_CHECK_CLOSE_FRACTION(quantile(infdf10, 0.2), quantile(maxdf10, 0.2), tolerance); //
  555. BOOST_CHECK_CLOSE_FRACTION(quantile(infdf10, 0.8), quantile(maxdf10, 0.8), tolerance); //
  556. BOOST_CHECK_CLOSE_FRACTION(quantile(infdf10, 0.25), quantile(complement(infdf10, 0.75)), tolerance); //
  557. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(infdf10, 0.5)), quantile(complement(maxdf10, 0.5)), tolerance); //
  558. BOOST_CHECK_CLOSE_FRACTION(quantile(maxdf10, 0.25), quantile(complement(maxdf10, 0.75)), tolerance); //
  559. BOOST_CHECK_CLOSE_FRACTION(quantile(infdf10, 0.99), quantile(complement(infdf10, 0.01)), tolerance); //
  560. BOOST_CHECK_CLOSE_FRACTION(quantile(infdf10, 0.4), quantile(complement(infdf10, 0.6)), tolerance); //
  561. BOOST_CHECK_CLOSE_FRACTION(quantile(infdf10, 0.01), quantile(complement(infdf10, 1 - 0.01)), tolerance); //
  562. }
  563. } // void test_big_df(RealType)
  564. template <class RealType>
  565. void test_ignore_policy(RealType)
  566. {
  567. // Check on returns when errors are ignored.
  568. if((typeid(RealType) != typeid(boost::math::concepts::real_concept))
  569. && std::numeric_limits<RealType>::has_infinity
  570. && std::numeric_limits<RealType>::has_quiet_NaN
  571. )
  572. { // Ordinary floats only.
  573. using namespace boost::math;
  574. // RealType inf = std::numeric_limits<RealType>::infinity();
  575. RealType nan = std::numeric_limits<RealType>::quiet_NaN();
  576. using boost::math::policies::policy;
  577. // Types of error whose action can be altered by policies:.
  578. //using boost::math::policies::evaluation_error;
  579. //using boost::math::policies::domain_error;
  580. //using boost::math::policies::overflow_error;
  581. //using boost::math::policies::underflow_error;
  582. //using boost::math::policies::domain_error;
  583. //using boost::math::policies::pole_error;
  584. //// Actions on error (in enum error_policy_type):
  585. //using boost::math::policies::errno_on_error;
  586. //using boost::math::policies::ignore_error;
  587. //using boost::math::policies::throw_on_error;
  588. //using boost::math::policies::denorm_error;
  589. //using boost::math::policies::pole_error;
  590. //using boost::math::policies::user_error;
  591. typedef policy<
  592. boost::math::policies::domain_error<boost::math::policies::ignore_error>,
  593. boost::math::policies::overflow_error<boost::math::policies::ignore_error>,
  594. boost::math::policies::underflow_error<boost::math::policies::ignore_error>,
  595. boost::math::policies::denorm_error<boost::math::policies::ignore_error>,
  596. boost::math::policies::pole_error<boost::math::policies::ignore_error>,
  597. boost::math::policies::evaluation_error<boost::math::policies::ignore_error>
  598. > ignore_all_policy;
  599. typedef non_central_t_distribution<RealType, ignore_all_policy> ignore_error_non_central_t;
  600. // Only test NaN and infinity if type has these features (realconcept returns zero).
  601. // Integers are always converted to RealType,
  602. // others requires static cast to RealType from long double.
  603. if(std::numeric_limits<RealType>::has_quiet_NaN)
  604. {
  605. // Mean
  606. BOOST_CHECK((boost::math::isnan)(mean(ignore_error_non_central_t(-nan, 0))));
  607. BOOST_CHECK((boost::math::isnan)(mean(ignore_error_non_central_t(+nan, 0))));
  608. BOOST_CHECK((boost::math::isnan)(mean(ignore_error_non_central_t(-1, 0))));
  609. BOOST_CHECK((boost::math::isnan)(mean(ignore_error_non_central_t(0, 0))));
  610. BOOST_CHECK((boost::math::isnan)(mean(ignore_error_non_central_t(1, 0))));
  611. BOOST_CHECK((boost::math::isnan)(mean(ignore_error_non_central_t(2, nan))));
  612. BOOST_CHECK((boost::math::isnan)(mean(ignore_error_non_central_t(nan, nan))));
  613. BOOST_CHECK(boost::math::isfinite(mean(ignore_error_non_central_t(2, 0)))); // OK
  614. // Variance
  615. BOOST_CHECK((boost::math::isnan)(variance(ignore_error_non_central_t(nan, 0))));
  616. BOOST_CHECK((boost::math::isnan)(variance(ignore_error_non_central_t(1, nan))));
  617. BOOST_CHECK((boost::math::isnan)(variance(ignore_error_non_central_t(2, nan))));
  618. BOOST_CHECK((boost::math::isnan)(variance(ignore_error_non_central_t(-1, 0))));
  619. BOOST_CHECK((boost::math::isnan)(variance(ignore_error_non_central_t(0, 0))));
  620. BOOST_CHECK((boost::math::isnan)(variance(ignore_error_non_central_t(1, 0))));
  621. BOOST_CHECK((boost::math::isnan)(variance(ignore_error_non_central_t(static_cast<RealType>(1.7L), 0))));
  622. BOOST_CHECK((boost::math::isnan)(variance(ignore_error_non_central_t(2, 0))));
  623. // Skewness
  624. BOOST_CHECK((boost::math::isnan)(skewness(ignore_error_non_central_t(std::numeric_limits<RealType>::quiet_NaN(), 0))));
  625. BOOST_CHECK((boost::math::isnan)(skewness(ignore_error_non_central_t(-1, 0))));
  626. BOOST_CHECK((boost::math::isnan)(skewness(ignore_error_non_central_t(0, 0))));
  627. BOOST_CHECK((boost::math::isnan)(skewness(ignore_error_non_central_t(1, 0))));
  628. BOOST_CHECK((boost::math::isnan)(skewness(ignore_error_non_central_t(2, 0))));
  629. BOOST_CHECK((boost::math::isnan)(skewness(ignore_error_non_central_t(3, 0))));
  630. // Kurtosis
  631. BOOST_CHECK((boost::math::isnan)(kurtosis(ignore_error_non_central_t(std::numeric_limits<RealType>::quiet_NaN(), 0))));
  632. BOOST_CHECK((boost::math::isnan)(kurtosis(ignore_error_non_central_t(-1, 0))));
  633. BOOST_CHECK((boost::math::isnan)(kurtosis(ignore_error_non_central_t(0, 0))));
  634. BOOST_CHECK((boost::math::isnan)(kurtosis(ignore_error_non_central_t(1, 0))));
  635. BOOST_CHECK((boost::math::isnan)(kurtosis(ignore_error_non_central_t(2, 0))));
  636. BOOST_CHECK((boost::math::isnan)(kurtosis(ignore_error_non_central_t(static_cast<RealType>(2.0001L), 0))));
  637. BOOST_CHECK((boost::math::isnan)(kurtosis(ignore_error_non_central_t(3, 0))));
  638. BOOST_CHECK((boost::math::isnan)(kurtosis(ignore_error_non_central_t(4, 0))));
  639. // Kurtosis excess
  640. BOOST_CHECK((boost::math::isnan)(kurtosis_excess(ignore_error_non_central_t(std::numeric_limits<RealType>::quiet_NaN(), 0))));
  641. BOOST_CHECK((boost::math::isnan)(kurtosis_excess(ignore_error_non_central_t(-1, 0))));
  642. BOOST_CHECK((boost::math::isnan)(kurtosis_excess(ignore_error_non_central_t(0, 0))));
  643. BOOST_CHECK((boost::math::isnan)(kurtosis_excess(ignore_error_non_central_t(1, 0))));
  644. BOOST_CHECK((boost::math::isnan)(kurtosis_excess(ignore_error_non_central_t(2, 0))));
  645. BOOST_CHECK((boost::math::isnan)(kurtosis_excess(ignore_error_non_central_t(static_cast<RealType>(2.0001L), 0))));
  646. BOOST_CHECK((boost::math::isnan)(kurtosis_excess(ignore_error_non_central_t(3, 0))));
  647. BOOST_CHECK((boost::math::isnan)(kurtosis_excess(ignore_error_non_central_t(4, 0))));
  648. } // has_quiet_NaN
  649. BOOST_CHECK(boost::math::isfinite(mean(ignore_error_non_central_t(1 + std::numeric_limits<RealType>::epsilon(), 0))));
  650. BOOST_CHECK(boost::math::isfinite(variance(ignore_error_non_central_t(2 + 2 * std::numeric_limits<RealType>::epsilon(), 0))));
  651. BOOST_CHECK(boost::math::isfinite(variance(ignore_error_non_central_t(static_cast<RealType>(2.0001L), 0))));
  652. BOOST_CHECK(boost::math::isfinite(variance(ignore_error_non_central_t(2 + 2 * std::numeric_limits<RealType>::epsilon(), 0))));
  653. BOOST_CHECK(boost::math::isfinite(skewness(ignore_error_non_central_t(3 + 3 * std::numeric_limits<RealType>::epsilon(), 0))));
  654. BOOST_CHECK(boost::math::isfinite(kurtosis(ignore_error_non_central_t(4 + 4 * std::numeric_limits<RealType>::epsilon(), 0))));
  655. BOOST_CHECK(boost::math::isfinite(kurtosis(ignore_error_non_central_t(static_cast<RealType>(4.0001L), 0))));
  656. // check_out_of_range<non_central_t_distribution<RealType> >(1, 0); // Fails one check because allows df = infinity.
  657. check_support<non_central_t_distribution<RealType> >(non_central_t_distribution<RealType>(1, 0));
  658. } // ordinary floats.
  659. } // template <class RealType> void test_ignore_policy(RealType)