Changed doctest to unittest

master
pbrod 9 years ago
parent 16bffcc725
commit 3495621f69

@ -5,70 +5,77 @@ Created on 17. juli 2010
''' '''
import numpy as np # @UnusedImport import numpy as np # @UnusedImport
from numpy import pi, inf # @UnusedImport from numpy import pi, inf # @UnusedImport
# @UnusedImport from numpy.testing import assert_array_almost_equal
from wafo.gaussian import (Rind, prbnormtndpc, prbnormndpc, prbnormnd, from wafo.gaussian import (Rind, prbnormtndpc, prbnormndpc, prbnormnd,
cdfnorm2d, prbnorm2d) cdfnorm2d, prbnorm2d)
def test_rind(): def test_rind():
'''
>>> Et = 0.001946 # # exact prob. Et = 0.001946 # exact prob.
>>> n = 5 n = 5
>>> Blo =-np.inf; Bup=-1.2; indI=[-1, n-1] # Barriers Blo = -np.inf
>>> m = np.zeros(n); rho = 0.3; Bup = -1.2
>>> Sc =(np.ones((n,n))-np.eye(n))*rho+np.eye(n) indI = [-1, n - 1] # Barriers
>>> rind = Rind() m = np.zeros(n)
>>> E0, err0, terr0 = rind(Sc,m,Blo,Bup,indI); 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' assert(np.abs(E0 - Et) < err0 + terr0)
>>> A = np.repeat(Blo,n); B = np.repeat(Bup,n) # Integration limits t = 'E0 = %2.6f' % E0
>>> E1, err1, terr1 = rind(np.triu(Sc),m,A,B); #same as E0 assert(t == 'E0 = 0.001946')
>>> np.abs(E1-Et)< err0+terr0
array([ True], dtype=bool) A = np.repeat(Blo, n)
>>> 'E1 = %2.5f' % E1 B = np.repeat(Bup, n) # Integration limits
'E1 = 0.00195' E1, err1, terr1 = rind(np.triu(Sc), m, A, B) # same as E0
assert(np.abs(E1 - Et) < err0 + terr0)
Compute expectation E( abs(X1*X2*...*X5) )
>>> xc = np.zeros((0,1)) t = 'E1 = %2.5f' % E1
>>> infinity = 37 assert(t == 'E1 = 0.00195')
>>> dev = np.sqrt(np.diag(Sc)) # std
>>> ind = np.nonzero(indI[1:])[0] # Compute expectation E( abs(X1*X2*...*X5) )
>>> Bup, Blo = np.atleast_2d(Bup,Blo) xc = np.zeros((0, 1))
>>> Bup[0,ind] = np.minimum(Bup[0,ind] , infinity*dev[indI[ind+1]]) infinity = 37
>>> Blo[0,ind] = np.maximum(Blo[0,ind] ,-infinity*dev[indI[ind+1]]) dev = np.sqrt(np.diag(Sc)) # std
>>> rind(Sc,m,Blo,Bup,indI, xc, nt=0) ind = np.nonzero(indI[1:])[0]
(array([ 0.05494076]), array([ 0.00083066]), array([ 1.00000000e-10])) Bup, Blo = np.atleast_2d(Bup, Blo)
Bup[0, ind] = np.minimum(Bup[0, ind], infinity * dev[indI[ind + 1]])
Compute expectation E( X1^{+}*X2^{+} ) with random Blo[0, ind] = np.maximum(Blo[0, ind], -infinity * dev[indI[ind + 1]])
correlation coefficient,Cov(X1,X2) = rho2. val, err, terr = rind(Sc, m, Blo, Bup, indI, xc, nt=0)
>>> m2 = [0, 0] assert_array_almost_equal(val, 0.05494076)
>>> rho2 = 0.3 #np.random.rand(1) assert(err < 0.001)
>>> Sc2 = [[1, rho2], [rho2 ,1]] assert_array_almost_equal(terr, 1.00000000e-10)
>>> Blo2 = 0; Bup2 = np.inf; indI2 = [-1, 1]
>>> rind2 = Rind(method=1) # Compute expectation E( X1^{+}*X2^{+} ) with random
>>> g2 = lambda x : (x*(np.pi/2+np.arcsin(x))+np.sqrt(1-x**2))/(2*np.pi) # correlation coefficient,Cov(X1,X2) = rho2.
>>> E2 = g2(rho2); E2 # exact value m2 = [0, 0]
0.24137214191774381 rho2 = 0.3 # np.random.rand(1)
Sc2 = [[1, rho2], [rho2, 1]]
>>> E3, err3, terr3 = rind(Sc2,m2,Blo2,Bup2,indI2,nt=0); E3;err3;terr3 Blo2 = 0
array([ 0.24127499]) Bup2 = np.inf
array([ 0.00013838]) indI2 = [-1, 1]
array([ 1.00000000e-10]) rind2 = Rind(method=1)
g2 = lambda x: (
>>> E4, err4, terr4 = rind2(Sc2,m2,Blo2,Bup2,indI2,nt=0); E4;err4;terr4 x * (np.pi / 2 + np.arcsin(x)) + np.sqrt(1 - x**2)) / (2 * np.pi)
array([ 0.24127499]) assert_array_almost_equal(g2(rho2), 0.24137214191774381) # exact value
array([ 0.00013838])
array([ 1.00000000e-10]) E3, err3, terr3 = rind(Sc2, m2, Blo2, Bup2, indI2, nt=0)
assert_array_almost_equal(E3, 0.24127499)
>>> E5, err5, terr5 = rind2(Sc2,m2,Blo2,Bup2,indI2,nt=0,abseps=1e-4); E5;err5;terr5 assert_array_almost_equal(err3, 0.00013838)
array([ 0.24127499]) assert_array_almost_equal(terr3, 1.00000000e-10)
array([ 0.00013838])
array([ 1.00000000e-10]) E4, err4, terr4 = rind2(Sc2,m2,Blo2,Bup2,indI2,nt=0)
''' assert_array_almost_equal(E4, 0.24127499)
assert_array_almost_equal(err4, 0.00013838)
assert_array_almost_equal(terr4, 1.00000000e-10)
#
# >>> E5, err5, terr5 = rind2(Sc2,m2,Blo2,Bup2,indI2,nt=0,abseps=1e-4)
# array([ 0.24127499])
# array([ 0.00013838])
# array([ 1.00000000e-10])
def test_prbnormtndpc(): def test_prbnormtndpc():
@ -85,9 +92,9 @@ def test_prbnormtndpc():
>>> rho3 = np.random.rand(3) >>> rho3 = np.random.rand(3)
>>> a3 = np.zeros(3) >>> a3 = np.zeros(3)
>>> b3 = np.repeat(inf,3) >>> b3 = np.repeat(inf,3)
>>> [val3,err3, ift3] = prbnormtndpc(rho3,a3,b3) >>> [val3,err3, ift3] = prbnormtndpc(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) >>> 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 >>> E3 = g3(rho3) # Exact value
>>> np.abs(E3-val3)<err3 >>> np.abs(E3-val3)<err3
True True
''' '''

Loading…
Cancel
Save