]>
Commit | Line | Data |
---|---|---|
92f5a8d4 TL |
1 | /* |
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) | |
6 | */ | |
7 | ||
8 | #include "math_unit_test.hpp" | |
9 | #include <vector> | |
10 | #include <random> | |
11 | #include <boost/math/statistics/runs_test.hpp> | |
12 | ||
13 | using boost::math::statistics::runs_above_and_below_median; | |
14 | ||
15 | void test_agreement_with_r_randtests() | |
16 | { | |
17 | // $R | |
18 | // install.packages("randtests") | |
19 | // library(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) | |
22 | // options(digits=18) | |
23 | //> h$statistic | |
24 | // -1.74772579501060576 | |
25 | // > h$p.value | |
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, | |
30 | 5.29, 5.44, 5.34, | |
31 | 5.79, 5.10, | |
32 | 5.27, 5.39, 5.42, | |
33 | 5.47, 5.63, | |
34 | 5.34, | |
35 | 5.46, /* median */ | |
36 | 5.30, | |
37 | 5.75, 5.68, 5.85}; | |
38 | // v -> {-,-,+,+,+,+,+,-,-,-,+,+,-,-,-,+,+,-,-,+,+,+}, 8 runs. | |
39 | double expected_statistic = -1.74772579501060576; | |
40 | double expected_pvalue = 0.0805115199405023046; | |
41 | ||
42 | auto [computed_statistic, computed_pvalue] = runs_above_and_below_median(v); | |
43 | ||
44 | CHECK_ULP_CLOSE(expected_statistic, computed_statistic, 3); | |
45 | CHECK_ULP_CLOSE(expected_pvalue, computed_pvalue, 3); | |
46 | } | |
47 | ||
48 | void test_doc_example() | |
49 | { | |
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; | |
53 | ||
54 | auto [computed_statistic, computed_pvalue] = runs_above_and_below_median(v); | |
55 | ||
56 | CHECK_ULP_CLOSE(expected_statistic, computed_statistic, 3); | |
57 | CHECK_ULP_CLOSE(expected_pvalue, computed_pvalue, 3); | |
58 | } | |
59 | ||
60 | void test_constant_vector() | |
61 | { | |
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"; | |
68 | } | |
69 | } | |
70 | ||
71 | int main() | |
72 | { | |
73 | test_constant_vector(); | |
74 | test_agreement_with_r_randtests(); | |
75 | test_doc_example(); | |
76 | return boost::math::test::report_errors(); | |
77 | } |