test_fisher_f.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. // test_fisher_squared.cpp
  2. // Copyright Paul A. Bristow 2006.
  3. // Copyright John Maddock 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)
  8. #include <boost/math/tools/test.hpp>
  9. #include <boost/math/concepts/real_concept.hpp> // for real_concept
  10. using ::boost::math::concepts::real_concept;
  11. #include <boost/math/distributions/fisher_f.hpp> // for fisher_f_distribution
  12. using boost::math::fisher_f_distribution;
  13. #define BOOST_TEST_MAIN
  14. #include <boost/test/unit_test.hpp> // for test_main
  15. #include <boost/test/tools/floating_point_comparison.hpp> // for BOOST_CHECK_CLOSE
  16. #include "test_out_of_range.hpp"
  17. #include <iostream>
  18. using std::cout;
  19. using std::endl;
  20. #include <limits>
  21. using std::numeric_limits;
  22. template <class RealType>
  23. RealType naive_pdf(RealType df1, RealType df2, RealType x)
  24. {
  25. //
  26. // Calculate the PDF naively using direct evaluation
  27. // of equation 2 from http://mathworld.wolfram.com/F-Distribution.html
  28. //
  29. // Our actual PDF implementation uses a completely different method,
  30. // so this is a good sanity check that our math is correct.
  31. //
  32. using namespace std; // For ADL of std functions.
  33. RealType e = boost::math::lgamma((df1 + df2) / 2);
  34. e += log(df1) * df1 / 2;
  35. e += log(df2) * df2 / 2;
  36. e += log(x) * ((df1 / 2) - 1);
  37. e -= boost::math::lgamma(df1 / 2);
  38. e -= boost::math::lgamma(df2 / 2);
  39. e -= log(df2 + x * df1) * (df1 + df2) / 2;
  40. return exp(e);
  41. }
  42. template <class RealType>
  43. void test_spot(
  44. RealType df1, // Degrees of freedom 1
  45. RealType df2, // Degrees of freedom 2
  46. RealType cs, // Chi Square statistic
  47. RealType P, // CDF
  48. RealType Q, // Complement of CDF
  49. RealType tol) // Test tolerance
  50. {
  51. boost::math::fisher_f_distribution<RealType> dist(df1, df2);
  52. BOOST_CHECK_CLOSE(
  53. cdf(dist, cs), P, tol);
  54. BOOST_CHECK_CLOSE(
  55. pdf(dist, cs), naive_pdf(dist.degrees_of_freedom1(), dist.degrees_of_freedom2(), cs), tol);
  56. if((P < 0.999) && (Q < 0.999))
  57. {
  58. //
  59. // We can only check this if P is not too close to 1,
  60. // so that we can guarantee Q is free of error:
  61. //
  62. BOOST_CHECK_CLOSE(
  63. cdf(complement(dist, cs)), Q, tol);
  64. BOOST_CHECK_CLOSE(
  65. quantile(dist, P), cs, tol);
  66. BOOST_CHECK_CLOSE(
  67. quantile(complement(dist, Q)), cs, tol);
  68. }
  69. }
  70. //
  71. // This test data is taken from the tables of upper
  72. // critical values of the F distribution available
  73. // at http://www.itl.nist.gov/div898/handbook/eda/section3/eda3673.htm
  74. //
  75. double q[] = { 0.10, 0.05, 0.025, 0.01, 0.001 };
  76. double upper_critical_values[][10] = {
  77. { 161.448,199.500,215.707,224.583,230.162,233.986,236.768,238.882,240.543,241.882 },
  78. { 18.513, 19.000, 19.164, 19.247, 19.296, 19.330, 19.353, 19.371, 19.385, 19.396 },
  79. { 10.128, 9.552, 9.277, 9.117, 9.013, 8.941, 8.887, 8.845, 8.812, 8.786 },
  80. { 7.709, 6.944, 6.591, 6.388, 6.256, 6.163, 6.094, 6.041, 5.999, 5.964 },
  81. { 6.608, 5.786, 5.409, 5.192, 5.050, 4.950, 4.876, 4.818, 4.772, 4.735 },
  82. { 5.987, 5.143, 4.757, 4.534, 4.387, 4.284, 4.207, 4.147, 4.099, 4.060 },
  83. { 5.591, 4.737, 4.347, 4.120, 3.972, 3.866, 3.787, 3.726, 3.677, 3.637 },
  84. { 5.318, 4.459, 4.066, 3.838, 3.687, 3.581, 3.500, 3.438, 3.388, 3.347 },
  85. { 5.117, 4.256, 3.863, 3.633, 3.482, 3.374, 3.293, 3.230, 3.179, 3.137 },
  86. { 4.965, 4.103, 3.708, 3.478, 3.326, 3.217, 3.135, 3.072, 3.020, 2.978 },
  87. { 4.844, 3.982, 3.587, 3.357, 3.204, 3.095, 3.012, 2.948, 2.896, 2.854 },
  88. { 4.747, 3.885, 3.490, 3.259, 3.106, 2.996, 2.913, 2.849, 2.796, 2.753 },
  89. { 4.667, 3.806, 3.411, 3.179, 3.025, 2.915, 2.832, 2.767, 2.714, 2.671 },
  90. { 4.600, 3.739, 3.344, 3.112, 2.958, 2.848, 2.764, 2.699, 2.646, 2.602 },
  91. { 4.543, 3.682, 3.287, 3.056, 2.901, 2.790, 2.707, 2.641, 2.588, 2.544 },
  92. { 4.494, 3.634, 3.239, 3.007, 2.852, 2.741, 2.657, 2.591, 2.538, 2.494 },
  93. { 4.451, 3.592, 3.197, 2.965, 2.810, 2.699, 2.614, 2.548, 2.494, 2.450 },
  94. { 4.414, 3.555, 3.160, 2.928, 2.773, 2.661, 2.577, 2.510, 2.456, 2.412 },
  95. { 4.381, 3.522, 3.127, 2.895, 2.740, 2.628, 2.544, 2.477, 2.423, 2.378 },
  96. { 4.351, 3.493, 3.098, 2.866, 2.711, 2.599, 2.514, 2.447, 2.393, 2.348 },
  97. { 4.325, 3.467, 3.072, 2.840, 2.685, 2.573, 2.488, 2.420, 2.366, 2.321 },
  98. { 4.301, 3.443, 3.049, 2.817, 2.661, 2.549, 2.464, 2.397, 2.342, 2.297 },
  99. { 4.279, 3.422, 3.028, 2.796, 2.640, 2.528, 2.442, 2.375, 2.320, 2.275 },
  100. { 4.260, 3.403, 3.009, 2.776, 2.621, 2.508, 2.423, 2.355, 2.300, 2.255 },
  101. { 4.242, 3.385, 2.991, 2.759, 2.603, 2.490, 2.405, 2.337, 2.282, 2.236 },
  102. { 4.225, 3.369, 2.975, 2.743, 2.587, 2.474, 2.388, 2.321, 2.265, 2.220 },
  103. { 4.210, 3.354, 2.960, 2.728, 2.572, 2.459, 2.373, 2.305, 2.250, 2.204 },
  104. { 4.196, 3.340, 2.947, 2.714, 2.558, 2.445, 2.359, 2.291, 2.236, 2.190 },
  105. { 4.183, 3.328, 2.934, 2.701, 2.545, 2.432, 2.346, 2.278, 2.223, 2.177 },
  106. { 4.171, 3.316, 2.922, 2.690, 2.534, 2.421, 2.334, 2.266, 2.211, 2.165 },
  107. { 4.160, 3.305, 2.911, 2.679, 2.523, 2.409, 2.323, 2.255, 2.199, 2.153 },
  108. { 4.149, 3.295, 2.901, 2.668, 2.512, 2.399, 2.313, 2.244, 2.189, 2.142 },
  109. { 4.139, 3.285, 2.892, 2.659, 2.503, 2.389, 2.303, 2.235, 2.179, 2.133 },
  110. { 4.130, 3.276, 2.883, 2.650, 2.494, 2.380, 2.294, 2.225, 2.170, 2.123 },
  111. { 4.121, 3.267, 2.874, 2.641, 2.485, 2.372, 2.285, 2.217, 2.161, 2.114 },
  112. { 4.113, 3.259, 2.866, 2.634, 2.477, 2.364, 2.277, 2.209, 2.153, 2.106 },
  113. { 4.105, 3.252, 2.859, 2.626, 2.470, 2.356, 2.270, 2.201, 2.145, 2.098 },
  114. { 4.098, 3.245, 2.852, 2.619, 2.463, 2.349, 2.262, 2.194, 2.138, 2.091 },
  115. { 4.091, 3.238, 2.845, 2.612, 2.456, 2.342, 2.255, 2.187, 2.131, 2.084 },
  116. { 4.085, 3.232, 2.839, 2.606, 2.449, 2.336, 2.249, 2.180, 2.124, 2.077 },
  117. { 4.079, 3.226, 2.833, 2.600, 2.443, 2.330, 2.243, 2.174, 2.118, 2.071 },
  118. { 4.073, 3.220, 2.827, 2.594, 2.438, 2.324, 2.237, 2.168, 2.112, 2.065 },
  119. { 4.067, 3.214, 2.822, 2.589, 2.432, 2.318, 2.232, 2.163, 2.106, 2.059 },
  120. { 4.062, 3.209, 2.816, 2.584, 2.427, 2.313, 2.226, 2.157, 2.101, 2.054 },
  121. { 4.057, 3.204, 2.812, 2.579, 2.422, 2.308, 2.221, 2.152, 2.096, 2.049 },
  122. { 4.052, 3.200, 2.807, 2.574, 2.417, 2.304, 2.216, 2.147, 2.091, 2.044 },
  123. { 4.047, 3.195, 2.802, 2.570, 2.413, 2.299, 2.212, 2.143, 2.086, 2.039 },
  124. { 4.043, 3.191, 2.798, 2.565, 2.409, 2.295, 2.207, 2.138, 2.082, 2.035 },
  125. { 4.038, 3.187, 2.794, 2.561, 2.404, 2.290, 2.203, 2.134, 2.077, 2.030 },
  126. { 4.034, 3.183, 2.790, 2.557, 2.400, 2.286, 2.199, 2.130, 2.073, 2.026 },
  127. { 4.030, 3.179, 2.786, 2.553, 2.397, 2.283, 2.195, 2.126, 2.069, 2.022 },
  128. { 4.027, 3.175, 2.783, 2.550, 2.393, 2.279, 2.192, 2.122, 2.066, 2.018 },
  129. { 4.023, 3.172, 2.779, 2.546, 2.389, 2.275, 2.188, 2.119, 2.062, 2.015 },
  130. { 4.020, 3.168, 2.776, 2.543, 2.386, 2.272, 2.185, 2.115, 2.059, 2.011 },
  131. { 4.016, 3.165, 2.773, 2.540, 2.383, 2.269, 2.181, 2.112, 2.055, 2.008 },
  132. { 4.013, 3.162, 2.769, 2.537, 2.380, 2.266, 2.178, 2.109, 2.052, 2.005 },
  133. { 4.010, 3.159, 2.766, 2.534, 2.377, 2.263, 2.175, 2.106, 2.049, 2.001 },
  134. { 4.007, 3.156, 2.764, 2.531, 2.374, 2.260, 2.172, 2.103, 2.046, 1.998 },
  135. { 4.004, 3.153, 2.761, 2.528, 2.371, 2.257, 2.169, 2.100, 2.043, 1.995 },
  136. { 4.001, 3.150, 2.758, 2.525, 2.368, 2.254, 2.167, 2.097, 2.040, 1.993 },
  137. { 3.998, 3.148, 2.755, 2.523, 2.366, 2.251, 2.164, 2.094, 2.037, 1.990 },
  138. { 3.996, 3.145, 2.753, 2.520, 2.363, 2.249, 2.161, 2.092, 2.035, 1.987 },
  139. { 3.993, 3.143, 2.751, 2.518, 2.361, 2.246, 2.159, 2.089, 2.032, 1.985 },
  140. { 3.991, 3.140, 2.748, 2.515, 2.358, 2.244, 2.156, 2.087, 2.030, 1.982 },
  141. { 3.989, 3.138, 2.746, 2.513, 2.356, 2.242, 2.154, 2.084, 2.027, 1.980 },
  142. { 3.986, 3.136, 2.744, 2.511, 2.354, 2.239, 2.152, 2.082, 2.025, 1.977 },
  143. { 3.984, 3.134, 2.742, 2.509, 2.352, 2.237, 2.150, 2.080, 2.023, 1.975 },
  144. { 3.982, 3.132, 2.740, 2.507, 2.350, 2.235, 2.148, 2.078, 2.021, 1.973 },
  145. { 3.980, 3.130, 2.737, 2.505, 2.348, 2.233, 2.145, 2.076, 2.019, 1.971 },
  146. { 3.978, 3.128, 2.736, 2.503, 2.346, 2.231, 2.143, 2.074, 2.017, 1.969 },
  147. { 3.976, 3.126, 2.734, 2.501, 2.344, 2.229, 2.142, 2.072, 2.015, 1.967 },
  148. { 3.974, 3.124, 2.732, 2.499, 2.342, 2.227, 2.140, 2.070, 2.013, 1.965 },
  149. { 3.972, 3.122, 2.730, 2.497, 2.340, 2.226, 2.138, 2.068, 2.011, 1.963 },
  150. { 3.970, 3.120, 2.728, 2.495, 2.338, 2.224, 2.136, 2.066, 2.009, 1.961 },
  151. { 3.968, 3.119, 2.727, 2.494, 2.337, 2.222, 2.134, 2.064, 2.007, 1.959 },
  152. { 3.967, 3.117, 2.725, 2.492, 2.335, 2.220, 2.133, 2.063, 2.006, 1.958 },
  153. { 3.965, 3.115, 2.723, 2.490, 2.333, 2.219, 2.131, 2.061, 2.004, 1.956 },
  154. { 3.963, 3.114, 2.722, 2.489, 2.332, 2.217, 2.129, 2.059, 2.002, 1.954 },
  155. { 3.962, 3.112, 2.720, 2.487, 2.330, 2.216, 2.128, 2.058, 2.001, 1.953 },
  156. { 3.960, 3.111, 2.719, 2.486, 2.329, 2.214, 2.126, 2.056, 1.999, 1.951 },
  157. { 3.959, 3.109, 2.717, 2.484, 2.327, 2.213, 2.125, 2.055, 1.998, 1.950 },
  158. { 3.957, 3.108, 2.716, 2.483, 2.326, 2.211, 2.123, 2.053, 1.996, 1.948 },
  159. { 3.956, 3.107, 2.715, 2.482, 2.324, 2.210, 2.122, 2.052, 1.995, 1.947 },
  160. { 3.955, 3.105, 2.713, 2.480, 2.323, 2.209, 2.121, 2.051, 1.993, 1.945 },
  161. { 3.953, 3.104, 2.712, 2.479, 2.322, 2.207, 2.119, 2.049, 1.992, 1.944 },
  162. { 3.952, 3.103, 2.711, 2.478, 2.321, 2.206, 2.118, 2.048, 1.991, 1.943 },
  163. { 3.951, 3.101, 2.709, 2.476, 2.319, 2.205, 2.117, 2.047, 1.989, 1.941 },
  164. { 3.949, 3.100, 2.708, 2.475, 2.318, 2.203, 2.115, 2.045, 1.988, 1.940 },
  165. { 3.948, 3.099, 2.707, 2.474, 2.317, 2.202, 2.114, 2.044, 1.987, 1.939 },
  166. { 3.947, 3.098, 2.706, 2.473, 2.316, 2.201, 2.113, 2.043, 1.986, 1.938 },
  167. { 3.946, 3.097, 2.705, 2.472, 2.315, 2.200, 2.112, 2.042, 1.984, 1.936 },
  168. { 3.945, 3.095, 2.704, 2.471, 2.313, 2.199, 2.111, 2.041, 1.983, 1.935 },
  169. { 3.943, 3.094, 2.703, 2.470, 2.312, 2.198, 2.110, 2.040, 1.982, 1.934 },
  170. { 3.942, 3.093, 2.701, 2.469, 2.311, 2.197, 2.109, 2.038, 1.981, 1.933 },
  171. { 3.941, 3.092, 2.700, 2.467, 2.310, 2.196, 2.108, 2.037, 1.980, 1.932 },
  172. { 3.940, 3.091, 2.699, 2.466, 2.309, 2.195, 2.106, 2.036, 1.979, 1.931 },
  173. { 3.939, 3.090, 2.698, 2.465, 2.308, 2.194, 2.105, 2.035, 1.978, 1.930 },
  174. { 3.938, 3.089, 2.697, 2.465, 2.307, 2.193, 2.104, 2.034, 1.977, 1.929 },
  175. { 3.937, 3.088, 2.696, 2.464, 2.306, 2.192, 2.103, 2.033, 1.976, 1.928 },
  176. { 3.936, 3.087, 2.696, 2.463, 2.305, 2.191, 2.103, 2.032, 1.975, 1.927 }
  177. };
  178. template <class RealType> // Any floating-point type RealType.
  179. void test_spots(RealType)
  180. {
  181. // Basic sanity checks, test data is to three decimal places only
  182. // so set tolerance to 0.002 expressed as a persentage. Note that
  183. // we can't even get full 3 digit accuracy since the data we're
  184. // using as input has *already been rounded*, leading to even
  185. // greater differences in output. As an accuracy test this is
  186. // pretty useless, but it is an excellent sanity check.
  187. RealType tolerance = 0.002f * 100;
  188. cout << "Tolerance = " << tolerance << "%." << endl;
  189. using boost::math::fisher_f_distribution;
  190. using ::boost::math::fisher_f;
  191. using ::boost::math::cdf;
  192. using ::boost::math::pdf;
  193. for(unsigned i = 0; i < sizeof(upper_critical_values) / sizeof(upper_critical_values[0]); ++i)
  194. {
  195. for(unsigned j = 0; j < sizeof(upper_critical_values[0])/sizeof(upper_critical_values[0][0]); ++j)
  196. {
  197. test_spot(
  198. static_cast<RealType>(j+1), // degrees of freedom 1
  199. static_cast<RealType>(i+1), // degrees of freedom 2
  200. static_cast<RealType>(upper_critical_values[i][j]), // test statistic F
  201. static_cast<RealType>(0.95), // Probability of result (CDF), P
  202. static_cast<RealType>(0.05), // Q = 1 - P
  203. tolerance);
  204. }
  205. }
  206. // http://www.vias.org/simulations/simusoft_distcalc.html
  207. // Distcalc version 1.2 Copyright 2002 H Lohninger, TU Wein
  208. // H.Lohninger: Teach/Me Data Analysis, Springer-Verlag, Berlin-New York-Tokyo, 1999. ISBN 3-540-14743-8
  209. // The Windows calculator is available zipped distcalc.exe for download at:
  210. // http://www.vias.org/simulations/simu_stat.html
  211. // This interactive Windows program was used to find some combination for which the
  212. // result appears to be exact. No doubt this can be done analytically too,
  213. // by mathematicians!
  214. // Some combinations for which the result is 'exact', or at least is to 40 decimal digits.
  215. // 40 decimal digits includes 128-bit significand User Defined Floating-Point types.
  216. // These all pass tests at near epsilon accuracy for the floating-point type.
  217. tolerance = boost::math::tools::epsilon<RealType>() * 5 * 100;
  218. cout << "Tolerance = " << tolerance << "%." << endl;
  219. BOOST_CHECK_CLOSE(
  220. cdf(fisher_f_distribution<RealType>(
  221. static_cast<RealType>(1.), // df1
  222. static_cast<RealType>(2.)), // df2
  223. static_cast<RealType>(2.)/static_cast<RealType>(3.) ), // F
  224. static_cast<RealType>(0.5), // probability.
  225. tolerance);
  226. BOOST_CHECK_CLOSE(
  227. cdf(complement(fisher_f_distribution<RealType>(
  228. static_cast<RealType>(1.), // df1
  229. static_cast<RealType>(2.)), // df2
  230. static_cast<RealType>(1.6L))), // F
  231. static_cast<RealType>(0.333333333333333333333333333333333333L), // probability.
  232. tolerance * 100); // needs higher tolerance at 128-bit precision - value not exact?
  233. BOOST_CHECK_CLOSE(
  234. cdf(complement(fisher_f_distribution<RealType>(
  235. static_cast<RealType>(1.), // df1
  236. static_cast<RealType>(2.)), // df2
  237. static_cast<RealType>(6.5333333333333333333333333333333333L))), // F
  238. static_cast<RealType>(0.125L), // probability.
  239. tolerance);
  240. BOOST_CHECK_CLOSE(
  241. cdf(complement(fisher_f_distribution<RealType>(
  242. static_cast<RealType>(2.), // df1
  243. static_cast<RealType>(2.)), // df2
  244. static_cast<RealType>(1.))), // F
  245. static_cast<RealType>(0.5L), // probability.
  246. tolerance);
  247. BOOST_CHECK_CLOSE(
  248. cdf(complement(fisher_f_distribution<RealType>(
  249. static_cast<RealType>(2.), // df1
  250. static_cast<RealType>(2.)), // df2
  251. static_cast<RealType>(3.))), // F
  252. static_cast<RealType>(0.25L), // probability.
  253. tolerance);
  254. BOOST_CHECK_CLOSE(
  255. cdf(complement(fisher_f_distribution<RealType>(
  256. static_cast<RealType>(2.), // df1
  257. static_cast<RealType>(2.)), // df2
  258. static_cast<RealType>(3.))), // F
  259. static_cast<RealType>(0.25L), // probability.
  260. tolerance);
  261. BOOST_CHECK_CLOSE(
  262. cdf(complement(fisher_f_distribution<RealType>(
  263. static_cast<RealType>(2.), // df1
  264. static_cast<RealType>(2.)), // df2
  265. static_cast<RealType>(7.))), // F
  266. static_cast<RealType>(0.125L), // probability.
  267. tolerance);
  268. BOOST_CHECK_CLOSE(
  269. cdf(complement(fisher_f_distribution<RealType>(
  270. static_cast<RealType>(2.), // df1
  271. static_cast<RealType>(2.)), // df2
  272. static_cast<RealType>(9.))), // F
  273. static_cast<RealType>(0.1L), // probability.
  274. tolerance);
  275. BOOST_CHECK_CLOSE(
  276. cdf(complement(fisher_f_distribution<RealType>(
  277. static_cast<RealType>(2.), // df1
  278. static_cast<RealType>(2.)), // df2
  279. static_cast<RealType>(19.))), // F
  280. static_cast<RealType>(0.05L), // probability.
  281. tolerance);
  282. BOOST_CHECK_CLOSE(
  283. cdf(complement(fisher_f_distribution<RealType>(
  284. static_cast<RealType>(2.), // df1
  285. static_cast<RealType>(2.)), // df2
  286. static_cast<RealType>(29.))), // F
  287. static_cast<RealType>(0.03333333333333333333333333333333333333333L), // probability.
  288. tolerance);
  289. BOOST_CHECK_CLOSE(
  290. cdf(complement(fisher_f_distribution<RealType>(
  291. static_cast<RealType>(2.), // df1
  292. static_cast<RealType>(2.)), // df2
  293. static_cast<RealType>(99.))), // F
  294. static_cast<RealType>(0.01L), // probability.
  295. tolerance);
  296. BOOST_CHECK_CLOSE(
  297. cdf(complement(fisher_f_distribution<RealType>(
  298. static_cast<RealType>(4.), // df1
  299. static_cast<RealType>(4.)), // df2
  300. static_cast<RealType>(9.))), // F
  301. static_cast<RealType>(0.028L), // probability.
  302. tolerance*10); // not quite exact???
  303. BOOST_CHECK_CLOSE(
  304. cdf(complement(fisher_f_distribution<RealType>(
  305. static_cast<RealType>(8.), // df1
  306. static_cast<RealType>(8.)), // df2
  307. static_cast<RealType>(1.))), // F
  308. static_cast<RealType>(0.5L), // probability.
  309. tolerance);
  310. // Inverse tests
  311. BOOST_CHECK_CLOSE(
  312. quantile(complement(fisher_f_distribution<RealType>(
  313. static_cast<RealType>(2.), // df1
  314. static_cast<RealType>(2.)), // df2
  315. static_cast<RealType>(0.03333333333333333333333333333333333333333L))), // probability
  316. static_cast<RealType>(29.), // F expected.
  317. tolerance*10);
  318. BOOST_CHECK_CLOSE(
  319. quantile(fisher_f_distribution<RealType>(
  320. static_cast<RealType>(2.), // df1
  321. static_cast<RealType>(2.)), // df2
  322. static_cast<RealType>(1.0L - 0.03333333333333333333333333333333333333333L)), // probability
  323. static_cast<RealType>(29.), // F expected.
  324. tolerance*10);
  325. // Also note limit cases for F(1, infinity) == normal distribution
  326. // F(1, n2) == Student's t distribution
  327. // F(n1, infinity) == Chisq distribution
  328. // These might allow some further cross checks?
  329. RealType tol2 = boost::math::tools::epsilon<RealType>() * 5 * 100; // 5 eps as a percent
  330. cout << "Tolerance = " << tol2 << "%." << endl;
  331. fisher_f_distribution<RealType> dist(static_cast<RealType>(8), static_cast<RealType>(6));
  332. RealType x = 7;
  333. using namespace std; // ADL of std names.
  334. // mean:
  335. BOOST_CHECK_CLOSE(
  336. mean(dist)
  337. , static_cast<RealType>(6)/static_cast<RealType>(4), tol2);
  338. // variance:
  339. BOOST_CHECK_CLOSE(
  340. variance(dist)
  341. , static_cast<RealType>(2 * 6 * 6 * (8 + 6 - 2)) / static_cast<RealType>(8 * 16 * 2), tol2);
  342. // std deviation:
  343. BOOST_CHECK_CLOSE(
  344. standard_deviation(dist)
  345. , sqrt(static_cast<RealType>(2 * 6 * 6 * (8 + 6 - 2)) / static_cast<RealType>(8 * 16 * 2)), tol2);
  346. // hazard:
  347. BOOST_CHECK_CLOSE(
  348. hazard(dist, x)
  349. , pdf(dist, x) / cdf(complement(dist, x)), tol2);
  350. // cumulative hazard:
  351. BOOST_CHECK_CLOSE(
  352. chf(dist, x)
  353. , -log(cdf(complement(dist, x))), tol2);
  354. // coefficient_of_variation:
  355. BOOST_CHECK_CLOSE(
  356. coefficient_of_variation(dist)
  357. , standard_deviation(dist) / mean(dist), tol2);
  358. BOOST_CHECK_CLOSE(
  359. mode(dist)
  360. , static_cast<RealType>(6*6)/static_cast<RealType>(8*8), tol2);
  361. fisher_f_distribution<RealType> dist2(static_cast<RealType>(8), static_cast<RealType>(12));
  362. BOOST_CHECK_CLOSE(
  363. skewness(dist2)
  364. , static_cast<RealType>(26 * sqrt(64.0L)) / (12*6), tol2);
  365. BOOST_CHECK_CLOSE(
  366. kurtosis_excess(dist2)
  367. , static_cast<RealType>(6272) * 12 / 3456, tol2);
  368. BOOST_CHECK_CLOSE(
  369. kurtosis(dist2)
  370. , static_cast<RealType>(6272) * 12 / 3456 + 3, tol2);
  371. // special cases:
  372. BOOST_MATH_CHECK_THROW(
  373. pdf(
  374. fisher_f_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1)),
  375. static_cast<RealType>(0)), std::overflow_error
  376. );
  377. BOOST_CHECK_EQUAL(
  378. pdf(fisher_f_distribution<RealType>(2, 2), static_cast<RealType>(0))
  379. , static_cast<RealType>(1.0f));
  380. BOOST_CHECK_EQUAL(
  381. pdf(fisher_f_distribution<RealType>(3, 3), static_cast<RealType>(0))
  382. , static_cast<RealType>(0.0f));
  383. BOOST_CHECK_EQUAL(
  384. cdf(fisher_f_distribution<RealType>(1, 1), static_cast<RealType>(0))
  385. , static_cast<RealType>(0.0f));
  386. BOOST_CHECK_EQUAL(
  387. cdf(fisher_f_distribution<RealType>(2, 2), static_cast<RealType>(0))
  388. , static_cast<RealType>(0.0f));
  389. BOOST_CHECK_EQUAL(
  390. cdf(fisher_f_distribution<RealType>(3, 3), static_cast<RealType>(0))
  391. , static_cast<RealType>(0.0f));
  392. BOOST_CHECK_EQUAL(
  393. cdf(complement(fisher_f_distribution<RealType>(1, 1), static_cast<RealType>(0)))
  394. , static_cast<RealType>(1));
  395. BOOST_CHECK_EQUAL(
  396. cdf(complement(fisher_f_distribution<RealType>(2, 2), static_cast<RealType>(0)))
  397. , static_cast<RealType>(1));
  398. BOOST_CHECK_EQUAL(
  399. cdf(complement(fisher_f_distribution<RealType>(3, 3), static_cast<RealType>(0)))
  400. , static_cast<RealType>(1));
  401. BOOST_MATH_CHECK_THROW(
  402. pdf(
  403. fisher_f_distribution<RealType>(-1, 2),
  404. static_cast<RealType>(1)), std::domain_error
  405. );
  406. BOOST_MATH_CHECK_THROW(
  407. pdf(
  408. fisher_f_distribution<RealType>(1, -1),
  409. static_cast<RealType>(1)), std::domain_error
  410. );
  411. BOOST_MATH_CHECK_THROW(
  412. pdf(
  413. fisher_f_distribution<RealType>(8, 2),
  414. static_cast<RealType>(-1)), std::domain_error
  415. );
  416. BOOST_MATH_CHECK_THROW(
  417. cdf(
  418. fisher_f_distribution<RealType>(-1, 1),
  419. static_cast<RealType>(1)), std::domain_error
  420. );
  421. BOOST_MATH_CHECK_THROW(
  422. cdf(
  423. fisher_f_distribution<RealType>(8, 4),
  424. static_cast<RealType>(-1)), std::domain_error
  425. );
  426. BOOST_MATH_CHECK_THROW(
  427. cdf(complement(
  428. fisher_f_distribution<RealType>(-1, 2),
  429. static_cast<RealType>(1))), std::domain_error
  430. );
  431. BOOST_MATH_CHECK_THROW(
  432. cdf(complement(
  433. fisher_f_distribution<RealType>(8, 4),
  434. static_cast<RealType>(-1))), std::domain_error
  435. );
  436. BOOST_MATH_CHECK_THROW(
  437. quantile(
  438. fisher_f_distribution<RealType>(-1, 2),
  439. static_cast<RealType>(0.5)), std::domain_error
  440. );
  441. BOOST_MATH_CHECK_THROW(
  442. quantile(
  443. fisher_f_distribution<RealType>(8, 8),
  444. static_cast<RealType>(-1)), std::domain_error
  445. );
  446. BOOST_MATH_CHECK_THROW(
  447. quantile(
  448. fisher_f_distribution<RealType>(8, 8),
  449. static_cast<RealType>(1.1)), std::domain_error
  450. );
  451. BOOST_MATH_CHECK_THROW(
  452. quantile(complement(
  453. fisher_f_distribution<RealType>(2, -1),
  454. static_cast<RealType>(0.5))), std::domain_error
  455. );
  456. BOOST_MATH_CHECK_THROW(
  457. quantile(complement(
  458. fisher_f_distribution<RealType>(8, 8),
  459. static_cast<RealType>(-1))), std::domain_error
  460. );
  461. BOOST_MATH_CHECK_THROW(
  462. quantile(complement(
  463. fisher_f_distribution<RealType>(8, 8),
  464. static_cast<RealType>(1.1))), std::domain_error
  465. );
  466. check_out_of_range<fisher_f_distribution<RealType> >(2, 3);
  467. } // template <class RealType>void test_spots(RealType)
  468. BOOST_AUTO_TEST_CASE( test_main )
  469. {
  470. // Check that can generate fisher distribution using the two convenience methods:
  471. boost::math::fisher_f myf1(1., 2); // Using typedef
  472. fisher_f_distribution<> myf2(1., 2); // Using default RealType double.
  473. // Basic sanity-check spot values.
  474. // (Parameter value, arbitrarily zero, only communicates the floating point type).
  475. test_spots(0.0F); // Test float.
  476. test_spots(0.0); // Test double.
  477. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  478. test_spots(0.0L); // Test long double.
  479. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
  480. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  481. #endif
  482. #endif
  483. } // BOOST_AUTO_TEST_CASE( test_main )
  484. /*
  485. Output is:
  486. Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_fisher.exe"
  487. Running 1 test case...
  488. Tolerance = 0.2%.
  489. Tolerance = 5.96046e-005%.
  490. Tolerance = 5.96046e-005%.
  491. Tolerance = 0.2%.
  492. Tolerance = 1.11022e-013%.
  493. Tolerance = 1.11022e-013%.
  494. Tolerance = 0.2%.
  495. Tolerance = 1.11022e-013%.
  496. Tolerance = 1.11022e-013%.
  497. Tolerance = 0.2%.
  498. Tolerance = 1.11022e-013%.
  499. Tolerance = 1.11022e-013%.
  500. *** No errors detected
  501. */