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

Last change on this file since 163 was 163, checked in by navarro, 18 years ago

header improvements : type of parameters and keywords, default values, spell checking + idldoc assistant (IDL online_help)

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