[335] | 1 | ;+ |
---|
| 2 | ; |
---|
| 3 | ; @file_comments |
---|
| 4 | ; |
---|
| 5 | ; @categories |
---|
| 6 | ; Statistics |
---|
| 7 | ; |
---|
| 8 | ; @param X {in}{required}{type=array} |
---|
[378] | 9 | ; An array which last dimension is the time dimension so size n. |
---|
[335] | 10 | ; |
---|
| 11 | ; @param NT |
---|
| 12 | ; |
---|
| 13 | ; @keyword DOUBLE |
---|
| 14 | ; If set to a non-zero value, computations are done in |
---|
| 15 | ; double precision arithmetic. |
---|
| 16 | ; |
---|
[378] | 17 | ; @keyword NAN |
---|
| 18 | ; |
---|
[335] | 19 | ; @hidden |
---|
| 20 | ; |
---|
| 21 | ; @version |
---|
| 22 | ; $Id$ |
---|
| 23 | ; |
---|
| 24 | ;- |
---|
[378] | 25 | FUNCTION skewness_num, x, nt, DOUBLE = double, NAN = nan |
---|
[335] | 26 | ; Compute the numerator of the skewness expression |
---|
| 27 | ; |
---|
| 28 | |
---|
| 29 | compile_opt idl2, strictarrsubs |
---|
| 30 | ; |
---|
| 31 | TimeDim = size(X, /n_dimensions) |
---|
| 32 | Xmean = NAN ? TOTAL(X, TimeDim, Double = Double, NAN = nan) / TOTAL(FINITE(X), TimeDim) : $ |
---|
| 33 | TOTAL(X, TimeDim, Double = Double) / nT |
---|
| 34 | one = double ? 1.0d : 1.0 |
---|
| 35 | Xmean = Xmean[*]#replicate(one, nT) |
---|
| 36 | res = TOTAL( (X-Xmean)^3, TimeDim, Double = Double, NAN = nan) |
---|
| 37 | |
---|
| 38 | RETURN, res |
---|
| 39 | |
---|
| 40 | END |
---|
| 41 | ;+ |
---|
| 42 | ; @file_comments |
---|
| 43 | ; Same function as SKEWNESS but accept array (until 4 |
---|
| 44 | ; dimension) for input and perform the skewness |
---|
| 45 | ; along the time dimension which must be the last |
---|
| 46 | ; one of the input array. |
---|
| 47 | ; |
---|
| 48 | ; @categories |
---|
| 49 | ; Statistics |
---|
| 50 | ; |
---|
| 51 | ; @param X {in}{required}{type=array} |
---|
[378] | 52 | ; An array which last dimension is the time dimension so size n. |
---|
[335] | 53 | ; |
---|
| 54 | ; @keyword DOUBLE |
---|
| 55 | ; If set to a non-zero value, computations are done in |
---|
| 56 | ; double precision arithmetic. |
---|
| 57 | ; |
---|
| 58 | ; @keyword NVAL |
---|
| 59 | ; A named variable that, on exit, contains the number of valid |
---|
| 60 | ; observations (not NAN) |
---|
| 61 | ; |
---|
| 62 | ; @examples |
---|
| 63 | ; |
---|
| 64 | ; @history |
---|
| 65 | ; May 2007 Michel Kolasinski (michel.kolasinski@locean-ipsl.upmc.fr) |
---|
| 66 | ; |
---|
| 67 | ; Based on the a_timecorrelate procedure of IDL |
---|
| 68 | ; INTRODUCTION TO STATISTICAL TIME SERIES |
---|
| 69 | ; Wayne A. Fuller |
---|
| 70 | ; ISBN 0-471-28715-6 |
---|
| 71 | ; |
---|
| 72 | ; @version |
---|
| 73 | ; $Id$ |
---|
| 74 | ; |
---|
| 75 | ;- |
---|
| 76 | |
---|
[378] | 77 | FUNCTION skewness_4d, x, DOUBLE = double, NVAL = nval |
---|
[335] | 78 | ; |
---|
| 79 | compile_opt idl2, strictarrsubs |
---|
| 80 | ; |
---|
| 81 | ; Compute the skewness from 1d to 4d vectors |
---|
| 82 | |
---|
| 83 | ON_ERROR, 2 |
---|
| 84 | |
---|
| 85 | XDim = SIZE(X, /dimensions) |
---|
| 86 | XNDim = SIZE(X, /n_dimensions) |
---|
| 87 | nT = XDim[XNDim-1] |
---|
| 88 | |
---|
| 89 | ; Keyword NAN activated if needed |
---|
| 90 | ; Keyword NVAL not compulsory. |
---|
| 91 | |
---|
| 92 | NAN = ( (WHERE(FINITE(X) EQ 0 ))[0] NE -1 ) ? 1 : 0 |
---|
| 93 | ;We can retrieve the matrix of real lenghts of time-series |
---|
| 94 | nTreal = ( (WHERE(FINITE(X) EQ 0 ))[0] NE -1 ) ? TOTAL(FINITE(X), XNDim) : nT |
---|
| 95 | |
---|
| 96 | IF ARG_PRESENT(NVAL) THEN nval = nTreal |
---|
| 97 | |
---|
| 98 | ; Check length. |
---|
| 99 | IF (WHERE(nTreal LE 1))[0] NE -1 THEN $ |
---|
| 100 | MESSAGE, "Matrix of length of time-series must contain 2 or more elements" |
---|
| 101 | |
---|
| 102 | ; If the DOUBLE keyword is not set then the internal precision and |
---|
| 103 | ; result are identical to the type of input. |
---|
| 104 | type = SIZE(X, /TYPE) |
---|
| 105 | useDouble = (N_ELEMENTS(Double) eq 1) ? KEYWORD_SET(Double) : $ |
---|
| 106 | (type eq 5) |
---|
| 107 | |
---|
| 108 | ; Type of outputs according to the type of data input |
---|
| 109 | case XNDim of |
---|
| 110 | 1: Skew = useDouble ? DBLARR(1) : FLTARR(1) |
---|
| 111 | 2: Skew = useDouble ? DBLARR(XDim[0]) : FLTARR(XDim[0]) |
---|
| 112 | 3: Skew = useDouble ? DBLARR(XDim[0], XDim[1]) : FLTARR(XDim[0], XDim[1]) |
---|
| 113 | 4: Skew = useDouble ? DBLARR(XDim[0], XDim[1], XDim[2]) : FLTARR(XDim[0], XDim[1], XDim[2]) |
---|
| 114 | endcase |
---|
| 115 | ; Compute standard deviation |
---|
| 116 | ; nTreal might be a matrix |
---|
| 117 | std = a_timecorrelate(X, 0, /covariance) |
---|
| 118 | std = sqrt(std) |
---|
| 119 | zero = where(std EQ 0) |
---|
| 120 | |
---|
| 121 | if zero[0] NE -1 then STOP, $ |
---|
| 122 | 'Cannot compute skewness since there are zeros in the matrix of standard deviations !' |
---|
| 123 | |
---|
| 124 | ; Problem with high masked values (x^3 makes NaN when x is high) |
---|
| 125 | ; Threshold put on the values of the tab |
---|
| 126 | idx_std = WHERE (std GT 1.0e+10) |
---|
| 127 | X = X < 1.0e+10 |
---|
| 128 | std = std < 1.0e+10 |
---|
| 129 | |
---|
| 130 | ; Compute skewness |
---|
| 131 | Skew = Skewness_Num(X, nT, Double = useDouble, NAN = nan) / (nTreal*std^3) |
---|
| 132 | IF idx_std[0] NE -1 THEN Skew[idx_std] = valmask |
---|
| 133 | |
---|
| 134 | return, useDouble ? Skew : FLOAT(Skew) |
---|
| 135 | |
---|
| 136 | END |
---|
| 137 | |
---|