3191
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3922
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
3191
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
3191
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
3191
|
15 ## You should have received a copy of the GNU General Public License |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
3191
|
19 |
3449
|
20 ## -*- texinfo -*- |
3909
|
21 ## @deftypefn {Function File} {[@var{pval}, @var{lm}] =} arch_test (@var{y}, @var{x}, @var{p}) |
3449
|
22 ## For a linear regression model |
3191
|
23 ## |
3449
|
24 ## @example |
|
25 ## y = x * b + e |
|
26 ## @end example |
|
27 ## |
|
28 ## @noindent |
|
29 ## perform a Lagrange Multiplier (LM) test of the null hypothesis of no |
|
30 ## conditional heteroscedascity against the alternative of CH(@var{p}). |
|
31 ## |
3191
|
32 ## I.e., the model is |
3449
|
33 ## |
|
34 ## @example |
3499
|
35 ## y(t) = b(1) * x(t,1) + @dots{} + b(k) * x(t,k) + e(t), |
3449
|
36 ## @end example |
|
37 ## |
|
38 ## @noindent |
3499
|
39 ## given @var{y} up to @math{t-1} and @var{x} up to @math{t}, |
|
40 ## @math{e}(t) is @math{N(0, h(t))} with |
3449
|
41 ## |
|
42 ## @example |
3499
|
43 ## h(t) = v + a(1) * e(t-1)^2 + @dots{} + a(p) * e(t-p)^2, |
3449
|
44 ## @end example |
|
45 ## |
|
46 ## @noindent |
3499
|
47 ## and the null is @math{a(1)} == @dots{} == @math{a(p)} == 0. |
3191
|
48 ## |
3499
|
49 ## If the second argument is a scalar integer, @math{k}, perform the same |
|
50 ## test in a linear autoregression model of order @math{k}, i.e., with |
3449
|
51 ## |
|
52 ## @example |
3499
|
53 ## [1, y(t-1), @dots{}, y(t-@var{k})] |
3449
|
54 ## @end example |
3191
|
55 ## |
3449
|
56 ## @noindent |
3499
|
57 ## as the @math{t}-th row of @var{x}. |
3191
|
58 ## |
3449
|
59 ## Under the null, LM approximately has a chisquare distribution with |
3499
|
60 ## @var{p} degrees of freedom and @var{pval} is the @math{p}-value (1 |
3449
|
61 ## minus the CDF of this distribution at LM) of the test. |
|
62 ## |
3499
|
63 ## If no output argument is given, the @math{p}-value is displayed. |
3449
|
64 ## @end deftypefn |
3426
|
65 |
3457
|
66 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
67 ## Description: Test for conditional heteroscedascity |
3426
|
68 |
3191
|
69 function [pval, lm] = arch_test (y, X, p) |
|
70 |
|
71 if (nargin != 3) |
|
72 error ("arch_test needs 3 input arguments"); |
|
73 endif |
|
74 |
4030
|
75 if (! (isvector (y))) |
3457
|
76 error ("arch_test: y must be a vector"); |
3191
|
77 endif |
|
78 T = length (y); |
|
79 y = reshape (y, T, 1); |
|
80 [rx, cx] = size (X); |
|
81 if ((rx == 1) && (cx == 1)) |
|
82 X = autoreg_matrix (y, X); |
3457
|
83 elseif (! (rx == T)) |
|
84 error ("arch_test: either rows(X) == length(y), or X is a scalar"); |
3191
|
85 endif |
4030
|
86 if (! (isscalar(p) && (rem(p, 1) == 0) && (p > 0))) |
3458
|
87 error ("arch_test: p must be a positive integer"); |
3191
|
88 endif |
3426
|
89 |
3191
|
90 [b, v_b, e] = ols (y, X); |
|
91 Z = autoreg_matrix (e.^2, p); |
|
92 f = e.^2 / v_b - ones (T, 1); |
|
93 f = Z' * f; |
|
94 lm = f' * inv (Z'*Z) * f / 2; |
|
95 pval = 1 - chisquare_cdf (lm, p); |
3426
|
96 |
3191
|
97 endfunction |