pep8 + added some updates from scipy.stats to wafo.stats
parent
d308357c5b
commit
6aec932677
File diff suppressed because it is too large
Load Diff
@ -1,147 +1,158 @@
|
|||||||
'''
|
'''
|
||||||
Created on 17. juli 2010
|
Created on 17. juli 2010
|
||||||
|
|
||||||
@author: pab
|
@author: pab
|
||||||
'''
|
'''
|
||||||
import numpy as np #@UnusedImport
|
import numpy as np # @UnusedImport
|
||||||
from numpy import pi, inf #@UnusedImport
|
from numpy import pi, inf # @UnusedImport
|
||||||
from wafo.gaussian import Rind, prbnormtndpc, prbnormndpc, prbnormnd, cdfnorm2d, prbnorm2d #@UnusedImport
|
# @UnusedImport
|
||||||
|
from wafo.gaussian import (Rind, prbnormtndpc, prbnormndpc, prbnormnd,
|
||||||
def test_rind():
|
cdfnorm2d, prbnorm2d)
|
||||||
'''
|
|
||||||
>>> Et = 0.001946 # # exact prob.
|
|
||||||
>>> n = 5
|
def test_rind():
|
||||||
>>> Blo =-np.inf; Bup=-1.2; indI=[-1, n-1] # Barriers
|
'''
|
||||||
>>> m = np.zeros(n); rho = 0.3;
|
>>> Et = 0.001946 # # exact prob.
|
||||||
>>> Sc =(np.ones((n,n))-np.eye(n))*rho+np.eye(n)
|
>>> n = 5
|
||||||
>>> rind = Rind()
|
>>> Blo =-np.inf; Bup=-1.2; indI=[-1, n-1] # Barriers
|
||||||
>>> E0, err0, terr0 = rind(Sc,m,Blo,Bup,indI);
|
>>> m = np.zeros(n); rho = 0.3;
|
||||||
|
>>> Sc =(np.ones((n,n))-np.eye(n))*rho+np.eye(n)
|
||||||
>>> np.abs(E0-Et)< err0+terr0
|
>>> rind = Rind()
|
||||||
array([ True], dtype=bool)
|
>>> E0, err0, terr0 = rind(Sc,m,Blo,Bup,indI);
|
||||||
>>> 'E0 = %2.6f' % E0
|
|
||||||
'E0 = 0.001946'
|
>>> np.abs(E0-Et)< err0+terr0
|
||||||
|
array([ True], dtype=bool)
|
||||||
>>> A = np.repeat(Blo,n); B = np.repeat(Bup,n) # Integration limits
|
>>> 'E0 = %2.6f' % E0
|
||||||
>>> E1, err1, terr1 = rind(np.triu(Sc),m,A,B); #same as E0
|
'E0 = 0.001946'
|
||||||
>>> np.abs(E1-Et)< err0+terr0
|
|
||||||
array([ True], dtype=bool)
|
>>> A = np.repeat(Blo,n); B = np.repeat(Bup,n) # Integration limits
|
||||||
>>> 'E1 = %2.5f' % E1
|
>>> E1, err1, terr1 = rind(np.triu(Sc),m,A,B); #same as E0
|
||||||
'E1 = 0.00195'
|
>>> np.abs(E1-Et)< err0+terr0
|
||||||
|
array([ True], dtype=bool)
|
||||||
Compute expectation E( abs(X1*X2*...*X5) )
|
>>> 'E1 = %2.5f' % E1
|
||||||
>>> xc = np.zeros((0,1))
|
'E1 = 0.00195'
|
||||||
>>> infinity = 37
|
|
||||||
>>> dev = np.sqrt(np.diag(Sc)) # std
|
Compute expectation E( abs(X1*X2*...*X5) )
|
||||||
>>> ind = np.nonzero(indI[1:])[0]
|
>>> xc = np.zeros((0,1))
|
||||||
>>> Bup, Blo = np.atleast_2d(Bup,Blo)
|
>>> infinity = 37
|
||||||
>>> Bup[0,ind] = np.minimum(Bup[0,ind] , infinity*dev[indI[ind+1]])
|
>>> dev = np.sqrt(np.diag(Sc)) # std
|
||||||
>>> Blo[0,ind] = np.maximum(Blo[0,ind] ,-infinity*dev[indI[ind+1]])
|
>>> ind = np.nonzero(indI[1:])[0]
|
||||||
>>> rind(Sc,m,Blo,Bup,indI, xc, nt=0)
|
>>> Bup, Blo = np.atleast_2d(Bup,Blo)
|
||||||
(array([ 0.05494076]), array([ 0.00083066]), array([ 1.00000000e-10]))
|
>>> Bup[0,ind] = np.minimum(Bup[0,ind] , infinity*dev[indI[ind+1]])
|
||||||
|
>>> Blo[0,ind] = np.maximum(Blo[0,ind] ,-infinity*dev[indI[ind+1]])
|
||||||
Compute expectation E( X1^{+}*X2^{+} ) with random
|
>>> rind(Sc,m,Blo,Bup,indI, xc, nt=0)
|
||||||
correlation coefficient,Cov(X1,X2) = rho2.
|
(array([ 0.05494076]), array([ 0.00083066]), array([ 1.00000000e-10]))
|
||||||
>>> m2 = [0, 0];
|
|
||||||
>>> rho2 = 0.3 #np.random.rand(1)
|
Compute expectation E( X1^{+}*X2^{+} ) with random
|
||||||
>>> Sc2 = [[1, rho2], [rho2 ,1]]
|
correlation coefficient,Cov(X1,X2) = rho2.
|
||||||
>>> Blo2 = 0; Bup2 = np.inf; indI2 = [-1, 1]
|
>>> m2 = [0, 0]
|
||||||
>>> rind2 = Rind(method=1)
|
>>> rho2 = 0.3 #np.random.rand(1)
|
||||||
>>> g2 = lambda x : (x*(np.pi/2+np.arcsin(x))+np.sqrt(1-x**2))/(2*np.pi)
|
>>> Sc2 = [[1, rho2], [rho2 ,1]]
|
||||||
>>> E2 = g2(rho2); E2 # exact value
|
>>> Blo2 = 0; Bup2 = np.inf; indI2 = [-1, 1]
|
||||||
0.24137214191774381
|
>>> rind2 = Rind(method=1)
|
||||||
|
>>> g2 = lambda x : (x*(np.pi/2+np.arcsin(x))+np.sqrt(1-x**2))/(2*np.pi)
|
||||||
>>> E3, err3, terr3 = rind(Sc2,m2,Blo2,Bup2,indI2,nt=0); E3;err3;terr3
|
>>> E2 = g2(rho2); E2 # exact value
|
||||||
array([ 0.24127499])
|
0.24137214191774381
|
||||||
array([ 0.00013838])
|
|
||||||
array([ 1.00000000e-10])
|
>>> E3, err3, terr3 = rind(Sc2,m2,Blo2,Bup2,indI2,nt=0); E3;err3;terr3
|
||||||
|
array([ 0.24127499])
|
||||||
>>> E4, err4, terr4 = rind2(Sc2,m2,Blo2,Bup2,indI2,nt=0); E4;err4;terr4
|
array([ 0.00013838])
|
||||||
array([ 0.24127499])
|
array([ 1.00000000e-10])
|
||||||
array([ 0.00013838])
|
|
||||||
array([ 1.00000000e-10])
|
>>> E4, err4, terr4 = rind2(Sc2,m2,Blo2,Bup2,indI2,nt=0); E4;err4;terr4
|
||||||
|
array([ 0.24127499])
|
||||||
>>> E5, err5, terr5 = rind2(Sc2,m2,Blo2,Bup2,indI2,nt=0,abseps=1e-4); E5;err5;terr5
|
array([ 0.00013838])
|
||||||
array([ 0.24127499])
|
array([ 1.00000000e-10])
|
||||||
array([ 0.00013838])
|
|
||||||
array([ 1.00000000e-10])
|
>>> E5, err5, terr5 = rind2(Sc2,m2,Blo2,Bup2,indI2,nt=0,abseps=1e-4); E5;err5;terr5
|
||||||
'''
|
array([ 0.24127499])
|
||||||
def test_prbnormtndpc():
|
array([ 0.00013838])
|
||||||
'''
|
array([ 1.00000000e-10])
|
||||||
>>> rho2 = np.random.rand(2);
|
'''
|
||||||
>>> a2 = np.zeros(2);
|
|
||||||
>>> b2 = np.repeat(np.inf,2);
|
|
||||||
>>> [val2,err2, ift2] = prbnormtndpc(rho2,a2,b2)
|
def test_prbnormtndpc():
|
||||||
>>> g2 = lambda x : 0.25+np.arcsin(x[0]*x[1])/(2*pi)
|
'''
|
||||||
>>> E2 = g2(rho2) #% exact value
|
>>> rho2 = np.random.rand(2)
|
||||||
>>> np.abs(E2-val2)<err2
|
>>> a2 = np.zeros(2)
|
||||||
True
|
>>> b2 = np.repeat(np.inf,2)
|
||||||
|
>>> [val2,err2, ift2] = prbnormtndpc(rho2,a2,b2)
|
||||||
>>> rho3 = np.random.rand(3)
|
>>> g2 = lambda x : 0.25+np.arcsin(x[0]*x[1])/(2*pi)
|
||||||
>>> a3 = np.zeros(3)
|
>>> E2 = g2(rho2) #% exact value
|
||||||
>>> b3 = np.repeat(inf,3)
|
>>> np.abs(E2-val2)<err2
|
||||||
>>> [val3,err3, ift3] = prbnormtndpc(rho3,a3,b3)
|
True
|
||||||
>>> g3 = lambda x : 0.5-sum(np.sort(np.arccos([x[0]*x[1],x[0]*x[2],x[1]*x[2]])))/(4*pi)
|
|
||||||
>>> E3 = g3(rho3) # Exact value
|
>>> rho3 = np.random.rand(3)
|
||||||
>>> np.abs(E3-val3)<err3
|
>>> a3 = np.zeros(3)
|
||||||
True
|
>>> b3 = np.repeat(inf,3)
|
||||||
'''
|
>>> [val3,err3, ift3] = prbnormtndpc(rho3,a3,b3)
|
||||||
def test_prbnormndpc():
|
>>> g3 = lambda x : 0.5-sum(np.sort(np.arccos([x[0]*x[1],x[0]*x[2],x[1]*x[2]])))/(4*pi)
|
||||||
'''
|
>>> E3 = g3(rho3) # Exact value
|
||||||
>>> rho2 = np.random.rand(2);
|
>>> np.abs(E3-val3)<err3
|
||||||
>>> a2 = np.zeros(2);
|
True
|
||||||
>>> b2 = np.repeat(np.inf,2);
|
'''
|
||||||
>>> [val2,err2, ift2] = prbnormndpc(rho2,a2,b2)
|
|
||||||
>>> g2 = lambda x : 0.25+np.arcsin(x[0]*x[1])/(2*pi)
|
|
||||||
>>> E2 = g2(rho2) #% exact value
|
def test_prbnormndpc():
|
||||||
>>> np.abs(E2-val2)<err2
|
'''
|
||||||
True
|
>>> rho2 = np.random.rand(2)
|
||||||
|
>>> a2 = np.zeros(2);
|
||||||
>>> rho3 = np.random.rand(3)
|
>>> b2 = np.repeat(np.inf,2)
|
||||||
>>> a3 = np.zeros(3)
|
>>> [val2,err2, ift2] = prbnormndpc(rho2,a2,b2)
|
||||||
>>> b3 = np.repeat(inf,3)
|
>>> g2 = lambda x : 0.25+np.arcsin(x[0]*x[1])/(2*pi)
|
||||||
>>> [val3,err3, ift3] = prbnormndpc(rho3,a3,b3)
|
>>> E2 = g2(rho2) #% exact value
|
||||||
>>> g3 = lambda x : 0.5-sum(np.sort(np.arccos([x[0]*x[1],x[0]*x[2],x[1]*x[2]])))/(4*pi)
|
>>> np.abs(E2-val2)<err2
|
||||||
>>> E3 = g3(rho3) # Exact value
|
True
|
||||||
>>> np.abs(E3-val3)<err2
|
|
||||||
True
|
>>> rho3 = np.random.rand(3)
|
||||||
'''
|
>>> a3 = np.zeros(3)
|
||||||
|
>>> b3 = np.repeat(inf,3)
|
||||||
def test_prbnormnd():
|
>>> [val3,err3, ift3] = prbnormndpc(rho3,a3,b3)
|
||||||
'''
|
>>> g3 = lambda x : 0.5-sum(np.sort(np.arccos([x[0]*x[1],x[0]*x[2],x[1]*x[2]])))/(4*pi)
|
||||||
>>> import numpy as np
|
>>> E3 = g3(rho3) # Exact value
|
||||||
>>> Et = 0.001946 # # exact prob.
|
>>> np.abs(E3-val3)<err2
|
||||||
>>> n = 5
|
True
|
||||||
>>> Blo =-np.inf; Bup=-1.2
|
'''
|
||||||
>>> m = np.zeros(n); rho = 0.3;
|
|
||||||
>>> Sc =(np.ones((n,n))-np.eye(n))*rho+np.eye(n)
|
|
||||||
>>> A = np.repeat(Blo,n)
|
def test_prbnormnd():
|
||||||
>>> B = np.repeat(Bup,n)-m
|
'''
|
||||||
>>> [val,err,inform] = prbnormnd(Sc,A,B)
|
>>> import numpy as np
|
||||||
>>> np.abs(val-Et)< err
|
>>> Et = 0.001946 # # exact prob.
|
||||||
True
|
>>> n = 5
|
||||||
>>> 'val = %2.5f' % val
|
>>> Blo =-np.inf; Bup=-1.2
|
||||||
'val = 0.00195'
|
>>> m = np.zeros(n); rho = 0.3;
|
||||||
'''
|
>>> Sc =(np.ones((n,n))-np.eye(n))*rho+np.eye(n)
|
||||||
def test_cdfnorm2d():
|
>>> A = np.repeat(Blo,n)
|
||||||
'''
|
>>> B = np.repeat(Bup,n)-m
|
||||||
>>> x = np.linspace(-3,3,3)
|
>>> [val,err,inform] = prbnormnd(Sc,A,B)
|
||||||
>>> [b1,b2] = np.meshgrid(x,x)
|
>>> np.abs(val-Et)< err
|
||||||
>>> r = 0.3
|
True
|
||||||
>>> cdfnorm2d(b1,b2,r)
|
>>> 'val = %2.5f' % val
|
||||||
array([[ 2.38515157e-05, 1.14504149e-03, 1.34987703e-03],
|
'val = 0.00195'
|
||||||
[ 1.14504149e-03, 2.98493342e-01, 4.99795143e-01],
|
'''
|
||||||
[ 1.34987703e-03, 4.99795143e-01, 9.97324055e-01]])
|
|
||||||
'''
|
|
||||||
|
def test_cdfnorm2d():
|
||||||
def test_prbnorm2d():
|
'''
|
||||||
'''
|
>>> x = np.linspace(-3,3,3)
|
||||||
>>> a = [-1, -2]
|
>>> [b1,b2] = np.meshgrid(x,x)
|
||||||
>>> b = [1, 1]
|
>>> r = 0.3
|
||||||
>>> r = 0.3
|
>>> cdfnorm2d(b1,b2,r)
|
||||||
>>> prbnorm2d(a,b,r)
|
array([[ 2.38515157e-05, 1.14504149e-03, 1.34987703e-03],
|
||||||
array([ 0.56659121])
|
[ 1.14504149e-03, 2.98493342e-01, 4.99795143e-01],
|
||||||
'''
|
[ 1.34987703e-03, 4.99795143e-01, 9.97324055e-01]])
|
||||||
if __name__ == '__main__':
|
'''
|
||||||
import doctest
|
|
||||||
doctest.testmod()
|
|
||||||
|
def test_prbnorm2d():
|
||||||
|
'''
|
||||||
|
>>> a = [-1, -2]
|
||||||
|
>>> b = [1, 1]
|
||||||
|
>>> r = 0.3
|
||||||
|
>>> prbnorm2d(a,b,r)
|
||||||
|
array([ 0.56659121])
|
||||||
|
'''
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
@ -1,58 +1,61 @@
|
|||||||
# -*- coding:utf-8 -*-
|
# -*- coding:utf-8 -*-
|
||||||
"""
|
"""
|
||||||
Created on 5. aug. 2010
|
Created on 5. aug. 2010
|
||||||
|
|
||||||
@author: pab
|
@author: pab
|
||||||
"""
|
"""
|
||||||
import wafo.data
|
import wafo.data # @UnusedImport
|
||||||
import numpy as np
|
import numpy as np # @UnusedImport
|
||||||
|
|
||||||
def test_timeseries():
|
|
||||||
'''
|
def test_timeseries():
|
||||||
>>> import wafo.data
|
'''
|
||||||
>>> import wafo.objects as wo
|
>>> import wafo.data
|
||||||
>>> x = wafo.data.sea()
|
>>> import wafo.objects as wo
|
||||||
>>> ts = wo.mat2timeseries(x)
|
>>> x = wafo.data.sea()
|
||||||
>>> ts.sampling_period()
|
>>> ts = wo.mat2timeseries(x)
|
||||||
0.25
|
>>> ts.sampling_period()
|
||||||
|
0.25
|
||||||
Estimate spectrum
|
|
||||||
>>> S = ts.tospecdata()
|
Estimate spectrum
|
||||||
The default L is set to 325
|
>>> S = ts.tospecdata()
|
||||||
>>> S.data[:10]
|
The default L is set to 325
|
||||||
array([ 0.00913087, 0.00881073, 0.00791944, 0.00664244, 0.00522429,
|
>>> S.data[:10]
|
||||||
0.00389816, 0.00282753, 0.00207843, 0.00162678, 0.0013916 ])
|
array([ 0.00913087, 0.00881073, 0.00791944, 0.00664244, 0.00522429,
|
||||||
|
0.00389816, 0.00282753, 0.00207843, 0.00162678, 0.0013916 ])
|
||||||
Estimated covariance function
|
|
||||||
>>> rf = ts.tocovdata(lag=150)
|
Estimated covariance function
|
||||||
>>> rf.data[:10]
|
>>> rf = ts.tocovdata(lag=150)
|
||||||
array([ 0.22368637, 0.20838473, 0.17110733, 0.12237803, 0.07024054,
|
>>> rf.data[:10]
|
||||||
0.02064859, -0.02218831, -0.0555993 , -0.07859847, -0.09166187])
|
array([ 0.22368637, 0.20838473, 0.17110733, 0.12237803, 0.07024054,
|
||||||
'''
|
0.02064859, -0.02218831, -0.0555993 , -0.07859847, -0.09166187])
|
||||||
def test_timeseries_trdata():
|
'''
|
||||||
'''
|
|
||||||
>>> import wafo.spectrum.models as sm
|
|
||||||
>>> import wafo.transform.models as tm
|
def test_timeseries_trdata():
|
||||||
>>> from wafo.objects import mat2timeseries
|
'''
|
||||||
>>> Hs = 7.0
|
>>> import wafo.spectrum.models as sm
|
||||||
>>> Sj = sm.Jonswap(Hm0=Hs)
|
>>> import wafo.transform.models as tm
|
||||||
>>> S = Sj.tospecdata() #Make spectrum object from numerical values
|
>>> from wafo.objects import mat2timeseries
|
||||||
>>> S.tr = tm.TrOchi(mean=0, skew=0.16, kurt=0, sigma=Hs/4, ysigma=Hs/4)
|
>>> Hs = 7.0
|
||||||
>>> xs = S.sim(ns=2**20)
|
>>> Sj = sm.Jonswap(Hm0=Hs)
|
||||||
>>> ts = mat2timeseries(xs)
|
>>> S = Sj.tospecdata() #Make spectrum object from numerical values
|
||||||
>>> g0, gemp = ts.trdata(monitor=True) # Monitor the development
|
>>> S.tr = tm.TrOchi(mean=0, skew=0.16, kurt=0, sigma=Hs/4, ysigma=Hs/4)
|
||||||
>>> g1, gemp = ts.trdata(method='m', gvar=0.5 ) # Equal weight on all points
|
>>> xs = S.sim(ns=2**20)
|
||||||
>>> g2, gemp = ts.trdata(method='n', gvar=[3.5, 0.5, 3.5]) # Less weight on the ends
|
>>> ts = mat2timeseries(xs)
|
||||||
>>> S.tr.dist2gauss()
|
>>> g0, gemp = ts.trdata(monitor=True) # Monitor the development
|
||||||
1.4106988010566603
|
>>> g1, gemp = ts.trdata(method='m', gvar=0.5 ) # Equal weight on all points
|
||||||
>>> np.round(g0.dist2gauss())
|
>>> g2, gemp = ts.trdata(method='n', gvar=[3.5, 0.5, 3.5]) # Less weight on the ends
|
||||||
1.0
|
>>> S.tr.dist2gauss()
|
||||||
>>> np.round(g1.dist2gauss())
|
1.4106988010566603
|
||||||
1.0
|
>>> np.round(g0.dist2gauss())
|
||||||
>>> np.round(g2.dist2gauss())
|
1.0
|
||||||
1.0
|
>>> np.round(g1.dist2gauss())
|
||||||
|
1.0
|
||||||
'''
|
>>> np.round(g2.dist2gauss())
|
||||||
if __name__=='__main__':
|
1.0
|
||||||
import doctest
|
|
||||||
doctest.testmod()
|
'''
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
Loading…
Reference in New Issue