File: test_runs_test.cpp

package info (click to toggle)
scipy 1.16.0-1exp7
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 234,820 kB
  • sloc: cpp: 503,145; python: 344,611; ansic: 195,638; javascript: 89,566; fortran: 56,210; cs: 3,081; f90: 1,150; sh: 848; makefile: 785; pascal: 284; csh: 135; lisp: 134; xml: 56; perl: 51
file content (77 lines) | stat: -rw-r--r-- 2,521 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * Copyright Nick Thompson, 2019
 * Use, modification and distribution are subject to 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)
 */

#include "math_unit_test.hpp"
#include <vector>
#include <random>
#include <boost/math/statistics/runs_test.hpp>

using boost::math::statistics::runs_above_and_below_median;

void test_agreement_with_r_randtests()
{
  // $R
  // install.packages("randtests")
  // library(randtests)
  // 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)
  // h = runs.test(earthden)
  // options(digits=18)
  //> h$statistic
  // -1.74772579501060576
  // > h$p.value
  // [1] 0.0805115199405023046
  // median of v is 5.46, 23 elements.
  std::vector<double> v{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, /* median */
                        5.30,
                        5.75, 5.68, 5.85};
  // v -> {-,-,+,+,+,+,+,-,-,-,+,+,-,-,-,+,+,-,-,+,+,+}, 8 runs.
  double expected_statistic = -1.74772579501060576;
  double expected_pvalue = 0.0805115199405023046;

  auto [computed_statistic, computed_pvalue] = runs_above_and_below_median(v);

  CHECK_ULP_CLOSE(expected_statistic, computed_statistic, 3);
  CHECK_ULP_CLOSE(expected_pvalue, computed_pvalue, 3);
}

void test_doc_example()
{
    std::vector<double> v{5, 2, 0, 4, 7, 9, 10, 6, 1, 8, 3};
    double expected_statistic = -0.670820393249936919;
    double expected_pvalue = 0.502334954360502017;

    auto [computed_statistic, computed_pvalue] = runs_above_and_below_median(v);

    CHECK_ULP_CLOSE(expected_statistic, computed_statistic, 3);
    CHECK_ULP_CLOSE(expected_pvalue, computed_pvalue, 3);
}

void test_constant_vector()
{
    std::vector<double> v{5,5,5,5,5,5,5};
    auto [computed_statistic, computed_pvalue] = runs_above_and_below_median(v);
    double expected_pvalue = 0;
    CHECK_ULP_CLOSE(expected_pvalue, computed_pvalue, 3);
    if (!std::isnan(computed_statistic)) {
        std::cerr << "Computed statistic is not a nan!\n";
    }
}

int main()
{
    test_constant_vector();
    test_agreement_with_r_randtests();
    test_doc_example();
    return boost::math::test::report_errors();
}