Mercurial > hg > octave-nkf
annotate scripts/signal/arch_fit.m @ 14348:95c43fc8dbe1 stable rc-3-6-1-0
3.6.1 release candidate 0
* configure.ac (AC_INIT): Version is now 3.6.1-rc0.
(OCTAVE_RELEASE_DATE): Now 2012-02-07.
* liboctave/Makefile.am: Bump liboctave revision version.
* src/Makefile.am: Bump liboctave revision version.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 09 Feb 2012 11:25:04 -0500 |
parents | 72c96de7a403 |
children | 5d3a684236b0 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1 ## Copyright (C) 1995-2012 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) 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 |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
3191 | 18 |
3449 | 19 ## -*- texinfo -*- |
20 ## @deftypefn {Function File} {[@var{a}, @var{b}] =} arch_fit (@var{y}, @var{x}, @var{p}, @var{iter}, @var{gamma}, @var{a0}, @var{b0}) | |
21 ## Fit an ARCH regression model to the time series @var{y} using the | |
22 ## scoring algorithm in Engle's original ARCH paper. The model is | |
3191 | 23 ## |
3449 | 24 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
25 ## @group |
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
26 ## 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
|
27 ## 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
|
28 ## @end group |
3449 | 29 ## @end example |
3191 | 30 ## |
3449 | 31 ## @noindent |
3499 | 32 ## in which @math{e(t)} is @math{N(0, h(t))}, given a time-series vector |
33 ## @var{y} up to time @math{t-1} and a matrix of (ordinary) regressors | |
34 ## @var{x} up to @math{t}. The order of the regression of the residual | |
35 ## variance is specified by @var{p}. | |
3191 | 36 ## |
3449 | 37 ## If invoked as @code{arch_fit (@var{y}, @var{k}, @var{p})} with a |
38 ## positive integer @var{k}, fit an ARCH(@var{k}, @var{p}) process, | |
3499 | 39 ## i.e., do the above with the @math{t}-th row of @var{x} given by |
3191 | 40 ## |
3449 | 41 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
42 ## [1, y(t-1), @dots{}, y(t-k)] |
3449 | 43 ## @end example |
44 ## | |
45 ## Optionally, one can specify the number of iterations @var{iter}, the | |
3499 | 46 ## updating factor @var{gamma}, and initial values @math{a0} and |
47 ## @math{b0} for the scoring algorithm. | |
3449 | 48 ## @end deftypefn |
3191 | 49 |
5428 | 50 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3457 | 51 ## Description: Fit an ARCH regression model |
3191 | 52 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
53 function [a, b] = arch_fit (y, x, p, iter, gamma, a0, b0) |
3191 | 54 |
55 if ((nargin < 3) || (nargin == 6) || (nargin > 7)) | |
6046 | 56 print_usage (); |
3191 | 57 endif |
3426 | 58 |
4030 | 59 if (! (isvector (y))) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11469
diff
changeset
|
60 error ("arch_fit: Y must be a vector"); |
3191 | 61 endif |
62 | |
63 T = length (y); | |
64 y = reshape (y, T, 1); | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
65 [rx, cx] = size (x); |
3191 | 66 if ((rx == 1) && (cx == 1)) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
67 x = autoreg_matrix (y, x); |
3457 | 68 elseif (! (rx == T)) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11469
diff
changeset
|
69 error ("arch_fit: either rows (X) == length (Y), or X is a scalar"); |
3191 | 70 endif |
71 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
72 [T, k] = size (x); |
3426 | 73 |
3191 | 74 if (nargin == 7) |
75 a = a0; | |
76 b = b0; | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
77 e = y - x * b; |
3191 | 78 else |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
79 [b, v_b, e] = ols (y, x); |
3273 | 80 a = [v_b, (zeros (1, p))]'; |
3191 | 81 if (nargin < 5) |
82 gamma = 0.1; | |
83 if (nargin < 4) | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
84 iter = 50; |
3191 | 85 endif |
86 endif | |
87 endif | |
3426 | 88 |
3191 | 89 esq = e.^2; |
90 Z = autoreg_matrix (esq, p); | |
91 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
92 for i = 1 : iter; |
3191 | 93 h = Z * a; |
94 tmp = esq ./ h.^2 - 1 ./ h; | |
95 s = 1 ./ h(1:T-p); | |
96 for j = 1 : p; | |
97 s = s - a(j+1) * tmp(j+1:T-p+j); | |
98 endfor | |
99 r = 1 ./ h(1:T-p); | |
8507 | 100 for j = 1:p; |
3191 | 101 r = r + 2 * h(j+1:T-p+j).^2 .* esq(1:T-p); |
102 endfor | |
103 r = sqrt (r); | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
104 X_tilde = x(1:T-p, :) .* (r * ones (1,k)); |
3191 | 105 e_tilde = e(1:T-p) .*s ./ r; |
106 delta_b = inv (X_tilde' * X_tilde) * X_tilde' * e_tilde; | |
107 b = b + gamma * delta_b; | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
108 e = y - x * b; |
3191 | 109 esq = e .^ 2; |
110 Z = autoreg_matrix (esq, p); | |
111 h = Z * a; | |
112 f = esq ./ h - ones(T,1); | |
113 Z_tilde = Z ./ (h * ones (1, p+1)); | |
114 delta_a = inv (Z_tilde' * Z_tilde) * Z_tilde' * f; | |
115 a = a + gamma * delta_a; | |
116 endfor | |
3426 | 117 |
3191 | 118 endfunction |