source: trunk/STATISTICS/a_timecorrelate.pro @ 2

Last change on this file since 2 was 2, checked in by opalod, 22 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.7 KB
Line 
1;+
2; NAME:
3;       A_TIMECORRELATE
4;
5; PURPOSE:
6;       Same function as A_CORRELATE but accept array (until 4
7;       dimension) for input and do the autocorrelation or the
8;       autocovariance along the time dimension which must be the last
9;       one of the input array.
10;
11;       This function computes the autocorrelation Px(L) or autocovariance
12;       Rx(L) of a sample population X as a function of the lag (L).
13;
14; CATEGORY:
15;       Statistics.
16;
17; CALLING SEQUENCE:
18;       Result = a_timecorrelate(X, Lag)
19;
20; INPUTS:
21;       X:    an Array which last dimension is the time dimension os
22;       size n.
23;
24;     LAG:    A scalar or n-element vector, in the interval [-(n-2), (n-2)],
25;             of type integer that specifies the absolute distance(s) between
26;             indexed elements of X.
27;
28; KEYWORD PARAMETERS:
29;       COVARIANCE:    If set to a non-zero value, the sample autocovariance
30;                      is computed.
31;
32;       DOUBLE:        If set to a non-zero value, computations are done in
33;                      double precision arithmetic.
34;
35; EXAMPLE
36;       Define an n-element sample population.
37;         x = [3.73, 3.67, 3.77, 3.83, 4.67, 5.87, 6.70, 6.97, 6.40, 5.57]
38;
39;       Compute the autocorrelation of X for LAG = -3, 0, 1, 3, 4, 8
40;         lag = [-3, 0, 1, 3, 4, 8]
41;         result = a_correlate(x, lag)
42;
43;       The result should be:
44;         [0.0146185, 1.00000, 0.810879, 0.0146185, -0.325279, -0.151684]
45;
46; PROCEDURE:
47;
48;
49;                         n-L-1
50;                         sigma  (X[k]-Xmean)(X[k+L]-Xmean)
51;                          k=0
52;   correlation(X,L)=----------------------------------------
53;                            n-1                   
54;                           sigma  (X[k]-Xmean)^2
55;                            k=0                   
56;
57;
58;
59;                        n-L-1
60;                        sigma  (X[k]-Xmean)(X[k+L]-Xmean)
61;                         k=0
62;   covariance(X,L)=-------------------------------------------
63;                                     n
64;
65;   Where Xmean is the Time mean of the sample population
66;   x=(x[t=0],x[t=1],...,x[t=n-1])
67;
68;
69; REFERENCE:
70;       INTRODUCTION TO STATISTICAL TIME SERIES
71;       Wayne A. Fuller
72;       ISBN 0-471-28715-6
73;
74; MODIFICATION HISTORY:
75;       24/2/2000 Sebastien Masson (smasson@lodyc.jussieu.fr)
76;       Based on the A_CORRELATE procedure of IDL
77;-
78
79FUNCTION TimeAuto_Cov, X, M, nT, Double = Double, zero2nan = zero2nan
80;Sample autocovariance function
81   TimeDim = size(X, /n_dimensions)
82   Xmean = TOTAL(X, TimeDim, Double = Double) / nT
83   if double then one = 1.0d ELSE one = 1.0
84   Xmean = Xmean[*]#replicate(one, nT - M)
85;
86;
87   case TimeDim of
88      1:res = TOTAL((X[0:nT - M - 1L] - Xmean) * (X[M:nT - 1L] - Xmean), $
89                    TimeDim, Double = Double)
90      2:res = TOTAL((X[*, 0:nT - M - 1L] - Xmean) * $
91                    (X[*, M:nT - 1L] - Xmean) $
92                    , TimeDim, Double = Double)
93      3:res = TOTAL((X[*, *, 0:nT - M - 1L] - Xmean) * $
94                    (X[*, *, M:nT - 1L] - Xmean) $
95                    , TimeDim, Double = Double)
96      4:res = TOTAL((X[*, *, *, 0:nT - M - 1L] - Xmean) * $
97                    (X[*, *, *, M:nT - 1L] - Xmean) $
98                    , TimeDim, Double = Double)
99   ENDCASE
100   if keyword_set(zero2nan) then begin
101      zero = where(res EQ 0)
102      if zero[0] NE -1 then res[zero] = !values.f_nan
103   endif
104   RETURN, res
105
106END
107
108FUNCTION A_TimeCorrelate, X, Lag, COVARIANCE = Covariance, DOUBLE = Double
109
110;Compute the sample-autocorrelation or autocovariance of (Xt, Xt+l)
111;as a function of the lag (l).
112
113   ON_ERROR, 2
114
115   XDim = SIZE(X, /dimensions)
116   XNDim = SIZE(X, /n_dimensions)
117   nT = XDim[XNDim-1]
118                                ;Check length.
119   if nT lt 2 then $
120    MESSAGE, "Time axis of X array must contain 2 or more elements."
121   
122;If the DOUBLE keyword is not set then the internal precision and
123;result are identical to the type of input.
124   if N_ELEMENTS(Double) eq 0 then $
125    Double = (SIZE(X, /type) eq 5)
126
127   
128   if n_elements(lag) EQ 0 then lag = 0
129   nLag = N_ELEMENTS(Lag)
130
131   if nLag eq 1 then Lag = [Lag] ;Create a 1-element vector.
132
133   case XNDim of
134      1:if Double eq 0 then Auto = FLTARR(nLag) else Auto = DBLARR(nLag)
135      2:if Double eq 0 then Auto = FLTARR(XDim[0], nLag) else Auto = DBLARR(XDim[0], nLag)
136      3:if Double eq 0 then Auto = FLTARR(XDim[0], XDim[1], nLag) $
137      else Auto = DBLARR(XDim[0], XDim[1], nLag)
138      4:if Double eq 0 then Auto = FLTARR(XDim[0], XDim[1], XDim[2], nLag) $
139      else Auto = DBLARR(XDim[0], XDim[1], XDim[2], nLag)
140   endcase
141
142   if KEYWORD_SET(Covariance) eq 0 then begin ;Compute Autocorrelation.
143      for k = 0, nLag-1 do $
144       case XNDim of
145         1:Auto[k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = Double) / $
146          TimeAuto_Cov(X, 0L, nT, Double = Double, /zero2nan)
147         2:Auto[*, k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = Double) / $
148          TimeAuto_Cov(X, 0L, nT, Double = Double, /zero2nan)
149         3:Auto[*, *, k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = Double) / $
150          TimeAuto_Cov(X, 0L, nT, Double = Double, /zero2nan)
151         4:Auto[*, *, *, k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = Double) / $
152          TimeAuto_Cov(X, 0L, nT, Double = Double, /zero2nan)
153      endcase
154   endif else begin             ;Compute Autocovariance.
155      for k = 0, nLag-1 do $
156       case XNDim of
157         1:Auto[k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = Double) / nT
158         2:Auto[*, k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = Double) / nT
159         3:Auto[*, *, k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = Double) / nT
160         4:Auto[*, *, *, k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = Double) / nT
161      endcase
162   endelse
163
164   if Double eq 0 then RETURN, FLOAT(Auto) else $
165    RETURN, Auto
166
167END
Note: See TracBrowser for help on using the repository browser.