3 // Copyright John Maddock 2008.
5 // Use, modification and distribution are subject to the
6 // Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt
8 // or copy at http://www.boost.org/LICENSE_1_0.txt)
13 #pragma warning (disable:4127 4512)
16 #if !defined(TEST_FLOAT) && !defined(TEST_DOUBLE) && !defined(TEST_LDOUBLE) && !defined(TEST_REAL_CONCEPT)
20 # define TEST_REAL_CONCEPT
23 #include <boost/math/concepts/real_concept.hpp> // for real_concept
24 #include <boost/math/distributions/non_central_f.hpp> // for chi_squared_distribution
25 #define BOOST_TEST_MAIN
26 #include <boost/test/unit_test.hpp> // for test_main
27 #include <boost/test/results_collector.hpp>
28 #include <boost/test/unit_test.hpp>
29 #include <boost/test/floating_point_comparison.hpp> // for BOOST_CHECK_CLOSE
30 #include "test_out_of_range.hpp"
32 #include "functor.hpp"
33 #include "handle_test_result.hpp"
40 using std::numeric_limits;
42 #define BOOST_CHECK_CLOSE_EX(a, b, prec, i) \
44 unsigned int failures = boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed;\
45 BOOST_CHECK_CLOSE(a, b, prec); \
46 if(failures != boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed)\
48 std::cerr << "Failure was at row " << i << std::endl;\
49 std::cerr << std::setprecision(35); \
50 std::cerr << "{ " << data[i][0] << " , " << data[i][1] << " , " << data[i][2];\
51 std::cerr << " , " << data[i][3] << " , " << data[i][4] << " } " << std::endl;\
55 #define BOOST_CHECK_EX(a, i) \
57 unsigned int failures = boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed;\
59 if(failures != boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed)\
61 std::cerr << "Failure was at row " << i << std::endl;\
62 std::cerr << std::setprecision(35); \
63 std::cerr << "{ " << data[i][0] << " , " << data[i][1] << " , " << data[i][2];\
64 std::cerr << " , " << data[i][3] << " , " << data[i][4] << " } " << std::endl;\
68 void expected_results()
71 // Define the max and mean errors expected for
72 // various compilers and platforms.
74 const char* largest_type;
75 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
76 if(boost::math::policies::digits<double, boost::math::policies::policy<> >() == boost::math::policies::digits<long double, boost::math::policies::policy<> >())
78 largest_type = "(long\\s+)?double|real_concept";
82 largest_type = "long double|real_concept";
85 largest_type = "(long\\s+)?double|real_concept";
89 // Finish off by printing out the compiler/stdlib/platform names,
90 // we do this to make it easier to mark up expected error rates.
92 std::cout << "Tests run with " << BOOST_COMPILER << ", "
93 << BOOST_STDLIB << ", " << BOOST_PLATFORM << std::endl;
96 template <class RealType>
97 RealType naive_pdf(RealType v1, RealType v2, RealType l, RealType f)
101 for(int i = 0; ; ++i)
103 RealType term = -l/2 + log(l/2) * i;
104 term += boost::math::lgamma(v2/2 + v1/2+i) - (boost::math::lgamma(v2/2) + boost::math::lgamma(v1/2+i));
105 term -= boost::math::lgamma(RealType(i+1));
106 term += log(v1/v2) * (v1/2+i) + log(v2 / (v2 + v1 * f)) * ((v1 + v2) / 2 + i);
107 term += log(f) * (v1/2 - 1 + i);
110 if((term/sum < boost::math::tools::epsilon<RealType>()) || (term == 0))
116 template <class RealType>
120 RealType ncp, // non-centrality param
121 RealType x, // F statistic
123 RealType Q, // Complement of CDF
125 RealType tol) // Test tolerance
127 boost::math::non_central_f_distribution<RealType> dist(a, b, ncp);
129 cdf(dist, x), P, tol);
131 pdf(dist, x), D, tol);
132 if(boost::math::tools::digits<RealType>() > 50)
135 // The "naive" pdf calculation fails at float precision.
138 pdf(dist, x), naive_pdf(a, b, ncp, x), tol);
141 if((P < 0.99) && (Q < 0.99))
144 // We can only check this if P is not too close to 1,
145 // so that we can guarentee Q is reasonably free of error:
148 cdf(complement(dist, x)), Q, tol);
150 quantile(dist, P), x, tol * 10);
152 quantile(complement(dist, Q)), x, tol * 10);
154 if(boost::math::tools::digits<RealType>() > 50)
157 // Sanity check mode:
159 RealType m = mode(dist);
160 RealType p = pdf(dist, m);
161 BOOST_CHECK(pdf(dist, m * (1 + sqrt(tol) * 10)) <= p);
162 BOOST_CHECK(pdf(dist, m * (1 - sqrt(tol)) * 10) <= p);
166 template <class RealType> // Any floating-point type RealType.
167 void test_spots(RealType)
169 RealType tolerance = boost::math::tools::epsilon<RealType>() * 10000;
171 cout << "Tolerance = " << (tolerance / 100) << "%." << endl;
174 // Spot tests from Mathematica computed values:
177 RealType(5), // alpha
179 RealType(1), // non-centrality param
180 RealType(1.5), // F statistic
181 RealType(0.49845842011686358665786775091245664L), // CDF
182 RealType(1 - 0.49845842011686358665786775091245664L), // Complement of CDF
183 RealType(0.20251311620629730205859816288225385L), // PDF
184 RealType(tolerance));
187 RealType(2), // alpha
189 RealType(1), // non-centrality param
190 RealType(2), // F statistic
191 RealType(0.64938711196845800322066756609406894L), // CDF
192 RealType(1 - 0.64938711196845800322066756609406894L), // Complement of CDF
193 RealType(0.15512617916132011524583796078456003L), // PDF
194 RealType(tolerance));
196 RealType(100), // alpha
198 RealType(15), // non-centrality param
199 RealType(105), // F statistic
200 RealType(0.99996207325249555786258005958906310L), // CDF
201 RealType(0.000037926747504442137419940410936905407L), // Complement of CDF
202 RealType(8.9562292619539161551049126260104435e-7), // PDF
203 RealType(tolerance * 10));
205 RealType(100), // alpha
207 RealType(15), // non-centrality param
208 RealType(1.5), // F statistic
209 RealType(0.57592315596686179870591317303126895L), // CDF
210 RealType(1 - 0.57592315596686179870591317303126895L), // Complement of CDF
211 RealType(0.36743745541686900593212039288061162L), // PDF
212 RealType(tolerance * 5));
214 RealType(5), // alpha
215 RealType(100), // beta
216 RealType(102), // non-centrality param
217 RealType(25), // F statistic
218 RealType(0.74993383259829917593356265102363267L), // CDF
219 RealType(1 - 0.74993383259829917593356265102363267L), // Complement of CDF
220 RealType(0.054467600423154020554785779421659007L), // PDF
221 RealType(tolerance * 5));
223 RealType(85), // alpha
224 RealType(100), // beta
225 RealType(0.5), // non-centrality param
226 RealType(1.25), // F statistic
227 RealType(0.85228624977948142884820398473385455L), // CDF
228 RealType(0.14771375022051857115179601526614545L), // Complement of CDF
229 RealType(0.88510283331631848299643323511414868L), // PDF
230 RealType(tolerance * 5));
233 // Spot tests use values computed by the R statistical
234 // package and the pbeta and dbeta functions:
236 tolerance = (std::max)(
237 boost::math::tools::epsilon<RealType>() * 100,
238 (RealType)1e-6) * 100;
241 RealType(85), // alpha
242 RealType(100), // beta
243 RealType(245), // non-centrality param
244 RealType(3.5), // F statistic
245 RealType(0.2697244), // CDF
246 RealType(1 - 0.2697244), // Complement of CDF
247 RealType(0.54352369104452836465948073900030320L), // PDF
248 RealType(tolerance));
253 // 5 eps expressed as a persentage, otherwise the limit of the test data:
255 RealType tol2 = (std::max)(boost::math::tools::epsilon<RealType>() * 500, RealType(1e-25));
258 boost::math::non_central_f_distribution<RealType> dist(20, 15, 30);
262 , static_cast<RealType>(2.8846153846153846153846153846154L), tol2);
266 , static_cast<RealType>(2.1422807961269499731038192576654L), tol2);
269 standard_deviation(dist)
270 , sqrt(variance(dist)), tol2);
274 , pdf(dist, x) / cdf(complement(dist, x)), tol2);
275 // cumulative hazard:
278 , -log(cdf(complement(dist, x))), tol2);
279 // coefficient_of_variation:
281 coefficient_of_variation(dist)
282 , standard_deviation(dist) / mean(dist), tol2);
287 static_cast<RealType>(0.5)), static_cast<RealType>(tol2));
291 , static_cast<RealType>(2.070019130232759428074835788815387743293972985542L), sqrt(tolerance));
295 , static_cast<RealType>(2.1011821125804540669752380228510691320707051692719L), tol2);
299 , 3 + kurtosis_excess(dist), tol2);
302 kurtosis_excess(dist)
303 , static_cast<RealType>(13.225781681053154767604638331440974359675882226873L), tol2);
305 // Error handling checks:
306 check_out_of_range<boost::math::non_central_f_distribution<RealType> >(1, 1, 1);
307 BOOST_CHECK_THROW(pdf(boost::math::non_central_f_distribution<RealType>(0, 1, 1), 0), std::domain_error);
308 BOOST_CHECK_THROW(pdf(boost::math::non_central_f_distribution<RealType>(-1, 1, 1), 0), std::domain_error);
309 BOOST_CHECK_THROW(pdf(boost::math::non_central_f_distribution<RealType>(1, 0, 1), 0), std::domain_error);
310 BOOST_CHECK_THROW(pdf(boost::math::non_central_f_distribution<RealType>(1, -1, 1), 0), std::domain_error);
311 BOOST_CHECK_THROW(quantile(boost::math::non_central_f_distribution<RealType>(1, 1, 1), -1), std::domain_error);
312 BOOST_CHECK_THROW(quantile(boost::math::non_central_f_distribution<RealType>(1, 1, 1), 2), std::domain_error);
313 } // template <class RealType>void test_spots(RealType)
315 BOOST_AUTO_TEST_CASE( test_main )
317 BOOST_MATH_CONTROL_FP;
318 // Basic sanity-check spot values.
320 // (Parameter value, arbitrarily zero, only communicates the floating point type).
321 test_spots(0.0F); // Test float.
322 test_spots(0.0); // Test double.
323 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
324 test_spots(0.0L); // Test long double.
325 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
326 test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
331 } // BOOST_AUTO_TEST_CASE( test_main )