Beispiel #1
0
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))
Beispiel #2
0
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))
Beispiel #3
0
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 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
Beispiel #5
0
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))
Beispiel #6
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_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)
Beispiel #7
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_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.)
Beispiel #8
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 main():
    data, data_ctl = get_data()
    signal, bkg, signal_ctl, bkg_ctl = get_templates()
    edges = list( data.xedges() )
#     print list(data.y())
#     data, be = hist_to_numpy(data)
#     print data
#     data_ctl, _ = hist_to_numpy(data_ctl)
#     signal, _ = hist_to_numpy(signal)
#     signal_ctl, _ = hist_to_numpy(signal_ctl)
#     bkg, _ = hist_to_numpy(bkg)
#     bkg_ctl, _ = hist_to_numpy(bkg_ctl)
    sig_pdf = hist_to_pdf( signal )
    bkg_pdf = hist_to_pdf( bkg )
    epsig = Extended( sig_pdf, extname = 'N1' )
    epbkg = Extended( bkg_pdf, extname = 'N2' )
    pdf = AddPdf( epbkg, epsig )
    fitdata = array( list( data.y() ) )
    blh = BinnedLH( pdf, fitdata, bins = n_bins, bound = ( min_x, max_x ), extended = True )
    m = Minuit( blh, N1 = 2000, N2 = 30000, error_N1 = 44, error_N2 = 200 )
    m.migrad()
    blh.draw( m, parts = True )
Beispiel #10
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)
Beispiel #11
0
from iminuit import Minuit
from probfit import BinnedLH, Extended, AddPdf, gen_toy
from probfit.pdf import HistogramPdf
from probfit.plotting import draw_pdf
import numpy as np

bound = (0, 10)
np.random.seed(0)
bkg = gen_toy(lambda x: x**2, 100000, bound=bound)  # a parabola background
sig = np.random.randn(50000) + 5  # a Gaussian signal
data = np.concatenate([sig, bkg])
# fill histograms with large statistics
hsig, be = np.histogram(sig, bins=40, range=bound)
hbkg, be = np.histogram(bkg, bins=be, range=bound)
# randomize data
data = np.random.permutation(data)
fitdata = data[:1000]

psig = HistogramPdf(hsig, be)
pbkg = HistogramPdf(hbkg, be)
epsig = Extended(psig, extname='N1')
epbkg = Extended(pbkg, extname='N2')
pdf = AddPdf(epbkg, epsig)

blh = BinnedLH(pdf, fitdata, bins=40, bound=bound, extended=True)
m = Minuit(blh, N1=330, N2=670, error_N1=20, error_N2=30)
#m.migrad()
blh.draw(m, parts=True)
Beispiel #12
0
    hist, _ = np.histogram(tree["disc"], weights=tree["weight"], bins=bins)
    signal_templates[(yuk, lam)] = (HistogramPdf(hist, bins), np.sum(hist))

print(f"Building asimov data (SM signal + background)...")
nominal = signals[(1.0, 1.0)]
asimov_data = np.concatenate([nominal["disc"], background["disc"]])
asimov_data_weights = np.concatenate([nominal["weight"], background["weight"]])
binned_asimov, _ = np.histogram(asimov_data,
                                weights=asimov_data_weights,
                                bins=bins)
binned_asimov_errors = np.sqrt(binned_asimov + syst * binned_asimov**2)

print(f"Running fits...")
results = {}
nominal_template = AddPdf(
    Extended(signal_templates[(1.0, 1.0)][0], extname="s"),
    Extended(bkg_template[0], extname="b"),
)

nominal_lh = BinnedLH(
    nominal_template,
    bin_centers,
    bins=n_bins,
    bound=bounds,
    weights=binned_asimov,
    weighterrors=binned_asimov_errors,
    use_w2=True,
    nint_subdiv=3,
    extended=True,
)
print(f"{Style.RESET_ALL}", end="")
nominal_lh_val = 2 * nominal_lh(signal_templates[(1.0, 1.0)][1],
Beispiel #13
0
peak1 = randn(1000) * 0.5 + 1.0
peak2 = randn(500) * 0.5 + 0.0
# two peaks data with shared width
data = np.concatenate([peak1, peak2])

# Share the width
# If you use Normalized here. Do not reuse the object.
# It will be really slow due to cache miss. Read Normalized doc for more info.
pdf1 = rename(gaussian, ("x", "m_1", "sigma"))
pdf2 = rename(gaussian, ("x", "m_2", "sigma"))

ext_pdf1 = Extended(pdf1, extname="N_1")
ext_pdf2 = Extended(pdf2, extname="N_2")

compdf = AddPdf(ext_pdf1, ext_pdf2)  # merge by name (merge sigma)

ulh = BinnedLH(compdf, data, extended=True)
m = Minuit(ulh, m_1=0.1, m_2=-0.1, sigma=0.1, N_1=900, N_2=480)

plt.figure(figsize=(8, 3))
plt.subplot(121)
ulh.draw(m, parts=True)
plt.title("Before")

m.migrad()  # fit

plt.subplot(122)
ulh.draw(m, parts=True)
plt.title("After")