def test_rename(): def f(x, y, z): return None assert_equal(describe(f), ["x", "y", "z"]) g = rename(f, ["x", "a", "b"]) assert_equal(describe(g), ["x", "a", "b"])
def test_rename(): def f(x, y, z): return None assert describe(f) == ['x', 'y', 'z'] g = rename(f, ['x', 'a', 'b']) assert describe(g) == ['x', 'a', 'b']
def test_rename(): def f(x, y, z): return None assert describe(f) == ["x", "y", "z"] g = rename(f, ["x", "a", "b"]) assert describe(g) == ["x", "a", "b"]
def test_rename(): def f(x, y, z): return None assert describe(f) == ['x', 'y', 'z'] g = rename(f, ['x', 'a', 'b']) assert describe(g) == ['x', 'a', 'b']
def test_addpdfnorm(): def f(x, y, z): return x + 2 * y + 3 * z def g(x, z, p): return 4 * x + 5 * z + 6 * z def p(x, y, q): return 7 * x + 8 * y + 9 * q h = AddPdfNorm(f, g) assert_equal(describe(h), ['x', 'y', 'z', 'p', 'f_0']) q = AddPdfNorm(f, g, p) assert_equal(describe(q), ['x', 'y', 'z', 'p', 'q', 'f_0', 'f_1']) assert_almost_equal(h(1, 2, 3, 4, 0.1), 0.1 * f(1, 2, 3) + 0.9 * g(1, 3, 4)) assert_almost_equal(q(1, 2, 3, 4, 5, 0.1, 0.2), 0.1 * f(1, 2, 3) + 0.2 * g(1, 3, 4) + 0.7 * p(1, 2, 5))
def test_add_pdf_factor(): def f(x, y, z): return x + y + z def g(x, a, b): return 2 * (x + a + b) def k1(n1, n2): return 3 * (n1 + n2) def k2(n1, y): return 4 * (n1 + y) A = AddPdf(f, g, prefix=["f", "g"], factors=[k1, k2]) assert describe(A) == [ "x", "fy", "fz", "ga", "gb", "fn1", "fn2", "gn1", "gy" ] ret = A(1, 2, 3, 4, 5, 6, 7, 8, 9) expected = k1(6, 7) * f(1, 2, 3) + k2(8, 9) * g(1, 4, 5) assert_almost_equal(ret, expected) parts = A.eval_parts(1, 2, 3, 4, 5, 6, 7, 8, 9) assert_almost_equal(parts[0], k1(6, 7) * f(1, 2, 3)) assert_almost_equal(parts[1], k2(8, 9) * g(1, 4, 5))
def test_add_pdf_factor(): def f(x, y, z): return x + y + z def g(x, a, b): return 2 * (x + a + b) def k1(n1, n2): return 3 * (n1 + n2) def k2(n1, y): return 4 * (n1 + y) A = AddPdf(f, g, prefix=['f', 'g'], factors=[k1, k2]) assert describe(A) == [ 'x', 'fy', 'fz', 'ga', 'gb', 'fn1', 'fn2', 'gn1', 'gy' ] ret = A(1, 2, 3, 4, 5, 6, 7, 8, 9) expected = k1(6, 7) * f(1, 2, 3) + k2(8, 9) * g(1, 4, 5) assert_almost_equal(ret, expected) parts = A.eval_parts(1, 2, 3, 4, 5, 6, 7, 8, 9) assert_almost_equal(parts[0], k1(6, 7) * f(1, 2, 3)) assert_almost_equal(parts[1], k2(8, 9) * g(1, 4, 5))
def test_normalized_decorator(): @normalized((-1, 1)) def f(x, mean, sigma): return ugaussian(x, mean, sigma) g = Normalized(ugaussian, (-1, 1)) assert_equal(describe(f), ['x', 'mean', 'sigma']) assert_almost_equal(g(1, 0, 1), f(1, 0, 1))
def model(fit_range, bin): nrm_bkg_pdf = Normalized(rename(exp, ['x', 'l%d' % bin]), fit_range) ext_bkg_pdf = Extended(nrm_bkg_pdf, extname='Ncomb_%d' % bin) ext_sig_pdf = Extended(rename(gaussian, ['x', 'm%d' % bin, "sigma%d" % bin]), extname='Nsig_%d' % bin) tot_pdf = AddPdf(ext_bkg_pdf, ext_sig_pdf) print('pdf: {}'.format(describe(tot_pdf))) return tot_pdf
def test_extended_decorator(): def f(x, y, z): return x + 2 * y + 3 * z @extended() def g(x, y, z): return x + 2 * y + 3 * z assert_equal(tuple(describe(g)), ('x', 'y', 'z', 'N')) assert_equal(g(1, 2, 3, 4), 4 * (f(1, 2, 3)))
def test_normalized_decorator(): @normalized((-1, 1)) def f(x, mean, sigma): return ugaussian(x, mean, sigma) g = Normalized(ugaussian, (-1, 1)) assert describe(f) == ["x", "mean", "sigma"] assert_almost_equal(g(1, 0, 1), f(1, 0, 1))
def test_extended_decorator(): def f(x, y, z): return x + 2 * y + 3 * z @extended() def g(x, y, z): return x + 2 * y + 3 * z assert describe(g) == ["x", "y", "z", "N"] assert_equal(g(1, 2, 3, 4), 4 * (f(1, 2, 3)))
def test_extended_decorator(): def f(x, y, z): return x + 2 * y + 3 * z @extended() def g(x, y, z): return x + 2 * y + 3 * z assert_equal(tuple(describe(g)), ("x", "y", "z", "N")) assert_equal(g(1, 2, 3, 4), 4 * (f(1, 2, 3)))
def test_addpdfnorm(): def f(x, y, z): return x + 2 * y + 3 * z def g(x, z, p): return 4 * x + 5 * z + 6 * z def p(x, y, q): return 7 * x + 8 * y + 9 * q h = AddPdfNorm(f, g) assert_equal(describe(h), ["x", "y", "z", "p", "f_0"]) q = AddPdfNorm(f, g, p) assert_equal(describe(q), ["x", "y", "z", "p", "q", "f_0", "f_1"]) assert_almost_equal(h(1, 2, 3, 4, 0.1), 0.1 * f(1, 2, 3) + 0.9 * g(1, 3, 4)) assert_almost_equal(q(1, 2, 3, 4, 5, 0.1, 0.2), 0.1 * f(1, 2, 3) + 0.2 * g(1, 3, 4) + 0.7 * p(1, 2, 5))
def test_convolution(): f = gaussian g = lambda x, mu1, sigma1: gaussian(x, mu1, sigma1) h = Convolve(f, g, (-10, 10), nbins=10000) assert describe(h) == ['x', 'mean', 'sigma', 'mu1', 'sigma1'] assert_almost_equal(h(1, 0, 1, 1, 2), 0.17839457037411527) # center assert_almost_equal(h(-1, 0, 1, 1, 2), 0.119581456625684) # left assert_almost_equal(h(0, 0, 1, 1, 2), 0.1614180824489487) # left assert_almost_equal(h(2, 0, 1, 1, 2), 0.1614180824489487) # right assert_almost_equal(h(3, 0, 1, 1, 2), 0.119581456625684) # right
def test_convolution(): f = gaussian g = lambda x, mu1, sigma1: gaussian(x, mu1, sigma1) h = Convolve(f, g, (-10, 10), nbins=10000) assert describe(h) == ["x", "mean", "sigma", "mu1", "sigma1"] assert_almost_equal(h(1, 0, 1, 1, 2), 0.17839457037411527) # center assert_almost_equal(h(-1, 0, 1, 1, 2), 0.119581456625684) # left assert_almost_equal(h(0, 0, 1, 1, 2), 0.1614180824489487) # left assert_almost_equal(h(2, 0, 1, 1, 2), 0.1614180824489487) # right assert_almost_equal(h(3, 0, 1, 1, 2), 0.119581456625684) # right
def test_addpdfnorm_analytical_integrate(): def f(x, y, z): return x + 2 * y + 3 * z def g(x, z, p): return 4 * x + 5 * z + 6 * z def p(x, y, q): return 7 * x + 8 * y + 9 * q f.integrate = lambda bound, nint, y, z: 1. g.integrate = lambda bound, nint, z, p: 2. p.integrate = lambda bound, nint, y, q: 3. q = AddPdfNorm(f, g, p) assert_equal(describe(q), ['x', 'y', 'z', 'p', 'q', 'f_0', 'f_1']) integral = integrate1d(q, (-10., 10.), 100, (1., 2., 3., 4., 0.1, 0.2)) assert_almost_equal(integral, 0.1 * 1. + 0.2 * 2. + 0.7 * 3.)
def test_addpdfnorm(): def f(x, y, z): return x + 2 * y + 3 * z def g(x, z, p): return 4 * x + 5 * z + 6 * z def p(x, y, q): return 7 * x + 8 * y + 9 * q h = AddPdfNorm(f, g) assert describe(h) == ["x", "y", "z", "p", "f_0"] q = AddPdfNorm(f, g, p) assert describe(q) == ["x", "y", "z", "p", "q", "f_0", "f_1"] assert_almost_equal(h(1, 2, 3, 4, 0.1), 0.1 * f(1, 2, 3) + 0.9 * g(1, 3, 4)) assert_almost_equal( q(1, 2, 3, 4, 5, 0.1, 0.2), 0.1 * f(1, 2, 3) + 0.2 * g(1, 3, 4) + 0.7 * p(1, 2, 5), )
def test_add_pdf_factor(): def f(x, y, z): return x + y + z def g(x, a, b): return 2 * (x + a + b) def k1(n1, n2): return 3 * (n1 + n2) def k2(n1, y): return 4 * (n1 + y) A = AddPdf(f, g, prefix=['f', 'g'], factors=[k1, k2]) assert_equal(tuple(describe(A)), ('x', 'fy', 'fz', 'ga', 'gb', 'fn1', 'fn2', 'gn1', 'gy')) ret = A(1, 2, 3, 4, 5, 6, 7, 8, 9) expected = k1(6, 7) * f(1, 2, 3) + k2(8, 9) * g(1, 4, 5) assert_almost_equal(ret, expected) parts = A.eval_parts(1, 2, 3, 4, 5, 6, 7, 8, 9) assert_almost_equal(parts[0], k1(6, 7) * f(1, 2, 3)) assert_almost_equal(parts[1], k2(8, 9) * g(1, 4, 5))
def test_add_pdf_cache(): def f(x, y, z): return x + y + z def g(x, a, b): return 2 * (x + a + b) def h(x, c, a): return 3 * (x + c + a) A = AddPdf(f, g, h) assert_equal(tuple(describe(A)), ('x', 'y', 'z', 'a', 'b', 'c')) ret = A(1, 2, 3, 4, 5, 6, 7) assert_equal(A.hit, 0) expected = f(1, 2, 3) + g(1, 4, 5) + h(1, 6, 4) assert_almost_equal(ret, expected) ret = A(1, 2, 3, 6, 7, 8, 9) assert_equal(A.hit, 1) expected = f(1, 2, 3) + g(1, 6, 7) + h(1, 8, 6) assert_almost_equal(ret, expected)
def test_extended(): def f(x, y, z): return x + 2 * y + 3 * z g = Extended(f) assert_equal(tuple(describe(g)), ('x', 'y', 'z', 'N')) assert_equal(g(1, 2, 3, 4), 4 * (f(1, 2, 3))) # extended should use analytical when available def ana_int(x, y): return y * x ** 2 ana_int_int = lambda b, n, y: 999. # wrong on purpose ana_int.integrate = ana_int_int g = Extended(ana_int) assert_almost_equal(g.integrate((0, 1), 100, 5., 2.), 999.*2.) # and not fail when it's not available def no_ana_int(x, y): return y * x ** 2 g = Extended(no_ana_int) assert_almost_equal(g.integrate((0, 1), 100, 5., 2.), (1.**3) / 3.*5.*2.)
def test_add_pdf(): def f(x, y, z): return x + y + z def g(x, a, b): return 2 * (x + a + b) def h(x, c, a): return 3 * (x + c + a) A = AddPdf(f, g, h) assert_equal(tuple(describe(A)), ('x', 'y', 'z', 'a', 'b', 'c')) ret = A(1, 2, 3, 4, 5, 6, 7) expected = f(1, 2, 3) + g(1, 4, 5) + h(1, 6, 4) assert_almost_equal(ret, expected) # wrong integral on purpose f.integrate = lambda bound, nint, y, z : 1. # unbound method works too g.integrate = lambda bound, nint, a, b : 2. h.integrate = lambda bound, nint, c, a : 3. assert_equal(integrate1d(A, (-10., 10.), 100, (1., 2., 3., 4., 5.)), 6.)
def test_addpdfnorm_analytical_integrate(): def f(x, y, z): return x + 2 * y + 3 * z def g(x, z, p): return 4 * x + 5 * z + 6 * z def p(x, y, q): return 7 * x + 8 * y + 9 * q f.integrate = lambda bound, nint, y, z: 1.0 g.integrate = lambda bound, nint, z, p: 2.0 p.integrate = lambda bound, nint, y, q: 3.0 q = AddPdfNorm(f, g, p) assert_equal(describe(q), ["x", "y", "z", "p", "q", "f_0", "f_1"]) integral = integrate1d(q, (-10.0, 10.0), 100, (1.0, 2.0, 3.0, 4.0, 0.1, 0.2)) assert_almost_equal(integral, 0.1 * 1.0 + 0.2 * 2.0 + 0.7 * 3.0)
def test_addpdfnorm_analytical_integrate(): def f(x, y, z): return x + 2 * y + 3 * z def g(x, z, p): return 4 * x + 5 * z + 6 * z def p(x, y, q): return 7 * x + 8 * y + 9 * q f.integrate = lambda bound, nint, y, z: 1.0 g.integrate = lambda bound, nint, z, p: 2.0 p.integrate = lambda bound, nint, y, q: 3.0 q = AddPdfNorm(f, g, p) assert describe(q) == ["x", "y", "z", "p", "q", "f_0", "f_1"] integral = integrate1d(q, (-10.0, 10.0), 100, (1.0, 2.0, 3.0, 4.0, 0.1, 0.2)) assert_almost_equal(integral, 0.1 * 1.0 + 0.2 * 2.0 + 0.7 * 3.0)
def test_add_pdf_cache(): def f(x, y, z): return x + y + z def g(x, a, b): return 2 * (x + a + b) def h(x, c, a): return 3 * (x + c + a) A = AddPdf(f, g, h) assert describe(A) == ["x", "y", "z", "a", "b", "c"] ret = A(1, 2, 3, 4, 5, 6, 7) assert_equal(A.hit, 0) expected = f(1, 2, 3) + g(1, 4, 5) + h(1, 6, 4) assert_almost_equal(ret, expected) ret = A(1, 2, 3, 6, 7, 8, 9) assert_equal(A.hit, 1) expected = f(1, 2, 3) + g(1, 6, 7) + h(1, 8, 6) assert_almost_equal(ret, expected)
def test_add_pdf_factor(): def f(x, y, z): return x + y + z def g(x, a, b): return 2 * (x + a + b) def k1(n1, n2): return 3 * (n1 + n2) def k2(n1, y): return 4 * (n1 + y) A = AddPdf(f, g, prefix=["f", "g"], factors=[k1, k2]) assert_equal(tuple(describe(A)), ("x", "fy", "fz", "ga", "gb", "fn1", "fn2", "gn1", "gy")) ret = A(1, 2, 3, 4, 5, 6, 7, 8, 9) expected = k1(6, 7) * f(1, 2, 3) + k2(8, 9) * g(1, 4, 5) assert_almost_equal(ret, expected) parts = A.eval_parts(1, 2, 3, 4, 5, 6, 7, 8, 9) assert_almost_equal(parts[0], k1(6, 7) * f(1, 2, 3)) assert_almost_equal(parts[1], k2(8, 9) * g(1, 4, 5))
def test_add_pdf(): def f(x, y, z): return x + y + z def g(x, a, b): return 2 * (x + a + b) def h(x, c, a): return 3 * (x + c + a) A = AddPdf(f, g, h) assert_equal(tuple(describe(A)), ("x", "y", "z", "a", "b", "c")) ret = A(1, 2, 3, 4, 5, 6, 7) expected = f(1, 2, 3) + g(1, 4, 5) + h(1, 6, 4) assert_almost_equal(ret, expected) # wrong integral on purpose f.integrate = lambda bound, nint, y, z: 1.0 # unbound method works too g.integrate = lambda bound, nint, a, b: 2.0 h.integrate = lambda bound, nint, c, a: 3.0 assert_equal(integrate1d(A, (-10.0, 10.0), 100, (1.0, 2.0, 3.0, 4.0, 5.0)), 6.0)
def test_extended(): def f(x, y, z): return x + 2 * y + 3 * z g = Extended(f) assert describe(g) == ["x", "y", "z", "N"] assert_equal(g(1, 2, 3, 4), 4 * (f(1, 2, 3))) # extended should use analytical when available def ana_int(x, y): return y * x**2 ana_int_int = lambda b, n, y: 999.0 # wrong on purpose ana_int.integrate = ana_int_int g = Extended(ana_int) assert_almost_equal(g.integrate((0, 1), 100, 5.0, 2.0), 999.0 * 2.0) # and not fail when it's not available def no_ana_int(x, y): return y * x**2 g = Extended(no_ana_int) assert_almost_equal(g.integrate((0, 1), 100, 5.0, 2.0), (1.0**3) / 3.0 * 5.0 * 2.0)
def test_add_pdf(): def f(x, y, z): return x + y + z def g(x, a, b): return 2 * (x + a + b) def h(x, c, a): return 3 * (x + c + a) A = AddPdf(f, g, h) assert describe(A) == ["x", "y", "z", "a", "b", "c"] ret = A(1, 2, 3, 4, 5, 6, 7) expected = f(1, 2, 3) + g(1, 4, 5) + h(1, 6, 4) assert_almost_equal(ret, expected) # wrong integral on purpose f.integrate = lambda bound, nint, y, z: 1.0 # unbound method works too g.integrate = lambda bound, nint, a, b: 2.0 h.integrate = lambda bound, nint, c, a: 3.0 assert_equal(integrate1d(A, (-10.0, 10.0), 100, (1.0, 2.0, 3.0, 4.0, 5.0)), 6.0)
def test_describe_normal_function(): def f(x, y, z): return x + y + z d = describe(f) assert_equal(list(d), ["x", "y", "z"])
# Using a polynomial to fit a distribution is problematic, because the # polynomial can assume negative values, which results in NaN (not a number) # values in the likelihood function. # To avoid this problem we restrict the fit to the range (0, 5) where # the polynomial is clearly positive. fit_range = (0, 5) normalized_poly = probfit.Normalized(probfit.Polynomial(2), fit_range) normalized_poly = probfit.Extended(normalized_poly, extname='NBkg') gauss1 = probfit.Extended(probfit.rename(probfit.gaussian, ['x', 'mu1', 'sigma1']), extname='N1') gauss2 = probfit.Extended(probfit.rename(probfit.gaussian, ['x', 'mu2', 'sigma2']), extname='N2') # Define an extended PDF consisting of three components pdf = probfit.AddPdf(normalized_poly, gauss1, gauss2) print('normalized_poly: {}'.format(probfit.describe(normalized_poly))) print('gauss1: {}'.format(probfit.describe(gauss1))) print('gauss2: {}'.format(probfit.describe(gauss2))) print('pdf: {}'.format(probfit.describe(pdf))) # <codecell> # Define the cost function in the usual way ... binned_likelihood = probfit.BinnedLH(pdf, data_all, bins=200, extended=True, bound=fit_range) # This is a quite complex fit (11 free parameters!), so we need good starting values. # Actually we even need to set an initial parameter error # for 'mu1' and 'mu2' to make MIGRAD converge. # The initial parameter error is used as the initial step size in the minimization. pars = dict(mu1=1.9, error_mu1=0.1, sigma1=0.2, N1=3000, mu2=4.1, error_mu2=0.1, sigma2=0.1, N2=5000,
def test_describe_normal_function(): def f(x, y, z): return x + y + z assert describe(f) == ['x', 'y', 'z']
ext_sig_pdf = Extended(rename(gaussian, ['x', 'm%d' % bin, "sigma%d" % bin]), extname='Nsig_%d' % bin) tot_pdf = AddPdf(ext_bkg_pdf, ext_sig_pdf) print('pdf: {}'.format(describe(tot_pdf))) return tot_pdf fit_range = (2900, 3300) mod_1 = model(fit_range, 1) lik_1 = UnbinnedLH(mod_1, tot_m, extended=True) mod_2 = model(fit_range, 2) lik_2 = UnbinnedLH(mod_2, tot_u, extended=True) sim_lik = SimultaneousFit(lik_1, lik_2) describe(sim_lik) pars = dict(l1=0.002, Ncomb_1=1000, m1=3100, sigma1=10, Nsig_1=1000, l2=0.002, Ncomb_2=1000, m2=3100, sigma2=10, Nsig_2=1000) minuit = Minuit(sim_lik, pedantic=False, print_level=0, **pars) # In[8]: if do_probfit: start = time.time() minuit.migrad() time_probfit = time.time() - start print("starting zfit") import zfit
def test_rename(): def f(x, y, z): return None assert_equal(describe(f), ['x', 'y', 'z']) g = rename(f, ['x', 'a', 'b']) assert_equal(describe(g), ['x', 'a', 'b'])
def test_describe_normal_function(): def f(x, y, z): return x + y + z assert describe(f) == ["x", "y", "z"]
npr.seed(0) x = linspace(0,10,20) y = 3*x+15+ randn(20) err = np.array([1]*20) errorbar(x,y,err,fmt='.'); #lets define our line #first argument has to be independent variable #arguments after that are shape parameters def line(x,m,c): #define it to be parabolic or whatever you like return m*x+c #We can make it faster but for this example this is plentily fast. #We will talk about speeding things up later(you will need cython) describe(line) #cost function chi2 = Chi2Regression(line,x,y,err) #Chi^2 regression is just a callable object nothing special about it describe(chi2) #minimize it #yes it gives you a heads up that you didn't give it initial value #we can ignore it for now minimizer = iminuit.Minuit(chi2) #see iminuit tutorial on how to give initial value/range/error minimizer.migrad(); #very stable robust minimizer #you can look at your terminal to see what it is doing; #lets see our results
def test_describe_normal_function(): def f(x, y, z): return x + y + z d = describe(f) assert_equal(list(d), ['x', 'y', 'z'])
y = 3 * x + 15 + randn(20) err = np.array([1] * 20) errorbar(x, y, err, fmt='.') #lets define our line #first argument has to be independent variable #arguments after that are shape parameters def line(x, m, c): #define it to be parabolic or whatever you like return m * x + c #We can make it faster but for this example this is plentily fast. #We will talk about speeding things up later(you will need cython) describe(line) #cost function chi2 = Chi2Regression(line, x, y, err) #Chi^2 regression is just a callable object nothing special about it describe(chi2) #minimize it #yes it gives you a heads up that you didn't give it initial value #we can ignore it for now minimizer = iminuit.Minuit( chi2) #see iminuit tutorial on how to give initial value/range/error minimizer.migrad() #very stable robust minimizer #you can look at your terminal to see what it is doing;
def test_describe_normal_function(): def f(x, y, z): return x + y + z assert describe(f) == ['x', 'y', 'z']