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 | ;- |
---|
30 | FUNCTION 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 | |
---|
60 | END |
---|
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 | ;- |
---|
115 | ; |
---|
116 | FUNCTION a_timecorrelate, x, lag, COVARIANCE = covariance, DOUBLE = double |
---|
117 | ; |
---|
118 | compile_opt idl2, strictarrsubs |
---|
119 | ; |
---|
120 | |
---|
121 | ;Compute the sample-autocorrelation or autocovariance of (Xt, Xt+l) |
---|
122 | ;as a function of the lag (l). |
---|
123 | |
---|
124 | ON_ERROR, 2 |
---|
125 | |
---|
126 | XDim = SIZE(X, /dimensions) |
---|
127 | XNDim = SIZE(X, /n_dimensions) |
---|
128 | nT = XDim[XNDim-1] |
---|
129 | ;Check length. |
---|
130 | if nT lt 2 then $ |
---|
131 | ras= report("Time axis of X array must contain 2 or more elements.") |
---|
132 | |
---|
133 | ;If the DOUBLE keyword is not set then the internal precision and |
---|
134 | ;result are identical to the type of input. |
---|
135 | if N_ELEMENTS(Double) eq 0 then $ |
---|
136 | Double = (SIZE(X, /type) eq 5) |
---|
137 | |
---|
138 | |
---|
139 | if n_elements(lag) EQ 0 then lag = 0 |
---|
140 | nLag = N_ELEMENTS(Lag) |
---|
141 | |
---|
142 | if nLag eq 1 then Lag = [Lag] ;Create a 1-element vector. |
---|
143 | |
---|
144 | case XNDim of |
---|
145 | 1:if Double eq 0 then Auto = FLTARR(nLag) else Auto = DBLARR(nLag) |
---|
146 | 2:if Double eq 0 then Auto = FLTARR(XDim[0], nLag) else Auto = DBLARR(XDim[0], nLag) |
---|
147 | 3:if Double eq 0 then Auto = FLTARR(XDim[0], XDim[1], nLag) $ |
---|
148 | else Auto = DBLARR(XDim[0], XDim[1], nLag) |
---|
149 | 4:if Double eq 0 then Auto = FLTARR(XDim[0], XDim[1], XDim[2], nLag) $ |
---|
150 | else Auto = DBLARR(XDim[0], XDim[1], XDim[2], nLag) |
---|
151 | endcase |
---|
152 | |
---|
153 | if KEYWORD_SET(Covariance) eq 0 then begin ;Compute Autocorrelation. |
---|
154 | for k = 0, nLag-1 do $ |
---|
155 | case XNDim of |
---|
156 | 1:Auto[k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = Double) / $ |
---|
157 | TimeAuto_Cov(X, 0L, nT, Double = Double, /zero2nan) |
---|
158 | 2:Auto[*, k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = Double) / $ |
---|
159 | TimeAuto_Cov(X, 0L, nT, Double = Double, /zero2nan) |
---|
160 | 3:Auto[*, *, k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = Double) / $ |
---|
161 | TimeAuto_Cov(X, 0L, nT, Double = Double, /zero2nan) |
---|
162 | 4:Auto[*, *, *, k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = Double) / $ |
---|
163 | TimeAuto_Cov(X, 0L, nT, Double = Double, /zero2nan) |
---|
164 | endcase |
---|
165 | endif else begin ;Compute Autocovariance. |
---|
166 | for k = 0, nLag-1 do $ |
---|
167 | case XNDim of |
---|
168 | 1:Auto[k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = Double) / nT |
---|
169 | 2:Auto[*, k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = Double) / nT |
---|
170 | 3:Auto[*, *, k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = Double) / nT |
---|
171 | 4:Auto[*, *, *, k] = TimeAuto_Cov(X, ABS(Lag[k]), nT, Double = Double) / nT |
---|
172 | endcase |
---|
173 | endelse |
---|
174 | |
---|
175 | if Double eq 0 then RETURN, FLOAT(Auto) else $ |
---|
176 | RETURN, Auto |
---|
177 | |
---|
178 | END |
---|