-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLjungBoxTest.m
More file actions
48 lines (37 loc) · 1.27 KB
/
LjungBoxTest.m
File metadata and controls
48 lines (37 loc) · 1.27 KB
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
%==========================================================================
% Advanced Econometrics, HEC Lausanne
% Portmanteau (Ljung-Box) test for serial correlation
%--------------------------------------------------------------------------
% Author: Daria Kalyaeva
% Version: November 2014
%==========================================================================
% INPUTS
% X a vector of time series data points;
% K number of lags;
% s number of estimated parameters (p+q for residuals of ARMA(p,q)
% models)
% alpha the significance level, a value between 0 and 1.
%
% OUTPUT
% LBResult a dataset variable with the LM statistic, the
% critical value at the specified confidence level,
% and the p-value
%
%
% Requires installation of Kevin Sheppard's MFE Toolbox
function LBresult = LjungBoxTest(X,K,s,alpha)
% Calculate autocorrelations
[rho,~] = sacf(X,K,1,0);
% Define parameters
n = length(X);
rho2 = rho.^2;
% Statistic for Ljung Box
QLBstat = n*(n+2)*sum(rho2./(ones(K,1)*n - (1:K)'));
df = K-s;
LBCritVal = chi2inv(1-alpha,df);
LBpValue = 1 - chi2cdf(QLBstat,df);
% Output
LBresult = [QLBstat,LBCritVal,LBpValue];
%'Qstat','CriticalValue','pValue'},...
%'ObsNames',{'Ljung-Box'});
end