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