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

Last change on this file since 325 was 325, checked in by pinsard, 17 years ago

modification of some headers (+some corrections) to prepare usage of the new idldoc

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