ljung_box.qbk 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. [/
  2. Copyright (c) 2019 Nick Thompson
  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. [section:ljung_box The Ljung-Box Test]
  8. [heading Synopsis]
  9. ```
  10. #include <boost/math/statistics/ljung_box.hpp>
  11. namespace boost::math::statistics {
  12. template<class RandomAccessIterator>
  13. std::pair<Real, Real> ljung_box(RandomAccessIterator begin, RandomAccessIterator end, int64_t lags = -1, int64_t fit_dof = 0);
  14. template<class RandomAccessContainer>
  15. auto ljung_box(RandomAccessContainer const & v, int64_t lags = -1, int64_t fit_dof = 0);
  16. }
  17. ```
  18. [heading Background]
  19. The Ljung-Box test is used to test if residuals from a fitted model have unwanted autocorrelation.
  20. If autocorrelation exists in the residuals, then presumably a model with more parameters can be fitted to the original data and explain more of the structure it contains.
  21. The test statistic is
  22. [$../graphs/ljung_box_definition.svg]
  23. where /n/ is the length of /v/ and \u2113 is the number of lags.
  24. The variance of the statistic slightly exceeds the variance of the chi squared distribution, but nonetheless it still is a fairly good test with reasonable computational cost.
  25. An example use is given below:
  26. ```
  27. #include <vector>
  28. #include <random>
  29. #include <iostream>
  30. #include <boost/math/statistics/ljung_box.hpp>
  31. using boost::math::statistics::ljung_box;
  32. std::random_device rd;
  33. std::normal_distribution<double> dis(0, 1);
  34. std::vector<double> v(8192);
  35. for (auto & x : v) { x = dis(rd); }
  36. auto [Q, p] = ljung_box(v);
  37. // Possible output: Q = 5.94734, p = 0.819668
  38. ```
  39. Now if the result is clearly autocorrelated:
  40. ```
  41. for (size_t i = 0; i < v.size(); ++i) { v[i] = i; }
  42. auto [Q, p] = ljung_box(v);
  43. // Possible output: Q = 81665.1, p = 0
  44. ```
  45. By default, the number of lags is taken to be the logarithm of the number of samples, so that the default complexity is [bigO](/n/ ln /n/).
  46. If you want to calculate a given number of lags, use the second argument:
  47. ```
  48. int64_t lags = 10;
  49. auto [Q, p] = ljung_box(v,10);
  50. ```
  51. Finally, it is sometimes relevant to specify how many degrees of freedom were used in creating the model from which the residuals were computed.
  52. This does not affect the test statistic /Q/, but only the /p/-value.
  53. If you need to specify the number of degrees of freedom, use
  54. ```
  55. int64_t fit_dof = 2;
  56. auto [Q, p] = ljung_box(v, -1, fit_dof);
  57. ```
  58. For example, if you fit your data with an ARIMA(/p/, /q/) model, then `fit_dof = p + q`.
  59. [endsect]
  60. [/section:ljung_box]