Mercurial > hg > octave-lyh
annotate scripts/signal/arch_fit.m @ 11305:c9df571efe95
subplot.m: Add suppport for "align" and "replace" options.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Thu, 02 Dec 2010 18:46:10 -0500 |
parents | d1978e7364ad |
children | c776f063fefe |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2005, 2006, |
8920 | 2 ## 2007, 2009 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 |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
26 ## @group |
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
27 ## y(t) = b(1) * x(t,1) + @dots{} + b(k) * x(t,k) + e(t), |
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
28 ## h(t) = a(1) + a(2) * e(t-1)^2 + @dots{} + a(p+1) * e(t-p)^2 |
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
29 ## @end group |
3449 | 30 ## @end example |
3191 | 31 ## |
3449 | 32 ## @noindent |
3499 | 33 ## in which @math{e(t)} is @math{N(0, h(t))}, given a time-series vector |
34 ## @var{y} up to time @math{t-1} and a matrix of (ordinary) regressors | |
35 ## @var{x} up to @math{t}. The order of the regression of the residual | |
36 ## variance is specified by @var{p}. | |
3191 | 37 ## |
3449 | 38 ## If invoked as @code{arch_fit (@var{y}, @var{k}, @var{p})} with a |
39 ## positive integer @var{k}, fit an ARCH(@var{k}, @var{p}) process, | |
3499 | 40 ## i.e., do the above with the @math{t}-th row of @var{x} given by |
3191 | 41 ## |
3449 | 42 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
43 ## [1, y(t-1), @dots{}, y(t-k)] |
3449 | 44 ## @end example |
45 ## | |
46 ## Optionally, one can specify the number of iterations @var{iter}, the | |
3499 | 47 ## updating factor @var{gamma}, and initial values @math{a0} and |
48 ## @math{b0} for the scoring algorithm. | |
3449 | 49 ## @end deftypefn |
3191 | 50 |
5428 | 51 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3457 | 52 ## Description: Fit an ARCH regression model |
3191 | 53 |
54 function [a, b] = arch_fit (y, X, p, ITER, gamma, a0, b0) | |
55 | |
56 if ((nargin < 3) || (nargin == 6) || (nargin > 7)) | |
6046 | 57 print_usage (); |
3191 | 58 endif |
3426 | 59 |
4030 | 60 if (! (isvector (y))) |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
61 error ("arch_fit: y must be a vector"); |
3191 | 62 endif |
63 | |
64 T = length (y); | |
65 y = reshape (y, T, 1); | |
66 [rx, cx] = size (X); | |
67 if ((rx == 1) && (cx == 1)) | |
68 X = autoreg_matrix (y, X); | |
3457 | 69 elseif (! (rx == T)) |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
70 error ("arch_fit: either rows (X) == length (y), or X is a scalar"); |
3191 | 71 endif |
72 | |
73 [T, k] = size (X); | |
3426 | 74 |
3191 | 75 if (nargin == 7) |
76 a = a0; | |
77 b = b0; | |
78 e = y - X * b; | |
79 else | |
80 [b, v_b, e] = ols (y, X); | |
3273 | 81 a = [v_b, (zeros (1, p))]'; |
3191 | 82 if (nargin < 5) |
83 gamma = 0.1; | |
84 if (nargin < 4) | |
3426 | 85 ITER = 50; |
3191 | 86 endif |
87 endif | |
88 endif | |
3426 | 89 |
3191 | 90 esq = e.^2; |
91 Z = autoreg_matrix (esq, p); | |
92 | |
93 for i = 1 : ITER; | |
94 h = Z * a; | |
95 tmp = esq ./ h.^2 - 1 ./ h; | |
96 s = 1 ./ h(1:T-p); | |
97 for j = 1 : p; | |
98 s = s - a(j+1) * tmp(j+1:T-p+j); | |
99 endfor | |
100 r = 1 ./ h(1:T-p); | |
8507 | 101 for j = 1:p; |
3191 | 102 r = r + 2 * h(j+1:T-p+j).^2 .* esq(1:T-p); |
103 endfor | |
104 r = sqrt (r); | |
105 X_tilde = X(1:T-p, :) .* (r * ones (1,k)); | |
106 e_tilde = e(1:T-p) .*s ./ r; | |
107 delta_b = inv (X_tilde' * X_tilde) * X_tilde' * e_tilde; | |
108 b = b + gamma * delta_b; | |
109 e = y - X * b; | |
110 esq = e .^ 2; | |
111 Z = autoreg_matrix (esq, p); | |
112 h = Z * a; | |
113 f = esq ./ h - ones(T,1); | |
114 Z_tilde = Z ./ (h * ones (1, p+1)); | |
115 delta_a = inv (Z_tilde' * Z_tilde) * Z_tilde' * f; | |
116 a = a + gamma * delta_a; | |
117 endfor | |
3426 | 118 |
3191 | 119 endfunction |