Beispiel #1
0
def differences_testing ( RULE , logger ) :

    a = 1.5
    fun        =   lambda x :   math.sin ( a * x ) 
    exact_ders = ( fun                     ,           ## 0
                   lambda x :   math.cos ( a * x ) * a             , ## 1
                   lambda x : - math.sin ( a * x ) * a * a         , ## 2 
                   lambda x : - math.cos ( a * x ) * pow ( a , 3 ) , ## 3  
                   lambda x :   math.sin ( a * x ) * pow ( a , 4 ) , ## 4
                   lambda x :   math.cos ( a * x ) * pow ( a , 5 ) , ## 4
                   lambda x : - math.sin ( a * x ) * pow ( a , 6 ) ) ## 4
    
    h0   = 0
    hmax = 1.2
    N    = 1000
    cd   = SE ()
    cp   = SE ()
    ch   = SE ()
    
    for D in range ( 1 , RULE.DMAX + 1 ) :

        table = [  ( 'I' , '#' , 'step' , '' , 'delta' , '' , 'pull' ) ]
        
        for I in range ( RULE.IMAX ( D ) ) :
            
            rule  = RULE ( D , I , with_error = True )
            
            for ik in range ( N ) :
                
                z      = random.uniform ( -math.pi , math.pi )
                
                h , _  = rule.optimal_step ( fun , z , h0 , hmax )
                r      = rule ( fun , z , h )
                exact  = exact_ders [ D ] ( z )
                

                dd     = float ( r ) - exact
                
                cd    += abs ( dd )
                cp    += dd / r.error()
                ch    += h 
                
                
            hh = ch.mean()
            dd = cd.mean()

            hh , nh = pretty_ve ( hh , width = 4 , precision = 3 , parentheses = False )
            dd , nd = pretty_ve ( dd , width = 4 , precision = 3 , parentheses = False )

            row = '%2d' % I , \
                  '%2d' % len( rule.stencil)  , \
                  hh , '' if not nh else '[10^%-3d]' % nh , \
                  dd , '' if not nd else '[10^%-3d]' % nd , \
                  "%-.2f" % cp.rms() 
            table.append ( row )
            

        title = 'Finite differences test for D=%s derivative' %  D  
        table = T.table ( table , title = title , prefix = '# ' , alignment = 'rrlclcr' )
        logger.info ('%s\n%s' % ( title , table ) ) 
Beispiel #2
0
def test_derivative_4 ():

    logger = getLogger ( 'test_derivative_4' )


    functions = (
        ( lambda x : cos(10.*x)   , lambda x : -10*sin(10.*x)                  ) , 
        ( lambda x : x**3         , lambda x : 3.0*x*x                         ) , 
        ( lambda x : exp(x)       , lambda x : exp(x)                          ) ,
        ( lambda x : x**8         , lambda x : 8.0*x**7                        ) ,
        ( lambda x : tanh(2.*x)   , lambda x : 2*(1.-tanh(2.*x)**2)            ) ,
        ( lambda x : 1.11*x       , lambda x : 1.11                            ) , 
        ( lambda x : 1.11111      , lambda x : 0.0                             ) , 
        ( lambda x : x**10        , lambda x : 10.*x**9                        ) , 
        )
    
    from ostap.core.core import SE
    counters = {} 

    from ostap.utils.progress_bar import progress_bar
    
    IMAX  = 8 
    table =  [ ['Function'] + [ 'I=%d' % i for i in range ( IMAX ) ] ] 
    for i , o in enumerate ( progress_bar ( functions ) ) :
        
        fun = o [ 0 ] ## function 
        der = o [ 1 ] ## derivative 
        
        row = [ '%2d' % (i+1) ]
        for I in range ( IMAX ) :
            
            cnt1 = SE ()            
            cnt2 = SE ()
            
            dd   = Derivative ( fun , step = 0.001 , calc = I , with_error = True )

            for j in range ( 1000 ) :
                
                x = random.uniform ( 0.05 , 1.5 )
                res  = dd ( x ) 
                dif  = float ( res ) - der ( x ) 
                cnt1 += dif 
                if res.cov2() > 0 : cnt2 += dif/res.error()  
                
            mmax1 = abs ( cnt1.max ()  *10**12 ) 
            if 2 < cnt2.nEntries() : 
                mmax2 = cnt2.max()
                row.append ( '%7.3f / %-5.2fs' % ( mmax1, mmax2 ) )
            else :
                mmax2 = 0
                row.append ( '%7.3f / %-5.2fs' % ( mmax1, mmax2 ) )
                
        table.append ( row )

    table = T.table ( table , prefix = '# ' , alignment=9*'c' )
    logger.info ('Numerical differentiation: Max difference [10^12]\n%s' % table ) 
