[157] | 1 | ;+ |
---|
[232] | 2 | ; |
---|
[157] | 3 | ; @file_comments |
---|
| 4 | ; |
---|
| 5 | ; @categories |
---|
| 6 | ; Statistics |
---|
| 7 | ; |
---|
| 8 | ; @param XD |
---|
| 9 | ; |
---|
| 10 | ; @param YD |
---|
| 11 | ; |
---|
| 12 | ; @param M |
---|
| 13 | ; |
---|
| 14 | ; @param NT |
---|
| 15 | ; |
---|
| 16 | ; @param NDIM |
---|
| 17 | ; |
---|
| 18 | ; @keyword ZERO2NAN |
---|
| 19 | ; |
---|
| 20 | ; @keyword DOUBLE |
---|
| 21 | ; If set to a non-zero value, computations are done in |
---|
| 22 | ; double precision arithmetic. |
---|
| 23 | ; |
---|
| 24 | ; @examples |
---|
| 25 | ; |
---|
| 26 | ; @history |
---|
| 27 | ; |
---|
| 28 | ; @version |
---|
| 29 | ; $Id$ |
---|
| 30 | ; |
---|
| 31 | ;- |
---|
[327] | 32 | FUNCTION timecross_cov, xd, yd, m, nt, ndim, DOUBLE=double, ZERO2NAN=zero2nan |
---|
[242] | 33 | ; |
---|
[150] | 34 | compile_opt hidden |
---|
[2] | 35 | ; |
---|
[242] | 36 | ;Sample cross covariance function. |
---|
[150] | 37 | case Ndim OF |
---|
| 38 | 1:res = TOTAL(Xd[0:nT - M - 1L] * Yd[M:nT - 1L] $ |
---|
| 39 | , Double = Double) |
---|
| 40 | 2:res = TOTAL(Xd[*, 0:nT - M - 1L] * Yd[*, M:nT - 1L] $ |
---|
| 41 | , Ndim, Double = Double) |
---|
| 42 | 3:res = TOTAL(Xd[*, *, 0:nT - M - 1L] * Yd[*, *, M:nT - 1L] $ |
---|
| 43 | , Ndim, Double = Double) |
---|
| 44 | 4:res = TOTAL(Xd[*, *, *, 0:nT - M - 1L] * Yd[*, *, *, M:nT - 1L] $ |
---|
| 45 | , Ndim, Double = Double) |
---|
| 46 | ENDCASE |
---|
| 47 | if keyword_set(zero2nan) then begin |
---|
| 48 | zero = where(res EQ 0) |
---|
| 49 | if zero[0] NE -1 then res[zero] = !values.f_nan |
---|
| 50 | ENDIF |
---|
[2] | 51 | ; |
---|
[150] | 52 | RETURN, res |
---|
| 53 | |
---|
| 54 | END |
---|
[242] | 55 | ; |
---|
[150] | 56 | ;+ |
---|
[242] | 57 | ; |
---|
[150] | 58 | ; @file_comments |
---|
| 59 | ; This function computes the "time cross correlation" Pxy(L) or |
---|
| 60 | ; the "time cross covariance" between 2 arrays (this is some |
---|
[327] | 61 | ; kind of <proidl>C_CORRELATE</proidl> but for multidimensional arrays) as a |
---|
[150] | 62 | ; function of the lag (L). |
---|
[2] | 63 | ; |
---|
[150] | 64 | ; @categories |
---|
[157] | 65 | ; Statistics |
---|
[2] | 66 | ; |
---|
[242] | 67 | ; @param X {in}{required} {type=array} |
---|
| 68 | ; An array which last dimension is the time dimension of |
---|
[150] | 69 | ; size n, float or double. |
---|
[2] | 70 | ; |
---|
[242] | 71 | ; @param Y {in}{required} {type=array} |
---|
| 72 | ; An array which last dimension is the time dimension of |
---|
[150] | 73 | ; size n, float or double. |
---|
[2] | 74 | ; |
---|
[163] | 75 | ; @param LAG {in}{required}{type=scalar or vector} |
---|
[242] | 76 | ; A scalar or n-elements vector, in the interval [-(n-2),(n-2)], |
---|
[150] | 77 | ; of type integer that specifies the absolute distance(s) between |
---|
| 78 | ; indexed elements of X. |
---|
[2] | 79 | ; |
---|
[150] | 80 | ; @keyword COVARIANCE |
---|
[226] | 81 | ; If set to a non-zero value, the sample cross |
---|
[150] | 82 | ; covariance is computed. |
---|
[2] | 83 | ; |
---|
[150] | 84 | ; @keyword DOUBLE |
---|
| 85 | ; If set to a non-zero value, computations are done in |
---|
| 86 | ; double precision arithmetic. |
---|
[2] | 87 | ; |
---|
[150] | 88 | ; @examples |
---|
[226] | 89 | ; |
---|
[242] | 90 | ; Define two n-elements sample populations. |
---|
[371] | 91 | ; IDL> x = [3.73, 3.67, 3.77, 3.83, 4.67, 5.87, 6.70, 6.97, 6.40, 5.57] |
---|
| 92 | ; IDL> y = [2.31, 2.76, 3.02, 3.13, 3.72, 3.88, 3.97, 4.39, 4.34, 3.95] |
---|
[2] | 93 | ; |
---|
[242] | 94 | ; Compute the cross correlation of X and Y for LAG = -5, 0, 1, 5, 6, 7 |
---|
[371] | 95 | ; IDL> lag = [-5, 0, 1, 5, 6, 7] |
---|
| 96 | ; IDL> result = c_timecorrelate(x, y, lag) |
---|
[2] | 97 | ; |
---|
[242] | 98 | ; The result should be: |
---|
| 99 | ; [-0.428246, 0.914755, 0.674547, -0.405140, -0.403100, -0.339685] |
---|
[2] | 100 | ; |
---|
[150] | 101 | ; @history |
---|
[157] | 102 | ; - 01/03/2000 Sebastien Masson (smasson\@lodyc.jussieu.fr) |
---|
[2] | 103 | ; Based on the C_CORRELATE procedure of IDL |
---|
[226] | 104 | ; - August 2003 Sebastien Masson |
---|
[21] | 105 | ; update according to the update made in C_CORRELATE by |
---|
| 106 | ; W. Biagiotti and available in IDL 5.5 |
---|
[2] | 107 | ; |
---|
[150] | 108 | ; INTRODUCTION TO STATISTICAL TIME SERIES |
---|
| 109 | ; Wayne A. Fuller |
---|
| 110 | ; ISBN 0-471-28715-6 |
---|
[2] | 111 | ; |
---|
[150] | 112 | ; @version |
---|
| 113 | ; $Id$ |
---|
| 114 | ; |
---|
| 115 | ;- |
---|
[327] | 116 | FUNCTION c_timecorrelate, x, y, lag, COVARIANCE=covariance, DOUBLE=double |
---|
[242] | 117 | ; |
---|
[2] | 118 | |
---|
[226] | 119 | ;Compute the sample cross correlation or cross covariance of |
---|
[2] | 120 | ;(Xt, Xt+l) and (Yt, Yt+l) as a function of the lag (l). |
---|
| 121 | |
---|
| 122 | ON_ERROR, 2 |
---|
| 123 | |
---|
| 124 | xsize = SIZE(X) |
---|
| 125 | ysize = SIZE(Y) |
---|
| 126 | nt = float(xsize[xsize[0]]) |
---|
| 127 | NDim = xsize[0] |
---|
| 128 | |
---|
| 129 | if total(xsize[0:xsize[0]] NE ysize[0:ysize[0]]) NE 0 then $ |
---|
[242] | 130 | ras = report("X and Y arrays must have the same size and the same dimensions") |
---|
[2] | 131 | |
---|
| 132 | ;Check length. |
---|
| 133 | if nt lt 2 then $ |
---|
[242] | 134 | ras = report("Time dimension of X and Y arrays must contain 2 or more elements.") |
---|
[226] | 135 | |
---|
[2] | 136 | ;If the DOUBLE keyword is not set then the internal precision and |
---|
| 137 | ;result are identical to the type of input. |
---|
| 138 | if N_ELEMENTS(Double) eq 0 then $ |
---|
| 139 | Double = (Xsize[Xsize[0]+1] eq 5 or ysize[ysize[0]+1] eq 5) |
---|
| 140 | |
---|
| 141 | if n_elements(lag) EQ 0 then lag = 0 |
---|
| 142 | nLag = N_ELEMENTS(Lag) |
---|
| 143 | |
---|
[21] | 144 | ;Deviations |
---|
| 145 | if double then one = 1.0d ELSE one = 1.0 |
---|
| 146 | Ndim = size(X, /n_dimensions) |
---|
| 147 | Xd = TOTAL(X, Ndim, Double = Double) / nT |
---|
| 148 | Xd = X - Xd[*]#replicate(one, nT) |
---|
| 149 | Yd = TOTAL(Y, Ndim, Double = Double) / nT |
---|
| 150 | Yd = Y - Yd[*]#replicate(one, nT) |
---|
| 151 | |
---|
[2] | 152 | if nLag eq 1 then Lag = [Lag] ;Create a 1-element vector. |
---|
| 153 | |
---|
| 154 | case NDim of |
---|
[21] | 155 | 1:if Double eq 0 then Cross = FLTARR(nLag) else Cross = DBLARR(nLag) |
---|
| 156 | 2:if Double eq 0 then Cross = FLTARR(Xsize[1], nLag) else Cross = DBLARR(Xsize[1], nLag) |
---|
| 157 | 3:if Double eq 0 then Cross = FLTARR(Xsize[1], Xsize[2], nLag) $ |
---|
| 158 | else Cross = DBLARR(Xsize[1], Xsize[2], nLag) |
---|
| 159 | 4:if Double eq 0 then Cross = FLTARR(Xsize[1], Xsize[2], Xsize[3], nLag) $ |
---|
| 160 | else Cross = DBLARR(Xsize[1], Xsize[2], Xsize[3], nLag) |
---|
[2] | 161 | endcase |
---|
| 162 | |
---|
[21] | 163 | if KEYWORD_SET(Covariance) eq 0 then begin ;Compute Cross Crossation. |
---|
[2] | 164 | for k = 0, nLag-1 do begin |
---|
[226] | 165 | if Lag[k] ge 0 then BEGIN |
---|
[2] | 166 | case NDim of |
---|
[21] | 167 | 1: Cross[k] = TimeCross_Cov(Xd, Yd, Lag[k], nT, Ndim, Double = Double) |
---|
| 168 | 2: Cross[*, k] = TimeCross_Cov(Xd, Yd, Lag[k], nT, Ndim, Double = Double) |
---|
| 169 | 3: Cross[*, *, k] = TimeCross_Cov(Xd, Yd, Lag[k], nT, Ndim, Double = Double) |
---|
[226] | 170 | 4: Cross[*, *, *, k] = TimeCross_Cov(Xd, Yd, Lag[k], nT, Ndim, Double = Double) |
---|
[21] | 171 | endcase |
---|
[226] | 172 | ENDIF else BEGIN |
---|
[2] | 173 | case NDim of |
---|
[21] | 174 | 1: Cross[k] = TimeCross_Cov(Yd, Xd, ABS(Lag[k]), nT, Ndim, Double = Double) |
---|
| 175 | 2: Cross[*, k] = TimeCross_Cov(Yd, Xd, ABS(Lag[k]), nT, Ndim, Double = Double) |
---|
| 176 | 3: Cross[*, *, k] = TimeCross_Cov(Yd, Xd, ABS(Lag[k]), nT, Ndim, Double = Double) |
---|
| 177 | 4: Cross[*, *, *, k] = TimeCross_Cov(Yd, Xd, ABS(Lag[k]), nT, Ndim, Double = Double) |
---|
| 178 | endcase |
---|
[226] | 179 | ENDELSE |
---|
[21] | 180 | ENDFOR |
---|
| 181 | div = sqrt(TimeCross_Cov(Xd, Xd, 0L, nT, Ndim, Double = Double, /zero2nan) * $ |
---|
| 182 | TimeCross_Cov(Yd, Yd, 0L, nT, Ndim, Double = Double, /zero2nan)) |
---|
| 183 | Cross = temporary(Cross)/((temporary(div))[*]#replicate(one, nLag)) |
---|
[2] | 184 | endif else begin ;Compute Cross Covariance. |
---|
| 185 | for k = 0, nLag-1 do begin |
---|
| 186 | if Lag[k] ge 0 then BEGIN |
---|
| 187 | case NDim of |
---|
[21] | 188 | 1: Cross[k] = TimeCross_Cov(Xd, Yd, Lag[k], nT, Ndim, Double = Double) / nT |
---|
| 189 | 2: Cross[*, k] = TimeCross_Cov(Xd, Yd, Lag[k], nT, Ndim, Double = Double) / nT |
---|
| 190 | 3: Cross[*, *, k] = TimeCross_Cov(Xd, Yd, Lag[k], nT, Ndim, Double = Double) / nT |
---|
| 191 | 4: Cross[*, *, *, k] = TimeCross_Cov(Xd, Yd, Lag[k], nT, Ndim, Double = Double) / nT |
---|
[2] | 192 | ENDCASE |
---|
[226] | 193 | ENDIF else BEGIN |
---|
[2] | 194 | case NDim of |
---|
[21] | 195 | 1: Cross[k] = TimeCross_Cov(yd, xd, ABS(Lag[k]), nT, Ndim, Double = Double) / nT |
---|
| 196 | 2: Cross[*, k] = TimeCross_Cov(yd, xd, ABS(Lag[k]), nT, Ndim, Double = Double) / nT |
---|
| 197 | 3: Cross[*, *, k] = TimeCross_Cov(yd, xd, ABS(Lag[k]), nT, Ndim, Double = Double) / nT |
---|
| 198 | 4: Cross[*, *, *, k] = TimeCross_Cov(yd, xd, ABS(Lag[k]), nT, Ndim, Double = Double) / nT |
---|
[2] | 199 | ENDCASE |
---|
[226] | 200 | ENDELSE |
---|
[2] | 201 | endfor |
---|
| 202 | endelse |
---|
| 203 | |
---|
[21] | 204 | if Double eq 0 then RETURN, FLOAT(Cross) else RETURN, Cross |
---|
[2] | 205 | |
---|
| 206 | END |
---|