ljung_box_test.cpp 1.4 KB

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