3191
|
1 ## Copyright (C) 1995, 1996, 1997 Friedrich Leisch |
|
2 ## |
|
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. |
|
7 ## |
|
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 |
|
11 ## General Public License for more details. |
|
12 ## |
|
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 |
|
17 ## usage: autocov (X [, h]) |
|
18 ## |
|
19 ## returns the autocovariances from lag 0 to h of vector X. |
|
20 ## If h is omitted, all autocovariances are computed. |
|
21 ## If X is a matrix, the autocovariances of every single column are |
|
22 ## computed. |
|
23 |
|
24 ## Author: FL <Friedrich.Leisch@ci.tuwien.ac.at> |
|
25 ## Description: Compute autocovariances |
|
26 |
|
27 function retval = autocov (X, h) |
|
28 |
|
29 [n c] = size (X); |
|
30 |
|
31 if (is_vector (X)) |
|
32 n = length (X); |
|
33 c = 1; |
|
34 X = reshape (X, n, 1); |
|
35 endif |
|
36 |
|
37 X = center (X); |
|
38 |
|
39 if (nargin == 1) |
|
40 h = n - 1; |
|
41 endif |
|
42 |
|
43 retval = zeros (h + 1, c); |
|
44 |
|
45 unwind_protect |
|
46 |
|
47 oldpcv = prefer_column_vectors; |
|
48 prefer_column_vectors = "false"; |
|
49 |
|
50 for i = 0 : h |
|
51 retval(i+1, :) = diag (X(i+1:n, :).' * conj (X(1:n-i, :))) / n; |
|
52 endfor |
|
53 |
|
54 unwind_protect_cleanup |
|
55 |
|
56 prefer_column_vectors = oldpcv; |
|
57 |
|
58 end_unwind_protect |
|
59 |
|
60 endfunction |