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