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

Last change on this file since 371 was 371, checked in by pinsard, 16 years ago

improvements of headers (alignments of IDL prompt in examples)

  • 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;
102; Define an n-element sample population.
103;   IDL> x = [3.73, 3.67, 3.77, 3.83, 4.67, 5.87, 6.70, 6.97, 6.40, 5.57]
104;
105; Compute the autocorrelation of X for LAG = -3, 0, 1, 3, 4, 8
106;   IDL> lag = [-3, 0, 1, 3, 4, 8]
107;   IDL> result = a_correlate(x, lag)
108;
109; The result should be:
110; [0.0146185, 1.00000, 0.810879, 0.0146185, -0.325279, -0.151684]
111;
112; @history
113; 24/2/2000 Sebastien Masson (smasson\@lodyc.jussieu.fr)
114;
115; Based on the A_CORRELATE procedure of IDL
116; INTRODUCTION TO STATISTICAL TIME SERIES
117; Wayne A. Fuller
118; ISBN 0-471-28715-6
119;
120; @version
121; $Id$
122;
123;-
124FUNCTION a_timecorrelate, x, lag, COVARIANCE=covariance, DOUBLE=double, NVAL =  nval
125;
126  compile_opt idl2, strictarrsubs
127;
128
129; Compute the sample-autocorrelation or autocovariance of (Xt, Xt+l)
130; as a function of the lag (l).
131
132   ON_ERROR, 2
133
134   XDim = SIZE(X, /dimensions)
135   XNDim = SIZE(X, /n_dimensions)
136   nT = XDim[XNDim-1]
137
138; Keyword NAN activated if needed for TimeAuto_Cov function
139; Keyword NVAL not compulsory.
140
141   NAN = ( (WHERE(FINITE(X) EQ 0 ))[0] NE -1 ) ? 1 : 0
142;We can retrieve the matrix of real lenghts of time-series   
143   nTreal = ( (WHERE(FINITE(X) EQ 0 ))[0] NE -1 ) ?  TOTAL(FINITE(X),  XNDim) : nT
144
145   IF ARG_PRESENT(NVAL) THEN nval = nTreal
146
147;Check length.
148   IF (WHERE(nTreal GT 1))[0] EQ -1 THEN $
149    MESSAGE,  "Matrix of length of time-series must contain 2 or more elements"   
150
151;If the DOUBLE keyword is not set then the internal precision and
152;result are identical to the type of input.
153   type = SIZE(X, /TYPE)
154   useDouble = (N_ELEMENTS(Double) eq 1) ? KEYWORD_SET(Double) : (type eq 5)
155
156   if n_elements(lag) EQ 0 then lag = 0
157   nLag = N_ELEMENTS(Lag)
158
159   if nLag eq 1 then Lag = [Lag] ;Create a 1-element vector.
160
161; Type of outputs according to the type of data input
162
163   case XNDim of
164      1: Auto = useDouble ? DBLARR(nLag) : FLTARR(nLag)
165      2: Auto = useDouble ? DBLARR(XDim[0], nLag) : FLTARR(XDim[0], nLag)
166      3: Auto = useDouble ? DBLARR(XDim[0], XDim[1], nLag) : FLTARR(XDim[0], XDim[1], nLag)
167      4: Auto = useDouble ? DBLARR(XDim[0], XDim[1], XDim[2], nLag) : FLTARR(XDim[0], XDim[1], XDim[2], nLag)
168   endcase
169
170; Compute lagged autocorrelation or autocovariance (no NaN)
171   FOR k = 0, nLag-1 DO BEGIN
172      case XNDim of
173         1: BEGIN
174            Auto[k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = useDouble, NAN = nan) / $
175             ( KEYWORD_SET(Covariance) ?  nTreal : TimeAuto_Cov(X, 0L, nT, Double = useDouble,  /zero2nan, NAN = nan) )
176         END
177         2: BEGIN
178            Auto[*, k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = useDouble, NAN = nan) / $
179             ( KEYWORD_SET(Covariance) ?  nTreal : TimeAuto_Cov(X, 0L, nT, Double = useDouble, /zero2nan, NAN = nan) )   
180         END
181         3: BEGIN
182            Auto[*, *, k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = useDouble, NAN = nan) / $
183             ( KEYWORD_SET(Covariance) ?  nTreal : TimeAuto_Cov(X, 0L, nT, Double = useDouble, /zero2nan, NAN = nan) )
184         END
185         4: BEGIN
186            Auto[*, *, *, k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = useDouble, NAN = nan) / $
187             ( KEYWORD_SET(Covariance) ?  nTreal : TimeAuto_Cov(X, 0L, nT, Double = useDouble, /zero2nan, NAN = nan) )
188         END
189      ENDCASE
190   ENDFOR
191
192   return, useDouble ? Auto : FLOAT(Auto)
193
194END
Note: See TracBrowser for help on using the repository browser.