/* test_chi_squared.cpp * * Copyright Steven Watanabe 2011 * Distributed under the Boost Software License, Version 1.0. (See * accompanying file LICENSE_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) * * $Id$ * */ #include #include #include #include #include #include #include #include #include #include #include "statistic_tests.hpp" bool do_test(double n, int max) { std::cout << "running chi_squared(" << n << ")" << " " << max << " times: " << std::flush; boost::math::chi_squared expected(n); boost::random::chi_squared_distribution<> dist(n); boost::mt19937 gen; kolmogorov_experiment test(max); boost::variate_generator > vgen(gen, dist); double prob = test.probability(test.run(vgen, expected)); bool result = prob < 0.99; const char* err = result? "" : "*"; std::cout << std::setprecision(17) << prob << err << std::endl; std::cout << std::setprecision(6); return result; } bool do_tests(int repeat, double max_n, int trials) { boost::mt19937 gen; boost::uniform_real<> ndist(0.00001, max_n); int errors = 0; for(int i = 0; i < repeat; ++i) { if(!do_test(ndist(gen), trials)) { ++errors; } } if(errors != 0) { std::cout << "*** " << errors << " errors detected ***" << std::endl; } return errors == 0; } int usage() { std::cerr << "Usage: test_chi_squared -r -n -t " << std::endl; return 2; } template bool handle_option(int& argc, char**& argv, char opt, T& value) { if(argv[0][1] == opt && argc > 1) { --argc; ++argv; value = boost::lexical_cast(argv[0]); return true; } else { return false; } } int main(int argc, char** argv) { int repeat = 10; double max_n = 1000.0; int trials = 1000000; if(argc > 0) { --argc; ++argv; } while(argc > 0) { if(argv[0][0] != '-') return usage(); else if(!handle_option(argc, argv, 'r', repeat) && !handle_option(argc, argv, 'n', max_n) && !handle_option(argc, argv, 't', trials)) { return usage(); } --argc; ++argv; } try { if(do_tests(repeat, max_n, trials)) { return 0; } else { return EXIT_FAILURE; } } catch(...) { std::cerr << boost::current_exception_diagnostic_information() << std::endl; return EXIT_FAILURE; } }