Beispiel #3
0
def test_interpolation():
    """Test spline interpolation
    """

    if 62006 <= root_version_int:
        logger.warning("Test_interpolation segfaults for ROOT %s" %
                       root_version_int)
        return

    from math import sin, pi, sqrt

    fun = lambda x: sin(2 * pi * x)

    bs = ostap.math.bspline.interpolate(
        fun, None, [0] + [random.uniform(0.01, 0.99) for i in range(50)] + [1],
        2)

    from ostap.stats.counters import SE
    s = SE()
    for i in range(10000):
        x = random.uniform(0, 1)
        vf = fun(x)
        vb = bs(x)
        s += vf - vb

    logger.info('Interpolation quality %s' % s)
Beispiel #4
0
def derivative_testing ( der_type , func , der_exact , logger , **kwargs ) :

    IMAX = der_type.IMAX
    
    cnt1  = {}
    
    for i in range ( IMAX )  :  cnt1 [i] = SE()
        
    ders = [ der_type ( func , I , **kwargs ) for I in range ( IMAX ) ]
    
    with timing ( 'derivative test' , logger = logger ) :
        
        for i in range(10000) :
            
            x = random.uniform ( 0 , pi )
            
            d_true = der_exact ( x )
            
            for I , dd in enumerate ( ders ) :
                
                delta     = dd ( x ) - d_true
                cnt1 [I] += delta

    rows = [ ('I' , 'rms' , 'min' , 'max' ) ]
    
    for i,c in cnt1.items() :
        row = '%d' % i , '% .4g' % c.rms () , '% .4g' % c.min() , '% .4g' % c.max() 
        rows.append ( row ) 
        
    table = T.table ( rows ,
                      title  = 'Test numerical derivatives' ,
                      prefix = '# ' , alignment = 'clll'     )
    
    logger.info ( 'Test numerical derivatives\n%s' % table )
Beispiel #5
0
 def stat(self, *args):
     """ Get the full statistic over all categories
     >>> method = ...
     >>> pt, eta, phi = 5 ,  3.0 , 0  ## variables
     >>> print 'Response statistic is %s' % method.stat (  pt , eta , phi ) 
     """
     from ostap.stats.counters import SE
     se = SE()
     for i in range(self.__N):
         se += self.evaluate(i, *args)
     return se
