forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lrratio.m
87 lines (72 loc) · 2.32 KB
/
lrratio.m
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
function lrratio(y,maxlag,minlag,simsc,x)
% PURPOSE: performs likelihood ratio test for var model
% to determine optimal lag length
%---------------------------------------------------
% USAGE: lrratio(y,maxlag,minlag,simsc,x)
% where: y = an (nobs x neqs) matrix of y-vectors
% maxlag = the maximum lag length
% minlag = the minimum lag length
% simsc = flag for Sim's dof correction factor
% 0 = no, 1 = use correction
% (default = 0)
% x = optional matrix of variables (nobs x nx)
% (NOTE: constant vector automatically included)
%---------------------------------------------------
% RETURNS: nothing, prints results to the MATLAB command window
%---------------------------------------------------
% SEE ALSO: var, varf, prt_var
%---------------------------------------------------
if nargin > 5
error('wrong # of arguments to lrratio');
elseif nargin == 5
xflag = 1;
elseif nargin == 4
xflag = 0;
elseif nargin == 3
simsc = 0;
xflag = 0;
end;
if maxlag < minlag
error('maxlag < minlag in lrratio');
end;
if minlag < 1
minlag = 1;
end;
[nobs neqs] = size(y);
% loop over lag lengths and do likelihood ratio tests
for i=maxlag:-1:minlag+1
% adjust nobs to feed the lags
nobse = nobs-i;
if xflag == 1 % case of deterministic variables
if i == maxlag
resid1 = var_resid(y,i,x);
resid2 = var_resid(y,i-1,x); % restricted model
else
resid1 = resid2; % save time by not running it again
resid2 = var_resid(y,i-1,x);
end;
else % case of no deterministic variables
if i == maxlag
resid1 = var_resid(y,i);
resid2 = var_resid(y,i-1); % restricted model
else
resid1 = resid2; % save time by not running it again
resid2 = var_resid(y,i-1);
end;
end;
% compute likelihood ratio test
% first get var-cov matrices for residuals
epe1 = cov(resid1); % cov is a MATLAB command
epe2 = cov(resid2);
if simsc == 1
tminusc = nobse-neqs*i+1;
else
tminusc = nobse;
end;
% compute (T-c)*(log(det(epe2)) - log(det(epe1)))
lratio = tminusc*(log(det(epe2)) - log(det(epe1)));
% find marginal probability
lprob = chis_prb(lratio,neqs*neqs);
out = [i i-1 lratio 1-lprob];
fprintf(1,'nlag = %2d %2d, LR statistic = %16.4f, probability = %6.4g \n',out);
end;