annotate extra/tsa/inst/acorf.m @ 12580:b6eace8bc216 octave-forge

[tsa] update contact email address
author schloegl
date Thu, 02 Apr 2015 10:00:34 +0000
parents 18ff3d258eea
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2414
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
1 function [AUTOCOV,stderr,lpq,qpval] = acorf(Z,N);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
2 % Calculates autocorrelations for multiple data series.
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
3 % Missing values in Z (NaN) are considered.
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
4 % Also calculates Ljung-Box Q stats and p-values.
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
5 %
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
6 % [AutoCorr,stderr,lpq,qpval] = acorf(Z,N);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
7 % If mean should be removed use
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
8 % [AutoCorr,stderr,lpq,qpval] = acorf(detrend(Z',0)',N);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
9 % If trend should be removed use
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
10 % [AutoCorr,stderr,lpq,qpval] = acorf(detrend(Z')',N);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
11 %
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
12 % INPUT
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
13 % Z is data series for which autocorrelations are required
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
14 % each in a row
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
15 % N maximum lag
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
16 %
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
17 % OUTPUT
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
18 % AutoCorr nr x N matrix of autocorrelations
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
19 % stderr nr x N matrix of (approx) std errors
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
20 % lpq nr x M matrix of Ljung-Box Q stats
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
21 % qpval nr x N matrix of p-values on Q stats
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
22 %
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
23 % All input and output parameters are organized in rows, one row
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
24 % corresponds to one series
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
25 %
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
26 % REFERENCES:
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
27 % S. Haykin "Adaptive Filter Theory" 3ed. Prentice Hall, 1996.
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
28 % M.B. Priestley "Spectral Analysis and Time Series" Academic Press, 1981.
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
29 % W.S. Wei "Time Series Analysis" Addison Wesley, 1990.
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
30 % J.S. Bendat and A.G.Persol "Random Data: Analysis and Measurement procedures", Wiley, 1986.
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
31
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
32 % calculating lpq, stderr, qpval from
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
33 % suggested by Philip Gray, University of New South Wales,
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
34
4907
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
35 % $Id$
12580
b6eace8bc216 [tsa] update contact email address
schloegl
parents: 4907
diff changeset
36 % Copyright (C) 1998-2002,2008 by Alois Schloegl <alois.schloegl@gmail.com>
4907
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
37 %
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
38 % This program is free software: you can redistribute it and/or modify
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
39 % it under the terms of the GNU General Public License as published by
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
40 % the Free Software Foundation, either version 3 of the License, or
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
41 % (at your option) any later version.
2414
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
42 %
4907
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
43 % This program is distributed in the hope that it will be useful,
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
44 % but WITHOUT ANY WARRANTY; without even the implied warranty of
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
45 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
46 % GNU General Public License for more details.
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
47 %
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
48 % You should have received a copy of the GNU General Public License
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
49 % along with this program. If not, see <http://www.gnu.org/licenses/>.
18ff3d258eea update license of TSA toolbox to GPLv3
schloegl
parents: 4404
diff changeset
50
2414
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
51
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
52 [nr,nc] = size(Z);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
53 NC = sum(~isnan(Z),2); % missing values
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
54
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
55 AUTOCOV = acovf(Z,N);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
56 AUTOCOV = AUTOCOV(:,2:N+1) ./ AUTOCOV(:,ones(1,N));
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
57
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
58 if nargout > 1
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
59 stderr = sqrt(1./NC)*ones(1,N);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
60 lpq = zeros(nr,N);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
61 qpval = zeros(nr,N);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
62
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
63 cum=zeros(nr,1);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
64 for k=1:N,
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
65 cum = cum+AUTOCOV(:,k).*conj(AUTOCOV(:,k))./(NC-k);
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
66 lpq(:,k) = NC.*(NC+2).*cum; % Ljung box Q for k lags
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
67 %qpval(:,k) = 1 - chi2cdf(lpq(:,k),k); % p-value of Q stat
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
68 qpval(:,k) = 1 - gammainc(lpq(:,k)/2,k/2); % replace chi2cdf by gammainc
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
69 end;
19251cd2753b Latest package manager mega patch, but not the last
adb014
parents:
diff changeset
70 end;