def test_kurtosis(self): for n in self.get_n(): x, y, xm, ym = self.generate_xy_sample(n) r = stats.kurtosis(x) rm = stats.mstats.kurtosis(xm) assert_almost_equal(r, rm, 10) r = stats.kurtosis(y) rm = stats.mstats.kurtosis(ym) assert_almost_equal(r, rm, 10)
def test_kurtosis(self): # Set flags for axis = 0 and fisher=0 (Pearson's definition of kurtosis # for compatibility with Matlab) y = mstats.kurtosis(self.testmathworks, 0, fisher=0, bias=1) assert_almost_equal(y, 2.1658856802973, 10) # Note that MATLAB has confusing docs for the following case # kurtosis(x,0) gives an unbiased estimate of Pearson's skewness # kurtosis(x) gives a biased estimate of Fisher's skewness (Pearson-3) # The MATLAB docs imply that both should give Fisher's y = mstats.kurtosis(self.testmathworks, fisher=0, bias=0) assert_almost_equal(y, 3.663542721189047, 10) y = mstats.kurtosis(self.testcase, 0, 0) assert_almost_equal(y, 1.64) # test that kurtosis works on multidimensional masked arrays correct_2d = ma.array(np.array( [-1.5, -3., -1.47247052385, 0., -1.26979517952]), mask=np.array([False, False, False, True, False], dtype=np.bool)) assert_array_almost_equal(mstats.kurtosis(self.testcase_2d, 1), correct_2d) for i, row in enumerate(self.testcase_2d): assert_almost_equal(mstats.kurtosis(row), correct_2d[i]) correct_2d_bias_corrected = ma.array( np.array([-1.5, -3., -1.88988209538, 0., -0.5234638463918877]), mask=np.array([False, False, False, True, False], dtype=np.bool)) assert_array_almost_equal( mstats.kurtosis(self.testcase_2d, 1, bias=False), correct_2d_bias_corrected) for i, row in enumerate(self.testcase_2d): assert_almost_equal(mstats.kurtosis(row, bias=False), correct_2d_bias_corrected[i]) # Check consistency between stats and mstats implementations assert_array_almost_equal_nulp(mstats.kurtosis(self.testcase_2d[2, :]), stats.kurtosis(self.testcase_2d[2, :]))
def trdata(self, timeseries): ''' Returns ------- tr, tr_emp : TrData objects with the smoothed and empirical transformation, respectively. TRDATA estimates the transformation in a transformed Gaussian model. Assumption: a Gaussian process, Y, is related to the non-Gaussian process, X, by Y = g(X). The empirical crossing intensity is usually very irregular. More than one local maximum of the empirical crossing intensity may cause poor fit of the transformation. In such case one should use a smaller value of CSM. In order to check the effect of smoothing it is recomended to also plot g and g2 in the same plot or plot the smoothed g against an interpolated version of g (when CSM=GSM=1). Example ------- >>> import wafo.spectrum.models as sm >>> import wafo.transform.models as tm >>> from wafo.objects import mat2timeseries >>> Hs = 7.0 >>> Sj = sm.Jonswap(Hm0=Hs) >>> S = Sj.tospecdata() #Make spectrum object from numerical values >>> S.tr = tm.TrOchi(mean=0, skew=0.16, kurt=0, ... sigma=Hs/4, ysigma=Hs/4) >>> xs = S.sim(ns=2**16, iseed=10) >>> ts = mat2timeseries(xs) >>> g0, g0emp = ts.trdata(monitor=True) >>> g1, g1emp = ts.trdata(method='m', gvar=0.5 ) >>> g2, g2emp = ts.trdata(method='n', gvar=[3.5, 0.5, 3.5]) >>> int(S.tr.dist2gauss()*100) 141 >>> int(g0emp.dist2gauss()*100) 217949 >>> int(g0.dist2gauss()*100) 93 >>> int(g1.dist2gauss()*100) 66 >>> int(g2.dist2gauss()*100) 84 See also -------- LevelCrossings.trdata wafo.transform.models References ---------- Rychlik, I. , Johannesson, P and Leadbetter, M. R. (1997) "Modelling and statistical analysis of ocean wavedata using transformed Gaussian process." Marine structures, Design, Construction and Safety, Vol. 10, No. 1, pp 13--47 Brodtkorb, P, Myrhaug, D, and Rue, H (1999) "Joint distribution of wave height and crest velocity from reconstructed data" in Proceedings of 9th ISOPE Conference, Vol III, pp 66-73 ''' data = np.atleast_1d(timeseries.data) ma = data.mean() sa = data.std() method = self.method[0] if method == 'l': return TrLinear(mean=ma, sigma=sa), TrLinear(mean=ma, sigma=sa) if method == 'n': tp = timeseries.turning_points() mM = tp.cycle_pairs() lc = mM.level_crossings(self.crossdef) return self._trdata_lc(lc) elif method == 'm': return self._trdata_cdf(data) elif method == 'h': ga1 = skew(data) ga2 = kurtosis(data, fisher=True) # kurt(xx(n+1:end))-3; up = min(4 * (4 * ga1 / 3)**2, 13) lo = (ga1**2) * 3 / 2 kurt1 = min(up, max(ga2, lo)) + 3 return TrHermite(mean=ma, var=sa**2, skew=ga1, kurt=kurt1) elif method[0] == 'o': ga1 = skew(data) return TrOchi(mean=ma, var=sa**2, skew=ga1)
def trdata(self, timeseries): ''' Returns ------- tr, tr_emp : TrData objects with the smoothed and empirical transformation, respectively. TRDATA estimates the transformation in a transformed Gaussian model. Assumption: a Gaussian process, Y, is related to the non-Gaussian process, X, by Y = g(X). The empirical crossing intensity is usually very irregular. More than one local maximum of the empirical crossing intensity may cause poor fit of the transformation. In such case one should use a smaller value of CSM. In order to check the effect of smoothing it is recomended to also plot g and g2 in the same plot or plot the smoothed g against an interpolated version of g (when CSM=GSM=1). Example ------- >>> import wafo.spectrum.models as sm >>> import wafo.transform.models as tm >>> from wafo.objects import mat2timeseries >>> Hs = 7.0 >>> Sj = sm.Jonswap(Hm0=Hs) >>> S = Sj.tospecdata() #Make spectrum object from numerical values >>> S.tr = tm.TrOchi(mean=0, skew=0.16, kurt=0, ... sigma=Hs/4, ysigma=Hs/4) >>> xs = S.sim(ns=2**16, iseed=10) >>> ts = mat2timeseries(xs) >>> g0, g0emp = ts.trdata(monitor=True) >>> g1, g1emp = ts.trdata(method='m', gvar=0.5 ) >>> g2, g2emp = ts.trdata(method='n', gvar=[3.5, 0.5, 3.5]) >>> int(S.tr.dist2gauss()*100) 141 >>> int(g0emp.dist2gauss()*100) 217949 >>> int(g0.dist2gauss()*100) 93 >>> int(g1.dist2gauss()*100) 66 >>> int(g2.dist2gauss()*100) 84 See also -------- LevelCrossings.trdata wafo.transform.models References ---------- Rychlik, I. , Johannesson, P and Leadbetter, M. R. (1997) "Modelling and statistical analysis of ocean wavedata using transformed Gaussian process." Marine structures, Design, Construction and Safety, Vol. 10, No. 1, pp 13--47 Brodtkorb, P, Myrhaug, D, and Rue, H (1999) "Joint distribution of wave height and crest velocity from reconstructed data" in Proceedings of 9th ISOPE Conference, Vol III, pp 66-73 ''' data = np.atleast_1d(timeseries.data) ma = data.mean() sa = data.std() method = self.method[0] if method == 'l': return TrLinear(mean=ma, sigma=sa), TrLinear(mean=ma, sigma=sa) if method == 'n': tp = timeseries.turning_points() mM = tp.cycle_pairs() lc = mM.level_crossings(self.crossdef) return self._trdata_lc(lc) elif method == 'm': return self._trdata_cdf(data) elif method == 'h': ga1 = skew(data) ga2 = kurtosis(data, fisher=True) # kurt(xx(n+1:end))-3; up = min(4 * (4 * ga1 / 3) ** 2, 13) lo = (ga1 ** 2) * 3 / 2 kurt1 = min(up, max(ga2, lo)) + 3 return TrHermite(mean=ma, var=sa ** 2, skew=ga1, kurt=kurt1) elif method[0] == 'o': ga1 = skew(data) return TrOchi(mean=ma, var=sa ** 2, skew=ga1)
#! agrees much better with the estimated covariance function clf() R2 = S2.tocovdata(nr=1) R2.plot('.') Rest.plot() show() #! Section 2.2.2 Transformed Gaussian models #!------------------------------------------- #! We begin with computing skewness and kurtosis #! for the data set xx and compare it with the second order wave approximation #! proposed by Winterstein: import wafo.stats as ws rho3 = ws.skew(xx[:, 1]) rho4 = ws.kurtosis(xx[:, 1]) sk, ku = S1.stats_nl(moments='sk') #! Comparisons of 3 transformations clf() import wafo.transform.models as wtm gh = wtm.TrHermite(mean=me, sigma=sa, skew=sk, kurt=ku).trdata() g = wtm.TrLinear(mean=me, sigma=sa).trdata() # Linear transformation glc, gemp = lc.trdata(mean=me, sigma=sa) glc.plot('b-') #! Transf. estimated from level-crossings gh.plot('b-.') #! Hermite Transf. estimated from moments g.plot('r') grid('on') show()