72e469da0a
[CI SKIP]
135 lines
14 KiB
HTML
135 lines
14 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
|
|
<title>The Ljung-Box Test</title>
|
|
<link rel="stylesheet" href="../math.css" type="text/css">
|
|
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
|
|
<link rel="home" href="../index.html" title="Math Toolkit 2.11.0">
|
|
<link rel="up" href="../statistics.html" title="Chapter 6. Statistics">
|
|
<link rel="prev" href="runs_test.html" title="Runs tests">
|
|
<link rel="next" href="../vector_functionals.html" title="Chapter 7. Vector Functionals - Norms">
|
|
</head>
|
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
|
<table cellpadding="2" width="100%"><tr>
|
|
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
|
|
<td align="center"><a href="../../../../../index.html">Home</a></td>
|
|
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
|
|
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
|
|
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
|
|
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
|
|
</tr></table>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="runs_test.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../statistics.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../vector_functionals.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
|
|
<a name="math_toolkit.ljung_box"></a><a class="link" href="ljung_box.html" title="The Ljung-Box Test">The Ljung-Box Test</a>
|
|
</h2></div></div></div>
|
|
<h4>
|
|
<a name="math_toolkit.ljung_box.h0"></a>
|
|
<span class="phrase"><a name="math_toolkit.ljung_box.synopsis"></a></span><a class="link" href="ljung_box.html#math_toolkit.ljung_box.synopsis">Synopsis</a>
|
|
</h4>
|
|
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">statistics</span><span class="special">/</span><span class="identifier">ljung_box</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
|
|
|
<span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">statistics</span> <span class="special">{</span>
|
|
|
|
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">RandomAccessIterator</span><span class="special">></span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="identifier">Real</span><span class="special">,</span> <span class="identifier">Real</span><span class="special">></span> <span class="identifier">ljung_box</span><span class="special">(</span><span class="identifier">RandomAccessIterator</span> <span class="identifier">begin</span><span class="special">,</span> <span class="identifier">RandomAccessIterator</span> <span class="identifier">end</span><span class="special">,</span> <span class="identifier">int64_t</span> <span class="identifier">lags</span> <span class="special">=</span> <span class="special">-</span><span class="number">1</span><span class="special">,</span> <span class="identifier">int64_t</span> <span class="identifier">fit_dof</span> <span class="special">=</span> <span class="number">0</span><span class="special">);</span>
|
|
|
|
|
|
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">RandomAccessContainer</span><span class="special">></span>
|
|
<span class="keyword">auto</span> <span class="identifier">ljung_box</span><span class="special">(</span><span class="identifier">RandomAccessContainer</span> <span class="keyword">const</span> <span class="special">&</span> <span class="identifier">v</span><span class="special">,</span> <span class="identifier">int64_t</span> <span class="identifier">lags</span> <span class="special">=</span> <span class="special">-</span><span class="number">1</span><span class="special">,</span> <span class="identifier">int64_t</span> <span class="identifier">fit_dof</span> <span class="special">=</span> <span class="number">0</span><span class="special">);</span>
|
|
|
|
<span class="special">}</span>
|
|
</pre>
|
|
<h4>
|
|
<a name="math_toolkit.ljung_box.h1"></a>
|
|
<span class="phrase"><a name="math_toolkit.ljung_box.background"></a></span><a class="link" href="ljung_box.html#math_toolkit.ljung_box.background">Background</a>
|
|
</h4>
|
|
<p>
|
|
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.
|
|
</p>
|
|
<p>
|
|
The test statistic is
|
|
</p>
|
|
<p>
|
|
<span class="inlinemediaobject"><object type="image/svg+xml" data="../../graphs/ljung_box_definition.svg" width="479" height="55"></object></span>
|
|
</p>
|
|
<p>
|
|
where <span class="emphasis"><em>n</em></span> is the length of <span class="emphasis"><em>v</em></span> and ℓ
|
|
is the number of lags.
|
|
</p>
|
|
<p>
|
|
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.
|
|
</p>
|
|
<p>
|
|
An example use is given below:
|
|
</p>
|
|
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">vector</span><span class="special">></span>
|
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">random</span><span class="special">></span>
|
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span>
|
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">statistics</span><span class="special">/</span><span class="identifier">ljung_box</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
|
<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">statistics</span><span class="special">::</span><span class="identifier">ljung_box</span><span class="special">;</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">random_device</span> <span class="identifier">rd</span><span class="special">;</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">normal_distribution</span><span class="special"><</span><span class="keyword">double</span><span class="special">></span> <span class="identifier">dis</span><span class="special">(</span><span class="number">0</span><span class="special">,</span> <span class="number">1</span><span class="special">);</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="keyword">double</span><span class="special">></span> <span class="identifier">v</span><span class="special">(</span><span class="number">8192</span><span class="special">);</span>
|
|
<span class="keyword">for</span> <span class="special">(</span><span class="keyword">auto</span> <span class="special">&</span> <span class="identifier">x</span> <span class="special">:</span> <span class="identifier">v</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">dis</span><span class="special">(</span><span class="identifier">rd</span><span class="special">);</span> <span class="special">}</span>
|
|
<span class="keyword">auto</span> <span class="special">[</span><span class="identifier">Q</span><span class="special">,</span> <span class="identifier">p</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">ljung_box</span><span class="special">(</span><span class="identifier">v</span><span class="special">);</span>
|
|
<span class="comment">// Possible output: Q = 5.94734, p = 0.819668</span>
|
|
</pre>
|
|
<p>
|
|
Now if the result is clearly autocorrelated:
|
|
</p>
|
|
<pre class="programlisting"><span class="keyword">for</span> <span class="special">(</span><span class="identifier">size_t</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special"><</span> <span class="identifier">v</span><span class="special">.</span><span class="identifier">size</span><span class="special">();</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">v</span><span class="special">[</span><span class="identifier">i</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">i</span><span class="special">;</span> <span class="special">}</span>
|
|
<span class="keyword">auto</span> <span class="special">[</span><span class="identifier">Q</span><span class="special">,</span> <span class="identifier">p</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">ljung_box</span><span class="special">(</span><span class="identifier">v</span><span class="special">);</span>
|
|
<span class="comment">// Possible output: Q = 81665.1, p = 0</span>
|
|
</pre>
|
|
<p>
|
|
By default, the number of lags is taken to be the logarithm of the number of
|
|
samples, so that the default complexity is [bigO](<span class="emphasis"><em>n</em></span> ln
|
|
<span class="emphasis"><em>n</em></span>). If you want to calculate a given number of lags, use
|
|
the second argument:
|
|
</p>
|
|
<pre class="programlisting"><span class="identifier">int64_t</span> <span class="identifier">lags</span> <span class="special">=</span> <span class="number">10</span><span class="special">;</span>
|
|
<span class="keyword">auto</span> <span class="special">[</span><span class="identifier">Q</span><span class="special">,</span> <span class="identifier">p</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">ljung_box</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span><span class="number">10</span><span class="special">);</span>
|
|
</pre>
|
|
<p>
|
|
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 <span class="emphasis"><em>Q</em></span>, but only the <span class="emphasis"><em>p</em></span>-value.
|
|
If you need to specify the number of degrees of freedom, use
|
|
</p>
|
|
<pre class="programlisting"><span class="identifier">int64_t</span> <span class="identifier">fit_dof</span> <span class="special">=</span> <span class="number">2</span><span class="special">;</span>
|
|
<span class="keyword">auto</span> <span class="special">[</span><span class="identifier">Q</span><span class="special">,</span> <span class="identifier">p</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">ljung_box</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="special">-</span><span class="number">1</span><span class="special">,</span> <span class="identifier">fit_dof</span><span class="special">);</span>
|
|
</pre>
|
|
<p>
|
|
For example, if you fit your data with an ARIMA(<span class="emphasis"><em>p</em></span>, <span class="emphasis"><em>q</em></span>)
|
|
model, then <code class="computeroutput"><span class="identifier">fit_dof</span> <span class="special">=</span>
|
|
<span class="identifier">p</span> <span class="special">+</span> <span class="identifier">q</span></code>.
|
|
</p>
|
|
</div>
|
|
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
|
<td align="left"></td>
|
|
<td align="right"><div class="copyright-footer">Copyright © 2006-2019 Nikhar
|
|
Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
|
|
Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
|
|
Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
|
|
Daryle Walker and Xiaogang Zhang<p>
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
|
|
</p>
|
|
</div></td>
|
|
</tr></table>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="runs_test.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../statistics.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../vector_functionals.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
|
|
</div>
|
|
</body>
|
|
</html>
|