Beispiel #6
0
def test_derivative():

    from math import sin, cos, pi
    from ostap.math.derivative import derivative, iszero
    import random
    from ostap.stats.counters import SE
    from ostap.utils.timing import timing

    cnt = {}
    cntE = {}
    for I in range(1, 9):
        cnt[I] = SE()

    func = lambda x: sin(10 * x) + x
    deri = lambda x: 10 * cos(10 * x) + 1

    func = lambda x: sin(x) / x
    deri = lambda x: (cos(x) - sin(x) / x) / x

    ## func = lambda x :   x**9
    ## deri = lambda x : 9*x**8

    ## from math import sinh, cosh
    ## func = lambda x :    sinh(2*x)
    ## deri = lambda x :  2*cosh(2*x)

    ## from math import tanh,cosh
    ## func = lambda x :      tanh(3*x)
    ## deri = lambda x :  3./(cosh(3*x)**2)

    with timing():

        for i in range(50000):

            x = random.uniform(0, pi)

            d_true = deri(x)

            for I in range(1, 9):
                delta = derivative(func, x, I=I, err=True) - d_true
                ## delta = derivative ( sin , x , I = I ) - d_true
                ## cnt [I] += delta*1.e+10
                ## cnt [I] += abs(delta/d_true)*1.e+10
                if not iszero(delta.cov2()):
                    cnt[I] += abs(delta.value()) / delta.error()

    for I in range(1, 9):
        print I, cnt[I]
Beispiel #7
0
def test_interpolation ():
    """Test bernstein interpolation
    """
    from math import sin,pi, sqrt

    fun = lambda x  : sin(2*pi*x) 
    bs  =  ostap.math.bernstein.interpolate ( fun , [0] + [  random.uniform(0.01,0.99) for i in range(25) ] + [1] , 0 , 1 )
    
    from ostap.stats.counters import SE
    s = SE()
    for i in range(10000) :
        x = random.uniform ( 0 , 1 )
        vf = fun(x)
        vb = bs (x) 
        s += vf-vb
            
    logger.info ('Interpolation quality %s' % s )
Beispiel #8
0
def test_approximation():
    """Test spline approximation
    """
    from math import sin, pi, sqrt

    fun = lambda x: sin(2 * pi * x)
    bs = ostap.math.bspline.approximate(
        fun, [0] + [random.uniform(0.01, 0.99) for i in range(50)] + [1], 2)

    from ostap.stats.counters import SE
    s = SE()
    for i in range(10000):
        x = random.uniform(0, 1)
        vf = fun(x)
        vb = bs(x)
        s += vf - vb

    logger.info('Approximation quality %s' % s)
def test_derivative_2():

    logger = getLogger('test_derivative_2')

    cnt = {}
    cntE = {}
    for I in range(1, 9):
        cnt[I] = SE()

    func = lambda x: sin(10 * x) + x
    deri = lambda x: 10 * cos(10 * x) + 1

    with timing():

        for i in range(10000):

            x = random.uniform(0, pi)

            d_true = deri(x)

            for I in range(1, 9):
                delta = derivative(func, x, I=I) - d_true
                cnt[I] += delta

    rows = [('Order', '#', 'min', 'max')]
    for i in cnt:
        c = cnt[I]
        row = '%d' % i, '%d' % c.nEntries(), '%+.5g' % c.min(
        ), '%+.5g' % c.max()
        rows.append(row)

    table = T.table(rows,
                    title='Test numerical derivatives',
                    prefix='# ',
                    alignment='crll')

    logger.info('Test numerical derivatives\n%s' % table)
Beispiel #10
0
if not hasattr(random, 've_gauss'): random.ve_gauss = random._inst.ve_gauss
if not hasattr(random.Random, 'poisson'): random.Random.poisson = _poisson_
if not hasattr(random, 'poisson'): random.poisson = random._inst.poisson
if not hasattr(random.Random, 'cauchy'): random.Random.cauchy = _cauchy_
if not hasattr(random, 'cauchy'): random.cauchy = random._inst.cauchy

bifur = random.bifur
ve_gauss = random.ve_gauss
poisson = random.poisson
cauchy = random.cauchy

# =============================================================================
if '__main__' == __name__:

    from ostap.utils.docme import docme
    docme(__name__, logger=logger)

    from ostap.stats.counters import SE
    cnt = SE()
    mu = 0.4
    for i in range(10000):
        cnt += poisson(0.4)

    logger.info('Poisson(mu=%s) : %s' % (mu, cnt))

    logger.info(80 * '*')

# =============================================================================
# The END
# =============================================================================