3191
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3191
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2, or (at your option) |
|
6 ## any later version. |
3426
|
7 ## |
3191
|
8 ## This program is distributed in the hope that it will be useful, but |
|
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
11 ## General Public License for more details. |
|
12 ## |
3191
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this file. If not, write to the Free Software Foundation, |
|
15 ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
16 |
3449
|
17 ## -*- texinfo -*- |
|
18 ## @deftypefn {Function File} {[@var{a}, @var{b}] =} arch_fit (@var{y}, @var{x}, @var{p}, @var{iter}, @var{gamma}, @var{a0}, @var{b0}) |
|
19 ## Fit an ARCH regression model to the time series @var{y} using the |
|
20 ## scoring algorithm in Engle's original ARCH paper. The model is |
3191
|
21 ## |
3449
|
22 ## @example |
|
23 ## y(t) = b(1) * x(t,1) + ... + b(k) * x(t,k) + e(t), |
|
24 ## h(t) = a(1) + a(2) * e(t-1)^2 + ... + a(p+1) * e(t-p)^2 |
|
25 ## @end example |
3191
|
26 ## |
3449
|
27 ## @noindent |
|
28 ## in which @var{e}(@var{t}) is @var{N}(0, @var{h}(@var{t})), given a |
|
29 ## time-series vector @var{y} up to time @var{t}-1 and a matrix of |
|
30 ## (ordinary) regressors @var{x} up to @var{t}. The order of the |
|
31 ## regression of the residual variance is specified by @var{p}. |
3191
|
32 ## |
3449
|
33 ## If invoked as @code{arch_fit (@var{y}, @var{k}, @var{p})} with a |
|
34 ## positive integer @var{k}, fit an ARCH(@var{k}, @var{p}) process, |
|
35 ## i.e., do the above with the @var{t}-th row of @var{x} given by |
3191
|
36 ## |
3449
|
37 ## @example |
|
38 ## [1, y(t-1), ..., y(t-k)] |
|
39 ## @end example |
|
40 ## |
|
41 ## Optionally, one can specify the number of iterations @var{iter}, the |
|
42 ## updating factor @var{gamma}, and initial values @var{a0} and @var{b0} |
|
43 ## for the scoring algorithm. |
|
44 ## @end deftypefn |
3191
|
45 |
|
46 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
47 ## Description: Fit an ARCH regression model |
|
48 |
|
49 function [a, b] = arch_fit (y, X, p, ITER, gamma, a0, b0) |
|
50 |
|
51 if ((nargin < 3) || (nargin == 6) || (nargin > 7)) |
3449
|
52 usage ("arch_fit (y, X, p, ITER, gamma, a0, b0)"); |
3191
|
53 endif |
3426
|
54 |
3191
|
55 if !(is_vector (y)) |
|
56 error ("arch_test: y must be a vector"); |
|
57 endif |
|
58 |
|
59 T = length (y); |
|
60 y = reshape (y, T, 1); |
|
61 [rx, cx] = size (X); |
|
62 if ((rx == 1) && (cx == 1)) |
|
63 X = autoreg_matrix (y, X); |
|
64 elseif !(rx == T) |
|
65 error (["arch_test: ", ... |
3426
|
66 "either rows (X) == length (y), or X is a scalar"]); |
3191
|
67 endif |
|
68 |
|
69 [T, k] = size (X); |
3426
|
70 |
3191
|
71 if (nargin == 7) |
|
72 a = a0; |
|
73 b = b0; |
|
74 e = y - X * b; |
|
75 else |
|
76 [b, v_b, e] = ols (y, X); |
3273
|
77 a = [v_b, (zeros (1, p))]'; |
3191
|
78 if (nargin < 5) |
|
79 gamma = 0.1; |
|
80 if (nargin < 4) |
3426
|
81 ITER = 50; |
3191
|
82 endif |
|
83 endif |
|
84 endif |
3426
|
85 |
3191
|
86 esq = e.^2; |
|
87 Z = autoreg_matrix (esq, p); |
|
88 |
|
89 for i = 1 : ITER; |
|
90 h = Z * a; |
|
91 tmp = esq ./ h.^2 - 1 ./ h; |
|
92 s = 1 ./ h(1:T-p); |
|
93 for j = 1 : p; |
|
94 s = s - a(j+1) * tmp(j+1:T-p+j); |
|
95 endfor |
|
96 r = 1 ./ h(1:T-p); |
|
97 for j=1:p; |
|
98 r = r + 2 * h(j+1:T-p+j).^2 .* esq(1:T-p); |
|
99 endfor |
|
100 r = sqrt (r); |
|
101 X_tilde = X(1:T-p, :) .* (r * ones (1,k)); |
|
102 e_tilde = e(1:T-p) .*s ./ r; |
|
103 delta_b = inv (X_tilde' * X_tilde) * X_tilde' * e_tilde; |
|
104 b = b + gamma * delta_b; |
|
105 e = y - X * b; |
|
106 esq = e .^ 2; |
|
107 Z = autoreg_matrix (esq, p); |
|
108 h = Z * a; |
|
109 f = esq ./ h - ones(T,1); |
|
110 Z_tilde = Z ./ (h * ones (1, p+1)); |
|
111 delta_a = inv (Z_tilde' * Z_tilde) * Z_tilde' * f; |
|
112 a = a + gamma * delta_a; |
|
113 endfor |
3426
|
114 |
3191
|
115 endfunction |