1 function [AUTOCOV,stderr,lpq,qpval] = acorf(Z,N);
2 % Calculates autocorrelations for multiple data series.
3 % Missing values in Z (NaN) are considered.
4 % Also calculates Ljung-Box Q stats and p-values.
6 % [AutoCorr,stderr,lpq,qpval] = acorf(Z,N);
7 % If mean should be removed use
8 % [AutoCorr,stderr,lpq,qpval] = acorf(detrend(Z',0)',N);
9 % If trend should be removed use
10 % [AutoCorr,stderr,lpq,qpval] = acorf(detrend(Z')',N);
13 % Z is data series for which autocorrelations are required
18 % AutoCorr nr x N matrix of autocorrelations
19 % stderr nr x N matrix of (approx) std errors
20 % lpq nr x M matrix of Ljung-Box Q stats
21 % qpval nr x N matrix of p-values on Q stats
23 % All input and output parameters are organized in rows, one row
24 % corresponds to one series
27 % S. Haykin "Adaptive Filter Theory" 3ed. Prentice Hall, 1996.
28 % M.B. Priestley "Spectral Analysis and Time Series" Academic Press, 1981.
29 % W.S. Wei "Time Series Analysis" Addison Wesley, 1990.
30 % J.S. Bendat and A.G.Persol "Random Data: Analysis and Measurement procedures", Wiley, 1986.
32 % calculating lpq, stderr, qpval from
33 % suggested by Philip Gray, University of New South Wales,
35 % $Id: acorf.m 5090 2008-06-05 08:12:04Z schloegl $
36 % Copyright (C) 1998-2002,2008 by Alois Schloegl <a.schloegl@ieee.org>
38 % This program is free software: you can redistribute it and/or modify
39 % it under the terms of the GNU General Public License as published by
40 % the Free Software Foundation, either version 3 of the License, or
41 % (at your option) any later version.
43 % This program is distributed in the hope that it will be useful,
44 % but WITHOUT ANY WARRANTY; without even the implied warranty of
45 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 % GNU General Public License for more details.
48 % You should have received a copy of the GNU General Public License
49 % along with this program. If not, see <http://www.gnu.org/licenses/>.
53 NC = sum(~isnan(Z),2); % missing values
56 AUTOCOV = AUTOCOV(:,2:N+1) ./ AUTOCOV(:,ones(1,N));
59 stderr = sqrt(1./NC)*ones(1,N);
65 cum = cum+AUTOCOV(:,k).*conj(AUTOCOV(:,k))./(NC-k);
66 lpq(:,k) = NC.*(NC+2).*cum; % Ljung box Q for k lags
67 %qpval(:,k) = 1 - chi2cdf(lpq(:,k),k); % p-value of Q stat
68 qpval(:,k) = 1 - gammainc(lpq(:,k)/2,k/2); % replace chi2cdf by gammainc