Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / math / test / ljung_box_test.cpp
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 <numeric>
10 #include <utility>
11 #include <random>
12 #include <boost/math/statistics/ljung_box.hpp>
13
14 using boost::math::statistics::ljung_box;
15
16 template<class Real>
17 void test_trivial()
18 {
19
20   // Validate in R:
21   // > v <- c(1,2)
22   // > Box.test(v, lag=1, "Ljung")
23   //    Box-Ljung test
24   // data:  v
25   // X-squared = 2, df = 1, p-value = 0.1573
26
27   std::vector<Real> v{1,2};
28
29   double expected_statistic = 2;
30   double expected_pvalue = 0.15729920705028455;
31
32   auto [computed_statistic, computed_pvalue] = ljung_box(v, 1);
33   CHECK_ULP_CLOSE(expected_statistic, computed_statistic, 10);
34   CHECK_ULP_CLOSE(expected_pvalue, computed_pvalue, 30);
35 }
36
37
38 void test_agreement_with_mathematica()
39 {
40   std::vector<double> v{0.7739928761039216,-0.4468259278452086,0.98287381303903,-0.3943029116201079,0.6569015496559457};
41   double expected_statistic = 10.2076093223439;
42   double expected_pvalue = 0.00607359458123835072;
43
44   auto [computed_statistic, computed_pvalue] = ljung_box(v);
45   CHECK_ULP_CLOSE(expected_statistic, computed_statistic, 3);
46   CHECK_ULP_CLOSE(expected_pvalue, computed_pvalue, 3);
47 }
48
49 int main()
50 {
51     test_trivial<double>();
52     test_agreement_with_mathematica();
53     return boost::math::test::report_errors();
54 }