File: ljung_box.qbk

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (87 lines) | stat: -rw-r--r-- 2,577 bytes parent folder | download | duplicates (9)
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
78
79
80
81
82
83
84
85
86
87
[/
Copyright (c) 2019 Nick Thompson
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)
]

[section:ljung_box The Ljung-Box Test]

[heading Synopsis]

```
#include <boost/math/statistics/ljung_box.hpp>

namespace boost::math::statistics {

template<class RandomAccessIterator>
std::pair<Real, Real> ljung_box(RandomAccessIterator begin, RandomAccessIterator end, int64_t lags = -1, int64_t fit_dof = 0);


template<class RandomAccessContainer>
auto ljung_box(RandomAccessContainer const & v, int64_t lags = -1, int64_t fit_dof = 0);

}
```

[heading Background]

The Ljung-Box test is used to test if residuals from a fitted model have unwanted autocorrelation.
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.

The test statistic is

[$../graphs/ljung_box_definition.svg]

where /n/ is the length of /v/ and \u2113 is the number of lags.

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.

An example use is given below:


```
#include <vector>
#include <random>
#include <iostream>
#include <boost/math/statistics/ljung_box.hpp>
using boost::math::statistics::ljung_box;
std::random_device rd;
std::normal_distribution<double> dis(0, 1);
std::vector<double> v(8192);
for (auto & x : v) { x = dis(rd); }
auto [Q, p] = ljung_box(v);
// Possible output: Q = 5.94734, p = 0.819668
```

Now if the result is clearly autocorrelated:

```
for (size_t i = 0; i < v.size(); ++i) { v[i] = i; }
auto [Q, p] = ljung_box(v);
// Possible output: Q = 81665.1, p = 0
```

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/).
If you want to calculate a given number of lags, use the second argument:

```
int64_t lags = 10;
auto [Q, p] = ljung_box(v,10);
```

Finally, it is sometimes relevant to specify how many degrees of freedom were used in creating the model from which the residuals were computed.
This does not affect the test statistic /Q/, but only the /p/-value.
If you need to specify the number of degrees of freedom, use

```
int64_t fit_dof = 2;
auto [Q, p] = ljung_box(v, -1, fit_dof);
```

For example, if you fit your data with an ARIMA(/p/, /q/) model, then `fit_dof = p + q`.



[endsect]
[/section:ljung_box]