source: trunk/SRC/ToBeReviewed/STATISTICS/a_timecorrelate.pro @ 335

Last change on this file since 335 was 335, checked in by smasson, 16 years ago

add Michel developments

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.0 KB
Line 
1;+
2;
3; @file_comments
4;
5; @categories
6; Statistics
7;
8; @param X {in}{required}{type=array}
9; An array which last dimension is the time dimension so
10; size n.
11;
12; @param M
13;
14; @param NT
15;
16; @keyword ZERO2NAN
17;
18; @keyword NAN
19;
20; @keyword DOUBLE
21; If set to a non-zero value, computations are done in
22; double precision arithmetic.
23;
24; @hidden
25;
26; @version
27; $Id$
28;
29;-
30FUNCTION timeauto_cov, x, m, nt, DOUBLE=double, ZERO2NAN=zero2nan, NAN =  nan
31;Sample autocovariance function
32;
33  compile_opt idl2, strictarrsubs
34;
35  IF NAN AND M GE 1 THEN $
36   STOP, 'Warning : lagged autocorrelation is not possible at the moment for time-series with NaN !!!'
37
38  TimeDim = size(X, /n_dimensions)
39  Xmean = NAN ? TOTAL(X, TimeDim, Double = Double,  NAN =  nan) / TOTAL(FINITE(X), TimeDim) : $
40   TOTAL(X, TimeDim, Double = Double) / nT                                                                               
41  one = double ? 1.0d : 1.0
42  Xmean = Xmean[*]#replicate(one, nT - M)
43
44; Time-series with NaN : only for Lag = 0
45  case TimeDim of
46     1:res = TOTAL((X[0:nT - M - 1L] - Xmean) * (X[M:nT - 1L] - Xmean), $
47                   TimeDim, Double = Double, NAN =  nan)
48     2:res = TOTAL((X[*, 0:nT - M - 1L] - Xmean) * $
49                   (X[*, M:nT - 1L] - Xmean[*]) $
50                   , TimeDim, Double = Double, NAN =  nan)
51     3:res = TOTAL((X[*, *, 0:nT - M - 1L] - Xmean[*, *]) * $
52                   (X[*, *, M:nT - 1L] - Xmean) $
53                   , TimeDim, Double = Double, NAN =  nan)
54     4:res = TOTAL((X[*, *, *, 0:nT - M - 1L] - Xmean) * $
55                   (X[*, *, *, M:nT - 1L] - Xmean) $
56                   , TimeDim, Double = Double,  NAN =  nan)
57  ENDCASE
58  if keyword_set(zero2nan) then begin
59     zero = where(res EQ 0)
60     if zero[0] NE -1 then res[zero] = !values.f_nan
61  endif
62  RETURN, res
63
64END
65;+
66;
67; @file_comments
68; Same function as <proidl>A_CORRELATE</proidl> but accept array (until 4
69; dimension) for input and do the autocorrelation or the
70; autocovariance along the time dimension which must be the last
71; one of the input array.
72;
73; This function computes the autocorrelation Px(L) or autocovariance
74; Rx(L) of a sample population X as a function of the lag (L).
75;
76; @categories
77; Statistics
78;
79; @param X {in}{required}{type=array}
80; An array which last dimension is the time dimension so
81; size n.
82;
83; @param LAG {in}{required}{type=scalar or vector}
84; A scalar or n-element vector, in the interval [-(n-2), (n-2)],
85; of type integer that specifies the absolute distance(s) between
86; indexed elements of X.
87;
88; @keyword COVARIANCE
89; If set to a non-zero value, the sample autocovariance
90; is computed.
91;
92; @keyword NVAL
93; A named variable that, on exit, contains the number of valid
94; observations (not NAN)
95;
96; @keyword DOUBLE
97; If set to a non-zero value, computations are done in
98; double precision arithmetic.
99;
100; @examples
101; Define an n-element sample population.
102; IDL> x = [3.73, 3.67, 3.77, 3.83, 4.67, 5.87, 6.70, 6.97, 6.40, 5.57]
103;
104; Compute the autocorrelation of X for LAG = -3, 0, 1, 3, 4, 8
105; IDL> lag = [-3, 0, 1, 3, 4, 8]
106; IDL> result = a_correlate(x, lag)
107;
108; The result should be:
109; [0.0146185, 1.00000, 0.810879, 0.0146185, -0.325279, -0.151684]
110;
111; @history
112; 24/2/2000 Sebastien Masson (smasson\@lodyc.jussieu.fr)
113;
114; Based on the A_CORRELATE procedure of IDL
115; INTRODUCTION TO STATISTICAL TIME SERIES
116; Wayne A. Fuller
117; ISBN 0-471-28715-6
118;
119; @version
120; $Id$
121;
122;-
123FUNCTION a_timecorrelate, x, lag, COVARIANCE=covariance, DOUBLE=double, NVAL =  nval
124;
125  compile_opt idl2, strictarrsubs
126;
127
128; Compute the sample-autocorrelation or autocovariance of (Xt, Xt+l)
129; as a function of the lag (l).
130
131   ON_ERROR, 2
132
133   XDim = SIZE(X, /dimensions)
134   XNDim = SIZE(X, /n_dimensions)
135   nT = XDim[XNDim-1]
136
137; Keyword NAN activated if needed for TimeAuto_Cov function
138; Keyword NVAL not compulsory.
139
140   NAN = ( (WHERE(FINITE(X) EQ 0 ))[0] NE -1 ) ? 1 : 0
141;We can retrieve the matrix of real lenghts of time-series   
142   nTreal = ( (WHERE(FINITE(X) EQ 0 ))[0] NE -1 ) ?  TOTAL(FINITE(X),  XNDim) : nT
143
144   IF ARG_PRESENT(NVAL) THEN nval = nTreal
145
146;Check length.
147   IF (WHERE(nTreal LE 1))[0] NE -1 THEN $
148    MESSAGE,  "Matrix of length of time-series must contain 2 or more elements"   
149
150;If the DOUBLE keyword is not set then the internal precision and
151;result are identical to the type of input.
152   type = SIZE(X, /TYPE)
153   useDouble = (N_ELEMENTS(Double) eq 1) ? KEYWORD_SET(Double) : (type eq 5)
154
155   if n_elements(lag) EQ 0 then lag = 0
156   nLag = N_ELEMENTS(Lag)
157
158   if nLag eq 1 then Lag = [Lag] ;Create a 1-element vector.
159
160; Type of outputs according to the type of data input
161
162   case XNDim of
163      1: Auto = useDouble ? DBLARR(nLag) : FLTARR(nLag)
164      2: Auto = useDouble ? DBLARR(XDim[0], nLag) : FLTARR(XDim[0], nLag)
165      3: Auto = useDouble ? DBLARR(XDim[0], XDim[1], nLag) : FLTARR(XDim[0], XDim[1], nLag)
166      4: Auto = useDouble ? DBLARR(XDim[0], XDim[1], XDim[2], nLag) : FLTARR(XDim[0], XDim[1], XDim[2], nLag)
167   endcase
168
169; Compute lagged autocorrelation or autocovariance (no NaN)
170   FOR k = 0, nLag-1 DO BEGIN
171      case XNDim of
172         1: BEGIN
173            Auto[k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = useDouble, NAN = nan) / $
174             ( KEYWORD_SET(Covariance) ?  nTreal : TimeAuto_Cov(X, 0L, nT, Double = useDouble,  /zero2nan, NAN = nan) )
175         END
176         2: BEGIN
177            Auto[*, k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = useDouble, NAN = nan) / $
178             ( KEYWORD_SET(Covariance) ?  nTreal : TimeAuto_Cov(X, 0L, nT, Double = useDouble, /zero2nan, NAN = nan) )   
179         END
180         3: BEGIN
181            Auto[*, *, k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = useDouble, NAN = nan) / $
182             ( KEYWORD_SET(Covariance) ?  nTreal : TimeAuto_Cov(X, 0L, nT, Double = useDouble, /zero2nan, NAN = nan) )
183         END
184         4: BEGIN
185            Auto[*, *, *, k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = useDouble, NAN = nan) / $
186             ( KEYWORD_SET(Covariance) ?  nTreal : TimeAuto_Cov(X, 0L, nT, Double = useDouble, /zero2nan, NAN = nan) )
187         END
188      ENDCASE
189   ENDFOR
190
191   return, useDouble ? Auto : FLOAT(Auto)
192
193END
Note: See TracBrowser for help on using the repository browser.