def run_grid_interpolation(tfunc, dct, N, low, high, scale=1.e-8): Abscissas = Ostap.Math.Interpolation.Abscissas ## uniform abscissas i0 = interpolate(dct) ## bernstein interpolant i1 = interpolate_bernstein(dct, None, low, high) xx = [] for i in range(100000): xx.append(random.uniform(low, high)) xx.sort() c0 = SE() c1 = SE() for x in xx: f = tfunc(x) f0 = i0(x) f1 = i1(x) d0 = f0 - f d1 = f1 - f c0 += abs(d0) / scale c1 += abs(d1) / scale logger.info('Grid precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % (scale, c0.mean().value(), c0.rms(), c0.max())) logger.info('Bernstein precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % (scale, c1.mean().value(), c1.rms(), c1.max()))
def run_func_interpolation ( fun , N , low , high , scale = 1.e-8 ) : Abscissas = Ostap.Math.Interpolation.Abscissas ## uniform abscissas i0 = interpolate ( fun , Abscissas ( N , low , high , 0 ) ) ## Chebyshev abscissas i1 = interpolate ( fun , Abscissas ( N , low , high , 1 ) ) ## lobatto abscissas i2 = interpolate ( fun , Abscissas ( N , low , high , 2 ) ) ## random abscissas i3 = interpolate ( fun , [ random.uniform ( low , high ) for i in range (N ) ] ) ## bernstein interpolant i4 = interpolate_bernstein ( fun , Abscissas ( N , low , high , 2 ) , low , high ) xx = [] for i in range ( 100000 ) : xx.append ( random.uniform ( low , high ) ) xx.sort() c0 = SE() c1 = SE() c2 = SE() c3 = SE() c4 = SE() for x in xx : f = fun ( x ) f0 = i0 ( x ) f1 = i1 ( x ) f2 = i2 ( x ) f3 = i3 ( x ) f4 = i4 ( x ) d0 = f0 - f d1 = f1 - f d2 = f2 - f d3 = f3 - f d4 = f4 - f c0 += abs ( d0 ) / scale c1 += abs ( d1 ) / scale c2 += abs ( d2 ) / scale c3 += abs ( d3 ) / scale c4 += abs ( d4 ) / scale logger.info ( 'Uniform precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % ( scale , c0.mean () . value () , c0.rms () , c0.max () ) ) logger.info ( 'Chebyshev precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % ( scale , c1.mean () . value () , c1.rms () , c1.max () ) ) logger.info ( 'Lobatto precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % ( scale , c2.mean () . value () , c2.rms () , c2.max () ) ) logger.info ( 'Random precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % ( scale , c3.mean () . value () , c3.rms () , c3.max () ) ) logger.info ( 'Bernstein precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % ( scale , c4.mean () . value () , c4.rms () , c4.max () ) )
def test_parameterize_3D(): with ROOT.TFile.Open(data_file, 'READ') as f: tree = f.S lx = Ostap.Math.LegendreSum3(8, 8, 8, -2, 2, -4, 4, -4, 6) ly = Ostap.Math.LegendreSum3(8, 8, 8, -2, 2, -4, 4, -4, 6) lz = Ostap.Math.LegendreSum3(8, 8, 8, -2, 2, -2, 2, -4, 6) lu = Ostap.Math.LegendreSum3(8, 8, 8, -2, 2, -2, 2, -4, 4) lx.parameterize(tree, 'y', 'z', 'u', cuts) ly.parameterize(tree, 'x', 'z', 'u', cuts) lz.parameterize(tree, 'x', 'y', 'u', cuts) lu.parameterize(tree, 'x', 'y', 'z', cuts) lxy = lz.integralZ() lzu = lx.integralX() lxu = ly.integralY() hxy = ROOT.TH2F(hID(), '', 20, -2, 2, 20, -2, 2) hzu = ROOT.TH2F(hID(), '', 20, -4, 4, 20, -4, 6) hxu = ROOT.TH2F(hID(), '', 20, -2, 2, 20, -4, 6) tree.project(hxy, 'y:x', cuts) tree.project(hzu, 'u:z', cuts) tree.project(hxu, 'u:x', cuts) lxy *= (4.0 / 20) * (4.0 / 20) lzu *= (8.0 / 20) * (10.0 / 20) lxu *= (4.0 / 20) * (10.0 / 20) d1 = SE() d2 = SE() d3 = SE() for i in range(100): x = random.uniform(-2, 2) y = random.uniform(-2, 2) z = random.uniform(-4, 4) u = random.uniform(-4, 6) d1 += (lxy(x, y) - hxy(x, y)) / max(lxy(x, y), hxy(x, y)) d2 += (lzu(z, u) - hzu(z, u)) / max(lzu(z, u), hzu(z, u)) d3 += (lxu(x, u) - hxu(x, u)) / max(lxu(x, u), hxu(x, u)) logger.info('3D-(xy)-DIFFERENCES are %s ' % d1) logger.info('3D-(zu)-DIFFERENCES are %s ' % d2) logger.info('3D-(xu)-DIFFERENCES are %s ' % d3)
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(30)] + [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) functions.add(bs) with wait(3), use_canvas('test_interpolation'): f1_draw(fun, xmin=0, xmax=1, linecolor=2) bs.draw('same', linecolor=4)
def test_pickle(): logger = getLogger('test_pickle') logger.info('Check pickling/unpickling') import pickle rows = [('#', 'before', 'after', 'mean', 'rms')] for i, f in enumerate(functions, start=1): fs = pickle.loads(pickle.dumps(f)) s = SE() for j in range(1000): x = random.uniform(f.xmin(), f.xmax()) s += abs(fs(x) - f(x)) row = '%d' % i, f.__class__.__name__, fs.__class__.__name__, '%-+.4g' % s.mean( ), '%-+.4g' % s.rms() rows.append(row) import ostap.logger.table as T title = "Compare before/after eserialisation" table = T.table(rows, title=title, prefix='# ', alignment='rllll') logger.info('%s\n%s' % (title, table))
def test_pickle(): logger = getLogger('test_pickle') logger.info('Check pickling/unpickling') bad = False import pickle rows = [('#', 'before', 'after', 'mean', 'rms')] for i, f in enumerate(progress_bar(functions), start=1): n, ff = f fs = pickle.loads(pickle.dumps(ff)) s = SE() for j in range(1000): x = random.uniform(ff.xmin(), ff.xmax()) s += abs(fs(x) - ff(x)) mean = '%-+.6g' % s.mean() rms = '%-+.6g' % s.rms() if 1.e-7 < s.mean(): mean = attention(mean) if 1.e-7 < s.rms(): rms = attention(rms) row = '%d' % i, ff.__class__.__name__, fs.__class__.__name__, mean, rms ## row = '%d' % i , '%s' % ff , '%s' % fs , '%-+.5g' % s.mean() , '%-+.5g' % s.rms() rows.append(row) import ostap.logger.table as T title = "Compare before/after serialisation" table = T.table(rows, title=title, prefix='# ', alignment='rllll') if bad: logger.warning('%s\n%s' % (title, table)) else: logger.info('%s\n%s' % (title, table))
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) functions.add(bs) with wait(3), use_canvas('test_approximation'): f1_draw(fun, xmin=0, xmax=1, linecolor=2) bs.draw('same', linecolor=4)
def test_interpolation(): """Test bernstein interpolation """ logger = getLogger("test_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) functions.add(bs)
def test_models(): xmin = 0 xmax = 2 ymin = xmin ymax = xmax zmin = xmin zmax = xmax funcs = [ Ostap.Math.Bernstein3D(2, 2, 2, xmin, xmax, ymin, ymax, zmin, zmax), Ostap.Math.Bernstein3DSym(2, xmin, xmax), Ostap.Math.Bernstein3DMix(2, 2, xmin, xmax, zmin, zmax), Ostap.Math.Positive3D(2, 2, 2, xmin, xmax, ymin, ymax, zmin, zmax), Ostap.Math.Positive3DSym(2, xmin, xmax), Ostap.Math.Positive3DMix(2, 2, xmin, xmax, zmin, zmax), ] cnt1 = SE() cnt2 = SE() cnt3 = SE() cnt4 = SE() cnt5 = SE() cnt6 = SE() cnt7 = SE() for f in funcs: ## print f.xmin() , f.xmax() , f.ymin() , f.ymax(), type(f) for i in range(f.npars()): f.setPar(i, random.uniform(1, 5)) for i in range(0, 100): if 0 == i: x1, x2, y1, y2, z1, z2 = xmin, xmax, ymin, ymax, zmin, zmax else: x1 = random.uniform(xmin, xmax / 2) x2 = random.uniform(x1, xmax) y1 = random.uniform(ymin, ymax / 2) y2 = random.uniform(y1, ymax) z1 = random.uniform(zmin, zmax / 2) z2 = random.uniform(z1, zmax) for f in funcs: i1 = f.integral(x1, x2, y1, y2, z1, z2) i2 = integral3(f, x1, x2, y1, y2, z1, z2) r1 = (i1 - i2) / (abs(i1) + abs(i2)) assert abs( r1 ) < 1.e-5, 'I3:ERROR: difference is too large: %s (%.2f,%.2f,%.2f,%.2f,%.2f,%.2f) %s' % ( r1, x1, x2, y1, y2, z1, z2, type(f)) cnt1 += r1 xm = 0.5 * (x1 + x2) ym = 0.5 * (y1 + y2) zm = 0.5 * (z1 + z2) i1 = f.integrateX(ym, zm, x1, x2) IX = Integrate3D_X(f, x1, x2) i2 = IX(ym, zm) r2 = (i1 - i2) / (abs(i1) + abs(i2)) assert abs( r2 ) < 1.e-5, 'IX:ERROR: difference is too large: %s (%.2f,%.2f,%.2f,%.2f) %s' % ( r2, x1, x2, ym, zm, type(f)) cnt2 += r2 i1 = f.integrateY(xm, zm, y1, y2) IY = Integrate3D_Y(f, y1, y2) i2 = IY(xm, zm) r3 = (i1 - i2) / (abs(i1) + abs(i2)) assert abs( r3 ) < 1.e-5, 'IY:ERROR: difference is too large: %s (%.2f,%.2f,%.2f,%.2f) %s' % ( r2, xm, y1, y2, zm, type(f)) cnt3 += r3 i1 = f.integrateZ(xm, ym, z1, z2) IZ = Integrate3D_Z(f, z1, z2) i2 = IZ(xm, ym) r4 = (i1 - i2) / (abs(i1) + abs(i2)) assert abs( r4 ) < 1.e-5, 'IY:ERROR: difference is too large: %s (%.2f,%.2f,%.2f,%.2f) %s' % ( r2, xm, ym, z1, z2, type(f)) cnt4 += r4 i1 = f.integrateXY(zm, x1, x2, y1, y2) I5 = Integrate3D_XY(f, x1, x2, y1, y2) i2 = I5(zm) r5 = (i1 - i2) / (abs(i1) + abs(i2)) assert abs( r5 ) < 1.e-5, 'I5:ERROR: difference is too large: %s (%.2f,%.2f,%.2f,%.2f,%.2f) %s' % ( r2, x1, x2, y1, y2, zm, type(f)) cnt5 += r5 i1 = f.integrateXZ(ym, x1, x2, z1, z2) I6 = Integrate3D_XZ(f, x1, x2, z1, z2) i2 = I6(ym) r6 = (i1 - i2) / (abs(i1) + abs(i2)) assert abs( r6 ) < 1.e-5, 'I6:ERROR: difference is too large: %s (%.2f,%.2f,%.2f,%.2f,%.2f) %s' % ( r2, x1, x2, ym, z1, z2, type(f)) cnt6 += r6 i1 = f.integrateYZ(xm, y1, y2, z1, z2) I7 = Integrate3D_YZ(f, y1, y2, z1, z2) i2 = I7(xm) r7 = (i1 - i2) / (abs(i1) + abs(i2)) assert abs( r7 ) < 1.e-5, 'I7:ERROR: difference is too large: %s (%.2f,%.2f,%.2f,%.2f,%.2f) %s' % ( r2, xm, y1, y2, z1, z2, type(f)) cnt7 += r7 print 'COUNTER(I3):', cnt1 print 'COUNTER(IX):', cnt2 print 'COUNTER(IY):', cnt3 print 'COUNTER(IZ):', cnt4 print 'COUNTER(I5):', cnt5 print 'COUNTER(I6):', cnt6 print 'COUNTER(I7):', cnt7
def test_models(): xmin = 0 xmax = 2 ymin = xmin ymax = xmax psx = Ostap.Math.PhaseSpaceNL(xmin + 0.00001 * (xmax - xmin), xmax - 0.00001 * (xmax - xmin), 3, 7) psy = Ostap.Math.PhaseSpaceNL(ymin + 0.00001 * (ymax - ymin), ymax - 0.00001 * (ymax - ymin), 3, 7) funcs = [ Ostap.Math.Bernstein2D(2, 2, xmin, xmax, ymin, ymax), Ostap.Math.Bernstein2DSym(2, xmin, xmax), Ostap.Math.Positive2D(2, 2, xmin, xmax, ymin, ymax), Ostap.Math.Positive2DSym(2, xmin, xmax), Ostap.Math.PS2DPol(psx, psy, 2, 2, xmin, xmax, ymin, ymax), Ostap.Math.PS2DPolSym(psx, 2, xmin, xmax), Ostap.Math.ExpoPS2DPol(psx, xmin, xmax, 2, 2, ymin, ymax), Ostap.Math.Expo2DPol(xmin, xmax, ymin, ymax, 2, 2), Ostap.Math.Expo2DPolSym(xmin, xmax, 2), ] cnt1 = SE() cnt2 = SE() cnt3 = SE() for f in funcs: for i in range(f.npars()): f.setPar(i, random.uniform(1, 5)) if hasattr(f, 'setTau'): f.setTau(random.uniform(-2, 2)) if hasattr(f, 'setTauX'): f.setTauX(random.uniform(-2, 2)) if hasattr(f, 'setTauY'): f.setTauY(random.uniform(-2, 2)) for i in range(0, 100): if 0 == i: x1, x2, y1, y2 = xmin, xmax, ymin, ymax else: x1 = random.uniform(xmin, xmax / 2) x2 = random.uniform(x1, xmax) y1 = random.uniform(ymin, ymax / 2) y2 = random.uniform(y1, ymax) for f in funcs: i1 = f.integral(x1, x2, y1, y2) i2 = integral2(f, x1, x2, y1, y2) r1 = (i1 - i2) / (abs(i1) + abs(i2)) if 1.e-5 < abs(r1): logger.error( 'I2:ERROR: difference is too large: %.6g (%.4g,%.4g,%.4g,%.4g) %s' % (r1, x1, x2, y1, y2, type(f))) cnt1 += r1 ym = 0.5 * (y1 + y2) i1 = f.integrateX(ym, x1, x2) IX = Integrate2D_X(f, x1, x2) i2 = IX(ym) r2 = (i1 - i2) / (abs(i1) + abs(i2)) if 1.e-5 < abs(r2): logger.error( 'IX:ERROR: difference is too large: %.6g (%.4g,%.4g,%.4g,%.4g) %s' % (r2, x1, x2, y1, y2, type(f))) cnt2 += r2 xm = 0.5 * (x1 + x2) i1 = f.integrateY(xm, y1, y2) IY = Integrate2D_Y(f, y1, y2) i2 = IY(xm) r3 = (i1 - i2) / (abs(i1) + abs(i2)) if 1.e-5 < abs(r3): logger.error( 'IY:ERROR: difference is too large: %.6g (%.4g,%.4g,%.4g,%.4g) %s' % (r3, x1, x2, y1, y2, type(f))) cnt3 += r3 logger.info('Counter(I2) %s' % cnt1) logger.info('Counter(IX) %s' % cnt2) logger.info('Counter(IY) %s' % cnt3)
def run_grid_interpolation ( tfunc , dct , N , low , high , scale = 1.e-8 ) : Abscissas = Ostap.Math.Interpolation.Abscissas data = points ( dct ) ## uniform abscissas i0 = interpolate ( data ) ## bernstein interpolant i1 = interpolate_bernstein ( data , None , low , high ) ## neville interpolant i2 = Ostap.Math.Neville ( data ) ## largange interpolant i3 = Ostap.Math.Lagrange ( data ) ## newton interpolant i4 = Ostap.Math.Newton ( data ) ## bspline interpolant degree = 3 ## bs = Ostap.Math.BSpline ( low , high , len ( data ) - 1 - degree , degree ) i5 = interpolate_bspline ( data , None , degree ) xx = [] for i in range ( 100000 ) : xx.append ( random.uniform ( low , high ) ) xx.sort() c0 = SE () c1 = SE () c2 = SE () c3 = SE () c4 = SE () c5 = SE () for x in xx : f = tfunc ( x ) f0 = i0 ( x ) f1 = i1 ( x ) f2 = i2 ( x ) f3 = i3 ( x ) f4 = i4 ( x ) f5 = i5 ( x ) d0 = f0 - f d1 = f1 - f d2 = f2 - f d3 = f3 - f d4 = f4 - f d5 = f5 - f c0 += abs ( d0 ) / scale c1 += abs ( d1 ) / scale c2 += abs ( d2 ) / scale c3 += abs ( d3 ) / scale c4 += abs ( d4 ) / scale c5 += abs ( d5 ) / scale logger.info ( 'Grid precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % ( scale , c0.mean () . value () , c0.rms () , c0.max () ) ) logger.info ( 'Bernstein precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % ( scale , c1.mean () . value () , c1.rms () , c1.max () ) ) logger.info ( 'Neville precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % ( scale , c2.mean () . value () , c2.rms () , c2.max () ) ) logger.info ( 'Lagrange precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % ( scale , c3.mean () . value () , c3.rms () , c3.max () ) ) logger.info ( 'Newton precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % ( scale , c4.mean () . value () , c4.rms () , c4.max () ) ) logger.info ( 'bSpline precision: mean/max[%s] = %9.2f +- %-09.1f/%-9.1f' % ( scale , c5.mean () . value () , c5.rms () , c5.max () ) ) import time i5.draw() time.sleep(5)
def test_parameterize_1D(): with ROOT.TFile.Open(data_file, 'READ') as f: tree = f.S lx = Ostap.Math.LegendreSum(4, -2, 2) ly = Ostap.Math.LegendreSum(4, -2, 2) lz = Ostap.Math.LegendreSum(4, -4, 4) lu = Ostap.Math.LegendreSum(4, -4, 6) lx.parameterize(tree, 'x', cuts) ly.parameterize(tree, 'y', cuts) lz.parameterize(tree, 'z', cuts) lu.parameterize(tree, 'u', cuts) hx = ROOT.TH1D(hID(), '', 100, -2, 2) hy = ROOT.TH1D(hID(), '', 100, -2, 2) hz = ROOT.TH1D(hID(), '', 100, -4, 4) hu = ROOT.TH1D(hID(), '', 100, -4, 6) tree.project(hx, 'x', cuts) tree.project(hy, 'y', cuts) tree.project(hz, 'z', cuts) tree.project(hu, 'u', cuts) hx.SetMinimum(0) hx.draw() lx *= 0.04 ## bin-width lx.draw('same', linecolor=2) hy.SetMinimum(0) hy.draw() ly *= 0.04 ## bin-width ly.draw('same', linecolor=2) hz.SetMinimum(0) hz.draw() lz *= 0.08 ## bin-width lz.draw('same', linecolor=2) hu.SetMinimum(0) hu.draw() lu *= 0.10 ## bin-width lu.draw('same', linecolor=2) d1 = SE() d2 = SE() d3 = SE() d4 = SE() for i in range(1000): x = random.uniform(-2, 2) y = random.uniform(-2, 2) z = random.uniform(-4, 4) u = random.uniform(-4, 6) d1 += (hx(x) - lx(x)) / max(hx(x), lx(x)) d2 += (hy(y) - ly(y)) / max(hy(y), ly(y)) d3 += (hz(z) - lz(z)) / max(hz(z), lz(z)) d4 += (hu(u) - lu(u)) / max(hu(u), lu(u)) logger.info('1D-(x)-DIFFERENCES are %s ' % d1) logger.info('1D-(y)-DIFFERENCES are %s ' % d2) logger.info('1D-(z)-DIFFERENCES are %s ' % d3) logger.info('1D-(u)-DIFFERENCES are %s ' % d4)