1 ## Copyright (C) 1995-2012 Kurt Hornik
3 ## This file is part of Octave.
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
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
26 ## y(t) = b(1) * x(t,1) + @dots{} + b(k) * x(t,k) + e(t),
27 ## h(t) = a(1) + a(2) * e(t-1)^2 + @dots{} + a(p+1) * e(t-p)^2
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}.
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,
39 ## i.e., do the above with the @math{t}-th row of @var{x} given by
42 ## [1, y(t-1), @dots{}, y(t-k)]
45 ## Optionally, one can specify the number of iterations @var{iter}, the
46 ## updating factor @var{gamma}, and initial values @math{a0} and
47 ## @math{b0} for the scoring algorithm.
50 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
51 ## Description: Fit an ARCH regression model
53 function [a, b] = arch_fit (y, x, p, iter, gamma, a0, b0)
55 if ((nargin < 3) || (nargin == 6) || (nargin > 7))
60 error ("arch_fit: Y must be a vector");
64 y = reshape (y, T, 1);
66 if ((rx == 1) && (cx == 1))
67 x = autoreg_matrix (y, x);
69 error ("arch_fit: either rows (X) == length (Y), or X is a scalar");
79 [b, v_b, e] = ols (y, x);
80 a = [v_b, (zeros (1, p))]';
90 Z = autoreg_matrix (esq, p);
94 tmp = esq ./ h.^2 - 1 ./ h;
97 s = s - a(j+1) * tmp(j+1:T-p+j);
101 r = r + 2 * h(j+1:T-p+j).^2 .* esq(1:T-p);
104 X_tilde = x(1:T-p, :) .* (r * ones (1,k));
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;
110 Z = autoreg_matrix (esq, p);
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;