3191
|
1 ## Copyright (C) 1995, 1996, 1997 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 |
3191
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## 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 |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
3191
|
19 |
3449
|
20 ## -*- texinfo -*- |
3500
|
21 ## @deftypefn {Function File} {} arch_rnd (@var{a}, @var{b}, @var{t}) |
|
22 ## Simulate an ARCH sequence of length @var{t} with AR |
3449
|
23 ## coefficients @var{b} and CH coefficients @var{a}. I.e., the result |
3500
|
24 ## @math{y(t)} follows the model |
3191
|
25 ## |
3449
|
26 ## @example |
3499
|
27 ## y(t) = b(1) + b(2) * y(t-1) + @dots{} + b(lb) * y(t-lb+1) + e(t), |
3449
|
28 ## @end example |
|
29 ## |
|
30 ## @noindent |
3499
|
31 ## where @math{e(t)}, given @var{y} up to time @math{t-1}, is |
|
32 ## @math{N(0, h(t))}, with |
3449
|
33 ## |
|
34 ## @example |
3499
|
35 ## h(t) = a(1) + a(2) * e(t-1)^2 + @dots{} + a(la) * e(t-la+1)^2 |
3449
|
36 ## @end example |
|
37 ## @end deftypefn |
3191
|
38 |
5428
|
39 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3457
|
40 ## Description: Simulate an ARCH process |
3191
|
41 |
|
42 function y = arch_rnd (a, b, T) |
3426
|
43 |
3191
|
44 if (nargin != 3) |
6046
|
45 print_usage (); |
3191
|
46 endif |
3426
|
47 |
3457
|
48 if (! ((min (size (a)) == 1) && (min (size (b)) == 1))) |
|
49 error ("arch_rnd: a and b must both be scalars or vectors"); |
3191
|
50 endif |
4030
|
51 if (! (isscalar (T) && (T > 0) && (rem (T, 1) == 0))) |
3457
|
52 error ("arch_rnd: T must be a positive integer"); |
3191
|
53 endif |
3426
|
54 |
3457
|
55 if (! (a(1) > 0)) |
|
56 error ("arch_rnd: a(1) must be positive"); |
3191
|
57 endif |
|
58 ## perhaps add a test for the roots of a(z) here ... |
3426
|
59 |
3191
|
60 la = length (a); |
|
61 a = reshape (a, 1, la); |
|
62 if (la == 1) |
|
63 a = [a, 0]; |
|
64 la = la + 1; |
|
65 endif |
|
66 lb = length (b); |
|
67 b = reshape (b, 1, lb); |
|
68 if (lb == 1) |
|
69 b = [b, 0]; |
|
70 lb = lb + 1; |
|
71 endif |
3238
|
72 M = max([la, lb]); |
3426
|
73 |
3191
|
74 e = zeros (T, 1); |
|
75 h = zeros (T, 1); |
|
76 y = zeros (T, 1); |
3426
|
77 |
3191
|
78 h(1) = a(1); |
|
79 e(1) = sqrt (h(1)) * randn; |
|
80 y(1) = b(1) + e(1); |
3426
|
81 |
3191
|
82 for t= 2 : M; |
3238
|
83 ta = min ([t, la]); |
3191
|
84 h(t) = a(1) + a(2:ta) * e(t-1:t-ta+1).^2; |
|
85 e(t) = sqrt (h(t)) * randn; |
3238
|
86 tb = min ([t, lb]); |
3191
|
87 y(t) = b(1) + b(2:tb) * y(t-1:t-tb+1) + e(t); |
|
88 endfor |
|
89 if (T > M) |
|
90 for t = M+1 : T; |
|
91 h(t) = a(1) + a(2:la) * e(t-1:t-la+1).^2; |
|
92 e(t) = sqrt (h(t)) * randn; |
|
93 y(t) = b(1) + b(2:lb) * y(t-1:t-tb+1) + e(t); |
|
94 endfor |
|
95 endif |
3426
|
96 |
3191
|
97 y = y(1:T); |
3426
|
98 |
3191
|
99 endfunction |