2 * Copyright Nick Thompson, 2019
3 * Use, modification and distribution are subject to the
4 * Boost Software License, Version 1.0. (See accompanying file
5 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #include "math_unit_test.hpp"
11 #include <boost/math/statistics/runs_test.hpp>
13 using boost::math::statistics::runs_above_and_below_median;
15 void test_agreement_with_r_randtests()
18 // install.packages("randtests")
20 // earthden <- c(5.36, 5.29, 5.58, 5.65, 5.57, 5.53, 5.62, 5.29, 5.44, 5.34, 5.79,5.10, 5.27, 5.39, 5.42, 5.47, 5.63, 5.34, 5.46, 5.30, 5.75, 5.68, 5.85)
21 // h = runs.test(earthden)
24 // -1.74772579501060576
26 // [1] 0.0805115199405023046
27 // median of v is 5.46, 23 elements.
28 std::vector<double> v{5.36, 5.29,
29 5.58, 5.65, 5.57, 5.53, 5.62,
38 // v -> {-,-,+,+,+,+,+,-,-,-,+,+,-,-,-,+,+,-,-,+,+,+}, 8 runs.
39 double expected_statistic = -1.74772579501060576;
40 double expected_pvalue = 0.0805115199405023046;
42 auto [computed_statistic, computed_pvalue] = runs_above_and_below_median(v);
44 CHECK_ULP_CLOSE(expected_statistic, computed_statistic, 3);
45 CHECK_ULP_CLOSE(expected_pvalue, computed_pvalue, 3);
48 void test_doc_example()
50 std::vector<double> v{5, 2, 0, 4, 7, 9, 10, 6, 1, 8, 3};
51 double expected_statistic = -0.670820393249936919;
52 double expected_pvalue = 0.502334954360502017;
54 auto [computed_statistic, computed_pvalue] = runs_above_and_below_median(v);
56 CHECK_ULP_CLOSE(expected_statistic, computed_statistic, 3);
57 CHECK_ULP_CLOSE(expected_pvalue, computed_pvalue, 3);
60 void test_constant_vector()
62 std::vector<double> v{5,5,5,5,5,5,5};
63 auto [computed_statistic, computed_pvalue] = runs_above_and_below_median(v);
64 double expected_pvalue = 0;
65 CHECK_ULP_CLOSE(expected_pvalue, computed_pvalue, 3);
66 if (!std::isnan(computed_statistic)) {
67 std::cerr << "Computed statistic is not a nan!\n";
73 test_constant_vector();
74 test_agreement_with_r_randtests();
76 return boost::math::test::report_errors();