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