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

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

improvements/corrections of some *.pro headers

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