7017
|
1 ## Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2005, 2006, |
|
2 ## 2007 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 -*- |
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 ## |
7031
|
26 ## @smallexample |
3499
|
27 ## y(t) = b(1) + b(2) * y(t-1) + @dots{} + b(lb) * y(t-lb+1) + e(t), |
7031
|
28 ## @end smallexample |
3449
|
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 ## |
7031
|
34 ## @smallexample |
3499
|
35 ## h(t) = a(1) + a(2) * e(t-1)^2 + @dots{} + a(la) * e(t-la+1)^2 |
7031
|
36 ## @end smallexample |
3449
|
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 |
7436
|
66 |
3191
|
67 lb = length (b); |
|
68 b = reshape (b, 1, lb); |
|
69 if (lb == 1) |
|
70 b = [b, 0]; |
|
71 lb = lb + 1; |
|
72 endif |
3238
|
73 M = max([la, lb]); |
3426
|
74 |
3191
|
75 e = zeros (T, 1); |
|
76 h = zeros (T, 1); |
|
77 y = zeros (T, 1); |
3426
|
78 |
3191
|
79 h(1) = a(1); |
|
80 e(1) = sqrt (h(1)) * randn; |
|
81 y(1) = b(1) + e(1); |
3426
|
82 |
7436
|
83 for t = 2:M |
3238
|
84 ta = min ([t, la]); |
7436
|
85 h(t) = a(1) + a(2:ta) * e(t-ta+1:t-1).^2; |
3191
|
86 e(t) = sqrt (h(t)) * randn; |
3238
|
87 tb = min ([t, lb]); |
7436
|
88 y(t) = b(1) + b(2:tb) * y(t-tb+1:t-1) + e(t); |
3191
|
89 endfor |
7436
|
90 |
3191
|
91 if (T > M) |
7436
|
92 for t = M+1:T |
|
93 h(t) = a(1) + a(2:la) * e(t-la+1:t-1).^2; |
3191
|
94 e(t) = sqrt (h(t)) * randn; |
7436
|
95 y(t) = b(1) + b(2:lb) * y(t-tb+1:t-1) + e(t); |
3191
|
96 endfor |
|
97 endif |
3426
|
98 |
3191
|
99 y = y(1:T); |
3426
|
100 |
3191
|
101 endfunction |