3191
|
1 ## Copyright (C) 1995, 1996, 1997 Friedrich Leisch |
3426
|
2 ## |
3191
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2, or (at your option) |
|
6 ## any later version. |
3426
|
7 ## |
3191
|
8 ## This program is distributed in the hope that it will be useful, but |
|
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
11 ## General Public License for more details. |
|
12 ## |
3191
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this file. If not, write to the Free Software Foundation, |
|
15 ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
16 |
3449
|
17 ## -*- texinfo -*- |
|
18 ## @deftypefn {Function File} {} arma_rnd (@var{a}, @var{b}, @var{v}, @var{t}, @var{n}) |
|
19 ## Return a simulation of the ARMA model |
|
20 ## |
|
21 ## @example |
|
22 ## x(n) = a(1) * x(n-1) + ... + a(k) * x(n-k) |
|
23 ## + e(n) + b(1) * e(n-1) + ... + b(l) * e(n-l) |
|
24 ## @end example |
3191
|
25 ## |
3449
|
26 ## @noindent |
|
27 ## in which @var{k} is the length of vector @var{a}, @var{l} is the |
|
28 ## length of vector @var{b} and @var{e} is gaussian white noise with |
|
29 ## variance @var{v}. The function returns a vector of length @var{t}. |
3191
|
30 ## |
3449
|
31 ## The optional parameter @var{n} gives the number of dummy |
|
32 ## @var{x}(@var{i}) used for initialization, i.e., a sequence of length |
|
33 ## @var{t}+@var{n} is generated and @var{x}(@var{n}+1:@var{t}+@var{n}) |
|
34 ## is returned. If @var{n} is omitted, @var{n} = 100 is used. |
|
35 ## @end deftypefn |
3426
|
36 |
3191
|
37 ## Author: FL <Friedrich.Leisch@ci.tuwien.ac.at> |
|
38 ## Description: Simulate an ARMA process |
|
39 |
|
40 function x = arma_rnd (a, b, v, t, n) |
|
41 |
|
42 unwind_protect |
|
43 orig_listelemok = empty_list_elements_ok; |
|
44 empty_list_elements_ok = "true"; |
3426
|
45 |
3191
|
46 if (nargin == 4) |
|
47 n = 100; |
|
48 elseif (nargin == 5) |
|
49 if (!is_scalar (t)) |
3426
|
50 error ("arma_rnd: n must be a scalar"); |
3191
|
51 endif |
|
52 else |
3449
|
53 usage ("arma_rnd (a, b, v, t, n)"); |
3191
|
54 endif |
|
55 |
|
56 if ( (min (size (a)) > 1) || (min (size (b)) > 1) ) |
|
57 error ("arma_rnd: a and b must not be matrices"); |
|
58 endif |
3426
|
59 |
3191
|
60 if (!is_scalar (t)) |
|
61 error ("arma_rnd: t must be a scalar"); |
|
62 endif |
3426
|
63 |
3191
|
64 ar = length (a); |
|
65 br = length (b); |
|
66 |
|
67 a = reshape (a, ar, 1); |
|
68 b = reshape (b, br, 1); |
3426
|
69 |
|
70 a = [1; -a]; # apply our notational convention |
3191
|
71 b = [1; b]; |
3426
|
72 |
3191
|
73 n = min (n, ar + br); |
3426
|
74 |
3191
|
75 e = sqrt (v) * randn (t + n, 1); |
3426
|
76 |
3191
|
77 x = filter (b, a, e); |
|
78 x = x(n + 1 : t + n); |
|
79 |
|
80 unwind_protect_cleanup |
3426
|
81 |
3191
|
82 empty_list_elements_ok = orig_listelemok; |
3426
|
83 |
3191
|
84 end_unwind_protect |
|
85 |
|
86 endfunction |