test_nc_f.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // test_nc_beta.cpp
  2. // Copyright John Maddock 2008.
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #include <pch.hpp>
  8. #ifdef _MSC_VER
  9. #pragma warning (disable:4127 4512)
  10. #endif
  11. #if !defined(TEST_FLOAT) && !defined(TEST_DOUBLE) && !defined(TEST_LDOUBLE) && !defined(TEST_REAL_CONCEPT)
  12. # define TEST_FLOAT
  13. # define TEST_DOUBLE
  14. # define TEST_LDOUBLE
  15. # define TEST_REAL_CONCEPT
  16. #endif
  17. #include <boost/math/tools/test.hpp>
  18. #include <boost/math/concepts/real_concept.hpp> // for real_concept
  19. #include <boost/math/distributions/non_central_f.hpp> // for chi_squared_distribution
  20. #define BOOST_TEST_MAIN
  21. #include <boost/test/unit_test.hpp> // for test_main
  22. #include <boost/test/results_collector.hpp>
  23. #include <boost/test/unit_test.hpp>
  24. #include <boost/test/tools/floating_point_comparison.hpp> // for BOOST_CHECK_CLOSE
  25. #include "test_out_of_range.hpp"
  26. #include "functor.hpp"
  27. #include "handle_test_result.hpp"
  28. #include <iostream>
  29. #include <iomanip>
  30. using std::cout;
  31. using std::endl;
  32. #include <limits>
  33. using std::numeric_limits;
  34. #define BOOST_CHECK_CLOSE_EX(a, b, prec, i) \
  35. {\
  36. unsigned int failures = boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed;\
  37. BOOST_CHECK_CLOSE(a, b, prec); \
  38. if(failures != boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed)\
  39. {\
  40. std::cerr << "Failure was at row " << i << std::endl;\
  41. std::cerr << std::setprecision(35); \
  42. std::cerr << "{ " << data[i][0] << " , " << data[i][1] << " , " << data[i][2];\
  43. std::cerr << " , " << data[i][3] << " , " << data[i][4] << " } " << std::endl;\
  44. }\
  45. }
  46. #define BOOST_CHECK_EX(a, i) \
  47. {\
  48. unsigned int failures = boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed;\
  49. BOOST_CHECK(a); \
  50. if(failures != boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed)\
  51. {\
  52. std::cerr << "Failure was at row " << i << std::endl;\
  53. std::cerr << std::setprecision(35); \
  54. std::cerr << "{ " << data[i][0] << " , " << data[i][1] << " , " << data[i][2];\
  55. std::cerr << " , " << data[i][3] << " , " << data[i][4] << " } " << std::endl;\
  56. }\
  57. }
  58. void expected_results()
  59. {
  60. //
  61. // Printing out the compiler/stdlib/platform names,
  62. // we do this to make it easier to mark up expected error rates.
  63. //
  64. std::cout << "Tests run with " << BOOST_COMPILER << ", "
  65. << BOOST_STDLIB << ", " << BOOST_PLATFORM << std::endl;
  66. }
  67. template <class RealType>
  68. RealType naive_pdf(RealType v1, RealType v2, RealType l, RealType f)
  69. {
  70. BOOST_MATH_STD_USING
  71. RealType sum = 0;
  72. for(int i = 0; ; ++i)
  73. {
  74. RealType term = -l/2 + log(l/2) * i;
  75. term += boost::math::lgamma(v2/2 + v1/2+i) - (boost::math::lgamma(v2/2) + boost::math::lgamma(v1/2+i));
  76. term -= boost::math::lgamma(RealType(i+1));
  77. term += log(v1/v2) * (v1/2+i) + log(v2 / (v2 + v1 * f)) * ((v1 + v2) / 2 + i);
  78. term += log(f) * (v1/2 - 1 + i);
  79. term = exp(term);
  80. sum += term;
  81. if((term/sum < boost::math::tools::epsilon<RealType>()) || (term == 0))
  82. break;
  83. }
  84. return sum;
  85. }
  86. template <class RealType>
  87. void test_spot(
  88. RealType a, // df1
  89. RealType b, // df2
  90. RealType ncp, // non-centrality param
  91. RealType x, // F statistic
  92. RealType P, // CDF
  93. RealType Q, // Complement of CDF
  94. RealType D, // PDF
  95. RealType tol) // Test tolerance
  96. {
  97. boost::math::non_central_f_distribution<RealType> dist(a, b, ncp);
  98. BOOST_CHECK_CLOSE(
  99. cdf(dist, x), P, tol);
  100. BOOST_CHECK_CLOSE(
  101. pdf(dist, x), D, tol);
  102. if(boost::math::tools::digits<RealType>() > 50)
  103. {
  104. //
  105. // The "naive" pdf calculation fails at float precision.
  106. //
  107. BOOST_CHECK_CLOSE(
  108. pdf(dist, x), naive_pdf(a, b, ncp, x), tol);
  109. }
  110. if((P < 0.99) && (Q < 0.99))
  111. {
  112. //
  113. // We can only check this if P is not too close to 1,
  114. // so that we can guarantee Q is reasonably free of error:
  115. //
  116. BOOST_CHECK_CLOSE(
  117. cdf(complement(dist, x)), Q, tol);
  118. BOOST_CHECK_CLOSE(
  119. quantile(dist, P), x, tol * 10);
  120. BOOST_CHECK_CLOSE(
  121. quantile(complement(dist, Q)), x, tol * 10);
  122. }
  123. if(boost::math::tools::digits<RealType>() > 50)
  124. {
  125. //
  126. // Sanity check mode:
  127. //
  128. RealType m = mode(dist);
  129. RealType p = pdf(dist, m);
  130. BOOST_CHECK(pdf(dist, m * (1 + sqrt(tol) * 10)) <= p);
  131. BOOST_CHECK(pdf(dist, m * (1 - sqrt(tol)) * 10) <= p);
  132. }
  133. }
  134. template <class RealType> // Any floating-point type RealType.
  135. void test_spots(RealType)
  136. {
  137. RealType tolerance = boost::math::tools::epsilon<RealType>() * 10000;
  138. cout << "Tolerance = " << (tolerance / 100) << "%." << endl;
  139. //
  140. // Spot tests from Mathematica computed values:
  141. //
  142. test_spot(
  143. RealType(5), // alpha
  144. RealType(2), // beta
  145. RealType(1), // non-centrality param
  146. RealType(1.5), // F statistic
  147. RealType(0.49845842011686358665786775091245664L), // CDF
  148. RealType(1 - 0.49845842011686358665786775091245664L), // Complement of CDF
  149. RealType(0.20251311620629730205859816288225385L), // PDF
  150. RealType(tolerance));
  151. test_spot(
  152. RealType(2), // alpha
  153. RealType(5), // beta
  154. RealType(1), // non-centrality param
  155. RealType(2), // F statistic
  156. RealType(0.64938711196845800322066756609406894L), // CDF
  157. RealType(1 - 0.64938711196845800322066756609406894L), // Complement of CDF
  158. RealType(0.15512617916132011524583796078456003L), // PDF
  159. RealType(tolerance));
  160. test_spot(
  161. RealType(100), // alpha
  162. RealType(5), // beta
  163. RealType(15), // non-centrality param
  164. RealType(105), // F statistic
  165. RealType(0.99996207325249555786258005958906310L), // CDF
  166. RealType(0.000037926747504442137419940410936905407L), // Complement of CDF
  167. RealType(8.9562292619539161551049126260104435e-7), // PDF
  168. RealType(tolerance * 10));
  169. test_spot(
  170. RealType(100), // alpha
  171. RealType(5), // beta
  172. RealType(15), // non-centrality param
  173. RealType(1.5), // F statistic
  174. RealType(0.57592315596686179870591317303126895L), // CDF
  175. RealType(1 - 0.57592315596686179870591317303126895L), // Complement of CDF
  176. RealType(0.36743745541686900593212039288061162L), // PDF
  177. RealType(tolerance * 5));
  178. test_spot(
  179. RealType(5), // alpha
  180. RealType(100), // beta
  181. RealType(102), // non-centrality param
  182. RealType(25), // F statistic
  183. RealType(0.74993383259829917593356265102363267L), // CDF
  184. RealType(1 - 0.74993383259829917593356265102363267L), // Complement of CDF
  185. RealType(0.054467600423154020554785779421659007L), // PDF
  186. RealType(tolerance * 5));
  187. test_spot(
  188. RealType(85), // alpha
  189. RealType(100), // beta
  190. RealType(0.5), // non-centrality param
  191. RealType(1.25), // F statistic
  192. RealType(0.85228624977948142884820398473385455L), // CDF
  193. RealType(0.14771375022051857115179601526614545L), // Complement of CDF
  194. RealType(0.88510283331631848299643323511414868L), // PDF
  195. RealType(tolerance * 5));
  196. //
  197. // Spot tests use values computed by the R statistical
  198. // package and the pbeta and dbeta functions:
  199. //
  200. tolerance = (std::max)(
  201. boost::math::tools::epsilon<RealType>() * 100,
  202. (RealType)1e-6) * 100;
  203. test_spot(
  204. RealType(85), // alpha
  205. RealType(100), // beta
  206. RealType(245), // non-centrality param
  207. RealType(3.5), // F statistic
  208. RealType(0.2697244), // CDF
  209. RealType(1 - 0.2697244), // Complement of CDF
  210. RealType(0.54352369104452836465948073900030320L), // PDF
  211. RealType(tolerance));
  212. BOOST_MATH_STD_USING
  213. //
  214. // 5 eps expressed as a persentage, otherwise the limit of the test data:
  215. //
  216. RealType tol2 = (std::max)(boost::math::tools::epsilon<RealType>() * 500, RealType(1e-25));
  217. RealType x = 2;
  218. boost::math::non_central_f_distribution<RealType> dist(20, 15, 30);
  219. // mean:
  220. BOOST_CHECK_CLOSE(
  221. mean(dist)
  222. , static_cast<RealType>(2.8846153846153846153846153846154L), tol2);
  223. // variance:
  224. BOOST_CHECK_CLOSE(
  225. variance(dist)
  226. , static_cast<RealType>(2.1422807961269499731038192576654L), tol2);
  227. // std deviation:
  228. BOOST_CHECK_CLOSE(
  229. standard_deviation(dist)
  230. , sqrt(variance(dist)), tol2);
  231. // hazard:
  232. BOOST_CHECK_CLOSE(
  233. hazard(dist, x)
  234. , pdf(dist, x) / cdf(complement(dist, x)), tol2);
  235. // cumulative hazard:
  236. BOOST_CHECK_CLOSE(
  237. chf(dist, x)
  238. , -log(cdf(complement(dist, x))), tol2);
  239. // coefficient_of_variation:
  240. BOOST_CHECK_CLOSE(
  241. coefficient_of_variation(dist)
  242. , standard_deviation(dist) / mean(dist), tol2);
  243. BOOST_CHECK_CLOSE(
  244. median(dist),
  245. quantile(
  246. dist,
  247. static_cast<RealType>(0.5)), static_cast<RealType>(tol2));
  248. // mode:
  249. BOOST_CHECK_CLOSE(
  250. mode(dist)
  251. , static_cast<RealType>(2.070019130232759428074835788815387743293972985542L), sqrt(tolerance));
  252. // skewness:
  253. BOOST_CHECK_CLOSE(
  254. skewness(dist)
  255. , static_cast<RealType>(2.1011821125804540669752380228510691320707051692719L), tol2);
  256. // kurtosis:
  257. BOOST_CHECK_CLOSE(
  258. kurtosis(dist)
  259. , 3 + kurtosis_excess(dist), tol2);
  260. // kurtosis excess:
  261. BOOST_CHECK_CLOSE(
  262. kurtosis_excess(dist)
  263. , static_cast<RealType>(13.225781681053154767604638331440974359675882226873L), tol2);
  264. // Error handling checks:
  265. check_out_of_range<boost::math::non_central_f_distribution<RealType> >(1, 1, 1);
  266. BOOST_MATH_CHECK_THROW(pdf(boost::math::non_central_f_distribution<RealType>(0, 1, 1), 0), std::domain_error);
  267. BOOST_MATH_CHECK_THROW(pdf(boost::math::non_central_f_distribution<RealType>(-1, 1, 1), 0), std::domain_error);
  268. BOOST_MATH_CHECK_THROW(pdf(boost::math::non_central_f_distribution<RealType>(1, 0, 1), 0), std::domain_error);
  269. BOOST_MATH_CHECK_THROW(pdf(boost::math::non_central_f_distribution<RealType>(1, -1, 1), 0), std::domain_error);
  270. BOOST_MATH_CHECK_THROW(quantile(boost::math::non_central_f_distribution<RealType>(1, 1, 1), -1), std::domain_error);
  271. BOOST_MATH_CHECK_THROW(quantile(boost::math::non_central_f_distribution<RealType>(1, 1, 1), 2), std::domain_error);
  272. } // template <class RealType>void test_spots(RealType)
  273. BOOST_AUTO_TEST_CASE( test_main )
  274. {
  275. BOOST_MATH_CONTROL_FP;
  276. // Basic sanity-check spot values.
  277. expected_results();
  278. // (Parameter value, arbitrarily zero, only communicates the floating point type).
  279. test_spots(0.0F); // Test float.
  280. test_spots(0.0); // Test double.
  281. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  282. test_spots(0.0L); // Test long double.
  283. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
  284. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  285. #endif
  286. #endif
  287. } // BOOST_AUTO_TEST_CASE( test_main )