Example #1
0
def test_display_pdfs():
    '''
    Test that the PDFs are displayed correctly as strings.
    '''
    # Define the model
    x = minkit.Parameter('x', bounds=(-5, +5))
    y = minkit.Parameter('y', bounds=(-5, +5))
    c = minkit.Parameter('c', 0, bounds=(-5, +5))

    k = minkit.Parameter('k', -0.1)

    sx = minkit.Parameter('sx', 2, bounds=(1, 3))
    sy = minkit.Parameter('sy', 1, bounds=(0.5, 3))

    gx = minkit.Gaussian('gx', x, c, sx)
    ex = minkit.Exponential('exp', x, k)
    gy = minkit.Gaussian('gy', y, c, sy)

    # Print a single PDF
    print(gx)

    # Print AddPDFs
    y = minkit.Parameter('y', 0.5)
    pdf = minkit.AddPDFs.two_components('pdf', gx, ex, y)
    print(pdf)

    # Print ProdPDFs
    pdf = minkit.ProdPDFs('pdf', [gx, gy])
    print(pdf)

    # Print ConvPDFs
    pdf = minkit.ConvPDFs('pdf', gx, gy)
    print(pdf)
Example #2
0
def test_simultaneous_minimizer():
    '''
    Test the "simultaneous_minimizer" function.
    '''
    m = minkit.Parameter('m', bounds=(10, 20))

    # Common mean
    s = minkit.Parameter('s', 1, bounds=(0.1, +3))

    # First Gaussian
    c1 = minkit.Parameter('c1', 15, bounds=(10, 20))
    g1 = minkit.Gaussian('g1', m, c1, s)

    data1 = g1.generate(size=1000)

    # Second Gaussian
    c2 = minkit.Parameter('c2', 15, bounds=(10, 20))
    g2 = minkit.Gaussian('g2', m, c2, s)

    data2 = g2.generate(size=10000)

    categories = [
        minkit.Category('uml', g1, data1),
        minkit.Category('uml', g2, data2)
    ]

    with helpers.fit_test(categories, simultaneous=True) as test:
        with minkit.simultaneous_minimizer(categories,
                                           minimizer='minuit') as minuit:
            test.result = minuit.migrad()
Example #3
0
def test_unbinned_maximum_likelihood():
    '''
    Test the "unbinned_maximum_likelihood" FCN.
    '''
    # Simple fit to a Gaussian
    m = minkit.Parameter('m', bounds=(5, 15))
    c = minkit.Parameter('c', 10., bounds=(5, 15))
    s = minkit.Parameter('s', 1., bounds=(0.5, 2))
    g = minkit.Gaussian('gaussian', m, c, s)

    data = g.generate(10000)

    with helpers.fit_test(g) as test:
        with minkit.minimizer('uml', g, data, minimizer='minuit') as minuit:
            test.result = minuit.migrad()

    # Add constraints
    cc = minkit.Parameter('cc', 10)
    sc = minkit.Parameter('sc', 0.1)
    gc = minkit.Gaussian('constraint', c, cc, sc)

    with helpers.fit_test(g) as test:
        with minkit.minimizer('uml',
                              g,
                              data,
                              minimizer='minuit',
                              constraints=[gc]) as minuit:
            test.result = minuit.migrad()
Example #4
0
def test_unbinned_extended_maximum_likelihood():
    '''
    Test the "unbinned_extended_maximum_likelihood" FCN.
    '''
    m = minkit.Parameter('m', bounds=(-5, +15))

    # Create an Exponential PDF
    k = minkit.Parameter('k', -0.1, bounds=(-0.2, 0))
    e = minkit.Exponential('exponential', m, k)

    # Create a Gaussian PDF
    c = minkit.Parameter('c', 10., bounds=(8, 12))
    s = minkit.Parameter('s', 1., bounds=(0.5, 2))
    g = minkit.Gaussian('gaussian', m, c, s)

    # Add them together
    ng = minkit.Parameter('ng', 10000, bounds=(0, 100000))
    ne = minkit.Parameter('ne', 1000, bounds=(0, 100000))
    pdf = minkit.AddPDFs.two_components('model', g, e, ng, ne)

    data = pdf.generate(int(ng.value + ne.value))

    with helpers.fit_test(pdf) as test:
        with minkit.minimizer('ueml', pdf, data, minimizer='minuit') as minuit:
            test.result = minuit.migrad()

    # Add constraints
    cc = minkit.Parameter('cc', 10)
    sc = minkit.Parameter('sc', 1)
    gc = minkit.Gaussian('constraint', c, cc, sc)

    with helpers.fit_test(pdf) as test:
        with minkit.minimizer('ueml', pdf, data, minimizer='minuit', constraints=[gc]) as minuit:
            test.result = minuit.migrad()
Example #5
0
def test_convpdfs(tmpdir):
    '''
    Test the "ConvPDFs" class.
    '''
    m = minkit.Parameter('m', bounds=(-20, +20))

    # Create two Gaussians
    c1 = minkit.Parameter('c1', 0, bounds=(-2, +2))
    s1 = minkit.Parameter('s1', 3, bounds=(0.5, +10))
    g1 = minkit.Gaussian('g1', m, c1, s1)

    c2 = minkit.Parameter('c2', 0, bounds=(-2, +2))
    s2 = minkit.Parameter('s2', 4, bounds=(0.5, +10))
    g2 = minkit.Gaussian('g2', m, c2, s2)

    pdf = minkit.ConvPDFs('convolution', g1, g2)

    data = pdf.generate(10000)

    # Check that the output is another Gaussian with bigger standard deviation
    mean = minkit.core.aop.sum(data[m.name]) / len(data)
    var = minkit.core.aop.sum((data[m.name] - mean)**2) / len(data)

    assert np.allclose(var, s1.value**2 + s2.value**2, rtol=0.1)

    # Check that the normalization is correct
    with pdf.bind() as proxy:
        assert np.allclose(proxy.integral(), 1.)
        assert np.allclose(proxy.norm(), 1.)
        assert np.allclose(proxy.numerical_normalization(), 1.)

    # Ordinary check for PDFs
    values, edges = np.histogram(minkit.as_ndarray(data[m.name]),
                                 bins=100,
                                 range=m.bounds)

    centers = minkit.DataSet.from_array(0.5 * (edges[1:] + edges[:-1]), m)

    pdf_values = minkit.plotting.scaled_pdf_values(pdf, centers, values, edges)

    assert np.allclose(np.sum(pdf_values), np.sum(values), rtol=0.01)

    # Test a fit
    with fit_test(pdf) as test:
        with minkit.minimizer('uml', pdf, data, minimizer='minuit') as minuit:
            test.result = minuit.migrad()

    # Test the JSON conversion
    with open(os.path.join(tmpdir, 'pdf.json'), 'wt') as fi:
        json.dump(minkit.pdf_to_json(pdf), fi)

    with open(os.path.join(tmpdir, 'pdf.json'), 'rt') as fi:
        s = minkit.pdf_from_json(json.load(fi))

    check_multi_pdfs(s, pdf)
Example #6
0
def test_prodpdfs(tmpdir):
    '''
    Test the "ProdPDFs" class.
    '''
    # Create two Gaussians
    mx = minkit.Parameter('mx', bounds=(-5, +5))
    cx = minkit.Parameter('cx', 0., bounds=(-2, +2))
    sx = minkit.Parameter('sx', 1., bounds=(0.1, +3))
    gx = minkit.Gaussian('gx', mx, cx, sx)

    my = minkit.Parameter('my', bounds=(-5, +5))
    cy = minkit.Parameter('cy', 0., bounds=(-2, +2))
    sy = minkit.Parameter('sy', 2., bounds=(0.5, +3))
    gy = minkit.Gaussian('gy', my, cy, sy)

    pdf = minkit.ProdPDFs('pdf', [gx, gy])

    # Test integration
    helpers.check_numerical_normalization(pdf)

    # Test consteness of the PDFs
    for p in gx.all_args:
        p.constant = True
    assert gx.constant and not pdf.constant
    for p in gy.all_args:
        p.constant = True
    assert pdf.constant

    # Test the JSON conversion
    with open(os.path.join(tmpdir, 'pdf.json'), 'wt') as fi:
        json.dump(minkit.pdf_to_json(pdf), fi)

    with open(os.path.join(tmpdir, 'pdf.json'), 'rt') as fi:
        s = minkit.pdf_from_json(json.load(fi))

    check_multi_pdfs(s, pdf)

    # Check copying the PDF
    pdf.copy()

    # Do a simple fit
    for p in pdf.all_real_args:
        p.constant = False

    data = pdf.generate(10000)

    with fit_test(pdf) as test:
        with minkit.minimizer('uml', pdf, data) as minimizer:
            test.result = minimizer.migrad()
Example #7
0
def default_add_pdfs(center='c',
                     sigma='s',
                     k='k',
                     extended=False,
                     yields=None):
    '''
    Create a combination of a Gaussian and an exponential.
    '''
    # Simple fit to a Gaussian
    x = minkit.Parameter('x', bounds=(0, 20))  # bounds to generate data later
    c = minkit.Parameter(center, 10, bounds=(8, 12))
    s = minkit.Parameter(sigma, 2, bounds=(1, 3))
    g = minkit.Gaussian('gaussian', x, c, s)

    # Test for a composed PDF
    k = minkit.Parameter(k, -0.1, bounds=(-1, 0))
    e = minkit.Exponential('exponential', x, k)

    if extended:
        ng_name, ne_name = tuple(yields if yields is not None else ('ng',
                                                                    'ne'))
        ng = minkit.Parameter(ng_name, 9000, bounds=(0, 10000))
        ne = minkit.Parameter(ne_name, 1000, bounds=(0, 10000))
        return minkit.AddPDFs.two_components('pdf', g, e, ng, ne)
    else:
        y_name = yields if yields is not None else 'y'
        y = minkit.Parameter(y_name, 0.5, bounds=(0, 1))
        return minkit.AddPDFs.two_components('pdf', g, e, y)
Example #8
0
def test_sourcepdf(tmpdir):
    '''
    Test the "SourcePDF" class.
    '''
    # Test the construction of a normal PDF
    m = minkit.Parameter('m', bounds=(-5, +5))
    c = minkit.Parameter('c', 0., bounds=(-2, +2))
    s = minkit.Parameter('s', 1., bounds=(-3, +3))
    g = minkit.Gaussian('gaussian', m, c, s)

    # Test the construction of a PDF with variable number of arguments
    m = minkit.Parameter('m', bounds=(-5, +5))
    p1 = minkit.Parameter('p1', 1.)
    p2 = minkit.Parameter('p2', 2.)
    pol0 = minkit.Polynomial('pol0', m)
    pol1 = minkit.Polynomial('pol1', m, p1)
    pol2 = minkit.Polynomial('pol2', m, p1, p2)

    # Test the JSON conversion
    with open(os.path.join(tmpdir, 'pdf.json'), 'wt') as fi:
        json.dump(minkit.pdf_to_json(pol0), fi)

    with open(os.path.join(tmpdir, 'pdf.json'), 'rt') as fi:
        s = minkit.pdf_from_json(json.load(fi))

    check_pdfs(s, pol0)
Example #9
0
def default_gaussian(pdf_name='g', data_par='x', center='c', sigma='s'):
    '''
    Create a Gaussian function.
    '''
    x = minkit.Parameter(data_par, bounds=(-4, +4))
    c = minkit.Parameter(center, 0, bounds=(-4, +4))
    s = minkit.Parameter(sigma, 1, bounds=(0.1, 2.))
    return minkit.Gaussian(pdf_name, x, c, s)
Example #10
0
def basic():
    '''
    Basic model.
    '''
    x = minkit.Parameter('x', bounds=(-5, +5))
    c = minkit.Parameter('c', 0, bounds=(-5, +5))
    s = minkit.Parameter('s', 1, bounds=(0.1, 5))
    g = minkit.Gaussian('g', x, c, s)
    return g
Example #11
0
def intermediate():
    '''
    Model composed by a single narrow Gaussian function.
    '''
    x = minkit.Parameter('x', bounds=(-5, +5))
    c = minkit.Parameter('c', 0, bounds=(-5, +5))
    s = minkit.Parameter('s', 0.5, bounds=(1e-3, 5))
    g = minkit.Gaussian('g', x, c, s)
    return g
Example #12
0
def basic():
    '''
    Basic Gaussian model.
    '''
    m = minkit.Parameter('m', bounds=(10, 20))
    c = minkit.Parameter('c', 15, bounds=(10, 20))
    s = minkit.Parameter('s', 2, bounds=(0.1, 5))
    g = minkit.Gaussian('g', m, c, s)
    return g
Example #13
0
def hard():
    '''
    Model composed by three narrow Gaussian functions.
    '''
    x1 = minkit.Parameter('x1', bounds=(-10, +10))
    c1 = minkit.Parameter('c1', -5, bounds=(-7, -3))
    s1 = minkit.Parameter('s1', 0.01, bounds=(1e-4, 0.1))
    g1 = minkit.Gaussian('g1', x1, c1, s1)

    x2 = minkit.Parameter('x2', bounds=(-10, +10))
    c2 = minkit.Parameter('c2', 0, bounds=(-2, +2))
    s2 = minkit.Parameter('s2', 0.01, bounds=(1e-4, 0.1))
    g2 = minkit.Gaussian('g2', x2, c2, s2)

    x3 = minkit.Parameter('x3', bounds=(-10, +10))
    c3 = minkit.Parameter('c3', +5, bounds=(+2, +7))
    s3 = minkit.Parameter('s3', 0.01, bounds=(1e-4, 0.1))
    g3 = minkit.Gaussian('g3', x3, c3, s3)

    return minkit.ProdPDFs('pdf', [g1, g2, g3])
Example #14
0
def test_addpdfs(tmpdir):
    '''
    Test the "AddPDFs" class.
    '''
    m = minkit.Parameter('m', bounds=(-5, +5))

    # Create an Exponential PDF
    k = minkit.Parameter('k', -0.05, bounds=(-0.1, 0))
    e = minkit.Exponential('exponential', m, k)

    # Create a Gaussian PDF
    c = minkit.Parameter('c', 0., bounds=(-2, +2))
    s = minkit.Parameter('s', 1., bounds=(-3, +3))
    g = minkit.Gaussian('gaussian', m, c, s)

    # Add them together
    g2e = minkit.Parameter('g2e', 0.5, bounds=(0, 1))
    pdf = minkit.AddPDFs.two_components('model', g, e, g2e)

    assert len(pdf.all_args) == (1 + len(g.args) + len(e.args))

    gdata = helpers.rndm_gen.normal(c.value, s.value, 100000)
    edata = helpers.rndm_gen.exponential(-1. / k.value, 100000)
    data = np.concatenate([gdata, edata])

    values, edges = np.histogram(data, bins=100, range=m.bounds)

    centers = minkit.DataSet.from_ndarray(0.5 * (edges[1:] + edges[:-1]), m)

    pdf_values = minkit.utils.core.scaled_pdf_values(pdf, centers, values,
                                                     edges)

    assert np.allclose(np.sum(pdf_values), np.sum(values))

    # Test consteness of the PDFs
    k.constant = True
    assert e.constant and not pdf.constant
    g2e.constant = True
    assert not pdf.constant
    for p in pdf.all_args:
        p.constant = True
    assert pdf.constant

    # Test the JSON conversion
    with open(os.path.join(tmpdir, 'pdf.json'), 'wt') as fi:
        json.dump(minkit.pdf_to_json(pdf), fi)

    with open(os.path.join(tmpdir, 'pdf.json'), 'rt') as fi:
        s = minkit.pdf_from_json(json.load(fi))

    check_multi_pdfs(s, pdf)

    # Check copying the PDF
    pdf.copy()
Example #15
0
def test_sweights():
    '''
    Test the "sweights" function.
    '''
    m = minkit.Parameter('m', bounds=(0, +20))

    # Create an Exponential PDF
    k = minkit.Parameter('k', -0.1, bounds=(-0.2, 0))
    e = minkit.Exponential('exponential', m, k)

    # Create a Gaussian PDF
    c = minkit.Parameter('c', 10., bounds=(0, 20))
    s = minkit.Parameter('s', 1., bounds=(0.1, 2))
    g = minkit.Gaussian('gaussian', m, c, s)

    # Add them together
    ng = minkit.Parameter('ng', 10000, bounds=(0, 100000))
    ne = minkit.Parameter('ne', 1000, bounds=(0, 100000))
    pdf = minkit.AddPDFs.two_components('model', g, e, ng, ne)

    data = pdf.generate(int(ng.value + ne.value))

    with minkit.minimizer('ueml', pdf, data, minimizer='minuit') as minuit:
        r = minuit.migrad()
        print(r)

    # Now we fix the parameters that are not yields, and we re-run the fit
    for p in (e, g):
        for a in p.args:
            a.constant = True

    with minkit.minimizer('ueml', pdf, data, minimizer='minuit') as minuit:
        r = minuit.migrad()
        print(r)

    result = minkit.minuit_to_registry(r.params)

    # Calculate the s-weights (first comes from the Gaussian, second from the exponential)
    sweights, V = minkit.sweights(pdf.pdfs,
                                  result.reduce(['ng', 'ne']),
                                  data,
                                  return_covariance=True)

    # The s-weights are normalized
    assert np.allclose(minkit.core.aop.sum(sweights[0]),
                       result.get(ng.name).value)
    assert np.allclose(minkit.core.aop.sum(sweights[1]),
                       result.get(ne.name).value)

    # The uncertainty on the yields is reflected in the s-weights
    assert np.allclose(minkit.core.aop.sum(sweights[0]**2), V[0][0])
    assert np.allclose(minkit.core.aop.sum(sweights[1]**2), V[1][1])
Example #16
0
def test_gaussian():
    '''
    Test the "Gaussian" PDF.
    '''
    m = minkit.Parameter('m', bounds=(-5, +5))
    c = minkit.Parameter('c', 0., bounds=(-2, +2))
    s = minkit.Parameter('s', 1., bounds=(-3, +3))
    g = minkit.Gaussian('gaussian', m, c, s)

    data = helpers.rndm_gen.normal(c.value, s.value, 100000)

    compare_with_numpy(g, data, m)

    helpers.check_numerical_normalization(g)
Example #17
0
def test_gaussian():
    '''
    Test the "Gaussian" PDF.
    '''
    m = minkit.Parameter('m', bounds=(-5, +5))
    c = minkit.Parameter('c', 0., bounds=(-2, +2))
    s = minkit.Parameter('s', 1., bounds=(-3, +3))
    g = minkit.Gaussian('gaussian', m, c, s)

    data = np.random.normal(c.value, s.value, 100000)

    compare_with_numpy(g, data, m)

    assert np.allclose(g.numerical_normalization(), g.norm())
Example #18
0
def test_prodpdfs(tmpdir):
    '''
    Test the "ProdPDFs" class.
    '''
    # Create two Gaussians
    mx = minkit.Parameter('mx', bounds=(-5, +5))
    cx = minkit.Parameter('cx', 0., bounds=(-2, +2))
    sx = minkit.Parameter('sx', 1., bounds=(-3, +3))
    gx = minkit.Gaussian('gx', mx, cx, sx)

    my = minkit.Parameter('my', bounds=(-5, +5))
    cy = minkit.Parameter('cy', 0., bounds=(-2, +2))
    sy = minkit.Parameter('sy', 1., bounds=(-3, +3))
    gy = minkit.Gaussian('gy', my, cy, sy)

    pdf = minkit.ProdPDFs('pdf', [gx, gy])

    # Test integration
    assert np.allclose(pdf.norm(), pdf.numerical_normalization())

    # Test consteness of the PDFs
    for p in gx.all_args:
        p.constant = True
    assert gx.constant and not pdf.constant
    for p in gy.all_args:
        p.constant = True
    assert pdf.constant

    # Test the JSON conversion
    with open(os.path.join(tmpdir, 'pdf.json'), 'wt') as fi:
        json.dump(minkit.pdf_to_json(pdf), fi)

    with open(os.path.join(tmpdir, 'pdf.json'), 'rt') as fi:
        s = minkit.pdf_from_json(json.load(fi))

    check_multi_pdfs(s, pdf)
Example #19
0
def test_minimizer():
    '''
    Test the "minimizer" function
    '''
    m = minkit.Parameter('m', bounds=(20, 80))
    c = minkit.Parameter('c', 50, bounds=(30, 70))
    s = minkit.Parameter('s', 5, bounds=(1, 10))
    g = minkit.Gaussian('gaussian', m, c, s)

    initials = g.get_values()

    arr = np.random.normal(c.value, s.value, 10000)

    data = minkit.DataSet.from_array(arr, m)

    with helpers.fit_test(g) as test:
        with minkit.minimizer('uml', g, data, minimizer='minuit') as minuit:
            test.result = pytest.shared_result = minuit.migrad()

    pytest.shared_names = [p.name for p in g.all_args]

    # Unweighted fit to uniform distribution fails
    arr = np.random.uniform(*m.bounds, 100000)
    data = minkit.DataSet.from_array(arr, m)

    with minkit.minimizer('uml', g, data, minimizer='minuit') as minuit:
        r = minuit.migrad()
        print(r)

    reg = minkit.minuit_to_registry(r.params)

    assert not np.allclose(reg.get(s.name).value, initials[s.name])

    # With weights fits correctly
    data.weights = minkit.as_ndarray(g(data))

    with helpers.fit_test(g) as test:
        with minkit.minimizer('uml', g, data, minimizer='minuit') as minuit:
            test.result = minuit.migrad()

    # Test the binned case
    data = data.make_binned(bins=100)

    with helpers.fit_test(g) as test:
        with minkit.minimizer('bml', g, data, minimizer='minuit') as minuit:
            test.result = minuit.migrad()
Example #20
0
def gaussian_model(backend):
    '''
    Return a gaussian model that can be in the minkit or RooFit backends.
    '''
    if backend == 'minkit':
        m = minkit.Parameter('m', bounds=(30, 50))
        c = minkit.Parameter('c', 40, bounds=(30, 50))
        s = minkit.Parameter('s', 5, bounds=(0.1, 10))
        return minkit.Gaussian('g', m, c, s)
    elif backend == 'roofit':
        m = rt.RooRealVar('m', 'm', 30, 50)
        c = rt.RooRealVar('c', 'c', 40, 30, 50)
        s = rt.RooRealVar('s', 's', 5, 0.1, 10)
        g = rt.RooGaussian('g', 'g', m, c, s)
        return RooFitModel(g, m, [c, s])
    else:
        raise ValueError(f'Unknown backend "{backend}"')
Example #21
0
def gaussian_constraint(backend, var, std=0.1):
    '''
    Create a gaussian constraint for the given backend and variable.
    '''

    if backend == 'minkit':
        cn, sn, gn = f'{var.name}_cc', f'{var.name}_cs', f'{var.name}_constraint'
        c = minkit.Parameter(cn, var.value)
        s = minkit.Parameter(sn, std)
        return minkit.Gaussian(gn, var, c, s)
    elif backend == 'roofit':
        cn, sn, gn = f'{var.GetName()}_cc', f'{var.GetName()}_cs', f'{var.GetName()}_constraint'
        c = rt.RooRealVar(cn, cn, var.getVal())
        s = rt.RooRealVar(sn, sn, std)
        g = rt.RooGaussian(gn, gn, var, c, s)
        return RooFitModel(g, var, [c, s])
    else:
        raise ValueError(f'Unknown backend "{backend}"')
Example #22
0
def test_binned_chisquare():
    '''
    Test the "binned_chisquare" FCN.
    '''
    # Single PDF
    m = minkit.Parameter('m', bounds=(0, 20))
    c = minkit.Parameter('c', 10., bounds=(8, 12))
    # all bins must be highly populated
    s = minkit.Parameter('s', 3., bounds=(2, 7))
    g = minkit.Gaussian('gaussian', m, c, s)

    data = g.generate(10000)

    values, edges = np.histogram(
        data[m.name].as_ndarray(), range=m.bounds, bins=100)

    data = minkit.BinnedDataSet.from_ndarray(edges, m, values)

    with helpers.fit_test(g) as test:
        with minkit.minimizer('chi2', g, data) as minimizer:
            test.result = minimizer.migrad()

    # Many PDfs
    k = minkit.Parameter('k', -0.1, bounds=(-1, 0))
    e = minkit.Exponential('exponential', m, k)

    ng = minkit.Parameter('ng', 10000, bounds=(0, 100000))
    ne = minkit.Parameter('ne', 1000, bounds=(0, 100000))

    pdf = minkit.AddPDFs.two_components('pdf', g, e, ng, ne)

    data = pdf.generate(int(ng.value + ne.value))

    values, edges = np.histogram(
        data[m.name].as_ndarray(), range=m.bounds, bins=100)

    data = minkit.BinnedDataSet.from_ndarray(edges, m, values)

    with helpers.fit_test(pdf) as test:
        with minkit.minimizer('chi2', pdf, data) as minimizer:
            test.result = minimizer.migrad()
Example #23
0
def test_scipyminimizer():
    '''
    Test the "SciPyMinimizer" class.
    '''
    m = minkit.Parameter('m', bounds=(10, 20))
    s = minkit.Parameter('s', 1, bounds=(0.5, 2))
    c = minkit.Parameter('c', 15, bounds=(10, 20))
    g = minkit.Gaussian('g', m, c, s)

    # Test the unbinned case
    data = g.generate(10000)

    values = []
    with minkit.minimizer('uml', g, data, minimizer='scipy') as minimizer:
        for m in minkit.minimizers.SCIPY_CHOICES:
            values.append(
                minimizer.result_to_registry(minimizer.minimize(method=m)))

    with minkit.minimizer('uml', g, data, minimizer='minuit') as minimizer:
        reference = minkit.minuit_to_registry(minimizer.migrad().params)

    for reg in values:
        for p, r in zip(reg, reference):
            helpers.check_parameters(p, r, rtol=0.01)

    # Test the binned case
    data = data.make_binned(bins=100)

    values = []
    with minkit.minimizer('bml', g, data, minimizer='scipy') as minimizer:
        for m in minkit.minimizers.SCIPY_CHOICES:
            values.append(
                minimizer.result_to_registry(minimizer.minimize(method=m)))

    with minkit.minimizer('bml', g, data, minimizer='minuit') as minimizer:
        reference = minkit.minuit_to_registry(minimizer.migrad().params)

    for reg in values:
        for p, r in zip(reg, reference):
            helpers.check_parameters(p, r, rtol=0.01)
Example #24
0
def test_formula(tmpdir):
    '''
    Test the "Formula" class.
    '''
    a = minkit.Parameter('a', 1)
    b = minkit.Parameter('b', 2)
    c = minkit.Formula('c', 'a * b', [a, b])

    assert np.allclose(c.value, a.value * b.value)

    # Test its use on a PDF
    m = minkit.Parameter('m', bounds=(10, 20))
    c = minkit.Parameter('c', 15, bounds=(10, 20))
    s = minkit.Formula('s', '0.1 + c / 10', [c])
    g = minkit.Gaussian('gaussian', m, c, s)

    data = g.generate(10000)

    nd = np.random.normal(c.value, s.value, 10000)

    compare_with_numpy(g, nd, m)

    with helpers.fit_test(g) as test:
        with minkit.minimizer('uml', g, data, minimizer='minuit') as minuit:
            test.result = minuit.migrad()

    # Test the JSON (only for formula)
    with open(os.path.join(tmpdir, 'r.json'), 'wt') as fi:
        json.dump(s.to_json_object(), fi)

    with open(os.path.join(tmpdir, 'r.json'), 'rt') as fi:
        s = minkit.Formula.from_json_object(json.load(fi), g.all_real_args)

    # Test the JSON (whole PDF)
    with open(os.path.join(tmpdir, 'pdf.json'), 'wt') as fi:
        json.dump(minkit.pdf_to_json(g), fi)

    with open(os.path.join(tmpdir, 'pdf.json'), 'rt') as fi:
        s = minkit.pdf_from_json(json.load(fi))
Example #25
0
def test_evaluation():
    '''
    Test the methods used for evaluation of the PDF.
    '''
    m = minkit.Parameter('m', bounds=(-5, +5))
    c = minkit.Parameter('c', 0., bounds=(-2, +2))
    s = minkit.Parameter('s', 1., bounds=(-3, +3))
    g = minkit.Gaussian('gaussian', m, c, s)

    m.set_range('reduced', (-3, +3))

    assert not np.allclose(g.function(), g.function('reduced'))

    data = g.generate(1000)

    g(data)  # normal evaluation

    binned_data = data.make_binned(100)

    bv = g.evaluate_binned(binned_data)  # evaluation on a binned data set

    assert np.allclose(bv.sum(), 1.)
Example #26
0
def test_constpdf(tmpdir):
    '''
    Test a fit with a constant PDF.
    '''
    m = minkit.Parameter('m', bounds=(0, 10))

    # Create an Exponential PDF
    k = minkit.Parameter('k', -0.05)
    e = minkit.Exponential('exponential', m, k)

    # Create a Gaussian PDF
    c = minkit.Parameter('c', 5., bounds=(0, 10))
    s = minkit.Parameter('s', 1., bounds=(0.5, 3))
    g = minkit.Gaussian('gaussian', m, c, s)

    # Add them together
    g2e = minkit.Parameter('g2e', 0.5, bounds=(0, 1))
    pdf = minkit.AddPDFs.two_components('model', g, e, g2e)

    # Check for "get_values" and "set_values"
    p = pdf.norm()
    pdf.set_values(**pdf.get_values())
    assert np.allclose(p, pdf.norm())

    # Test a simple fit
    data = pdf.generate(10000)

    with fit_test(pdf) as test:
        with minkit.minimizer('uml', pdf, data, minimizer='minuit') as minuit:
            test.result = minuit.migrad()

    # Test the JSON conversion
    with open(os.path.join(tmpdir, 'pdf.json'), 'wt') as fi:
        json.dump(minkit.pdf_to_json(pdf), fi)

    with open(os.path.join(tmpdir, 'pdf.json'), 'rt') as fi:
        s = minkit.pdf_from_json(json.load(fi))

    check_multi_pdfs(s, pdf)
Example #27
0
def test_restoring_state():
    '''
    Test that the state of the PDFs is treated correctly.
    '''
    m = minkit.Parameter('m', bounds=(10, 20))
    c = minkit.Parameter('c', 15, bounds=(10, 20))
    s = minkit.Formula('s', '0.1 * {c}', [c])
    g = minkit.Gaussian('g', m, c, s)

    data = g.generate(10000)

    with minkit.minimizer('uml', g, data) as minuit:
        minuit.migrad()
        result = g.args.copy()

    data = g.generate(10000)  # new data set

    with g.restoring_state(), minkit.minimizer('uml', g, data) as minuit:
        minuit.migrad()

    # The values of the PDF must be those of the first minimization
    for f, s in zip(result, g.real_args):
        helpers.check_parameters(f, s)
Example #28
0
def test_bind_class_arguments():
    '''
    Test the "bind_class_arguments" function.
    '''
    m = minkit.Parameter('m', bounds=(-5, +5))
    c = minkit.Parameter('c', 0., bounds=(-2, +2))
    s = minkit.Parameter('s', 1., bounds=(-3, +3))
    g = minkit.Gaussian('gaussian', m, c, s)

    m.set_range('sides', ((-5, -2), (+2, +5)))

    data = g.generate(10000)

    # Single call
    with g.bind() as proxy:
        proxy(data)

    # Call with arguments
    with g.bind(range='sides') as proxy:
        proxy(data)

    # Use same arguments as in bind
    with g.bind(range='sides') as proxy:
        proxy(data, range='sides')

    # Use different arguments as in bind (raises error)
    with g.bind(range='sides') as proxy:
        with pytest.raises(ValueError):
            proxy(data, range='full')

    # Same tests with positionals
    with g.bind(range='sides') as proxy:
        proxy(data, 'sides')

    with g.bind(range='sides') as proxy:
        with pytest.raises(ValueError):
            proxy(data, 'full')
Example #29
0
def test_formula(tmpdir):
    '''
    Test the "Formula" class.
    '''
    a = minkit.Parameter('a', 1)
    b = minkit.Parameter('b', 2)
    c = minkit.Formula('c', '{a} * {b}', [a, b])

    assert np.allclose(c.value, a.value * b.value)

    # Test its use on a PDF
    m = minkit.Parameter('m', bounds=(10, 20))
    c = minkit.Parameter('c', 15, bounds=(10, 20))
    s = minkit.Formula('s', '0.1 + {c} / 10', [c])
    g = minkit.Gaussian('gaussian', m, c, s)

    data = g.generate(10000)

    nd = rndm_gen.normal(c.value, s.value, 10000)

    compare_with_numpy(g, nd, m)

    with helpers.fit_test(g) as test:
        with minkit.minimizer('uml', g, data, minimizer='minuit') as minuit:
            test.result = minuit.migrad()

    # Test the JSON (only for formula)
    with open(os.path.join(tmpdir, 'r.json'), 'wt') as fi:
        json.dump(s.to_json_object(), fi)

    with open(os.path.join(tmpdir, 'r.json'), 'rt') as fi:
        s = minkit.Formula.from_json_object(json.load(fi), g.all_real_args)

    # Test the JSON (whole PDF)
    with open(os.path.join(tmpdir, 'pdf.json'), 'wt') as fi:
        json.dump(minkit.pdf_to_json(g), fi)

    with open(os.path.join(tmpdir, 'pdf.json'), 'rt') as fi:
        minkit.pdf_from_json(json.load(fi))

    # Test the copy of a formula
    new_args = s.args.copy()

    assert all(not o is p for o, p in zip(s.args, s.copy(new_args).args))

    # Test for a formula depending on another formula
    m = minkit.Parameter('m', bounds=(10, 20))
    c = minkit.Parameter('c', 15, bounds=(10, 20))
    d = minkit.Formula('d', '0.1 + {c} / 10', [c])
    s = minkit.Formula('s', '2 * {d}', [d])
    g = minkit.Gaussian('gaussian', m, c, s)

    assert s.value == 3.2

    data = g.generate(10000)

    with helpers.fit_test(g) as test:
        with minkit.minimizer('uml', g, data, minimizer='minuit') as minuit:
            test.result = minuit.migrad()

    # Test the JSON (only for formula)
    with open(os.path.join(tmpdir, 'r.json'), 'wt') as fi:
        json.dump(s.to_json_object(), fi)

    with open(os.path.join(tmpdir, 'r.json'), 'rt') as fi:
        s = minkit.Formula.from_json_object(json.load(fi), g.all_args)

    # Test the copy of a formula depending on another formula
    new_args = s.args.copy()

    assert all(not o is p for o, p in zip(s.args, s.copy(new_args).args))

    # Test the JSON (whole PDF)
    with open(os.path.join(tmpdir, 'pdf.json'), 'wt') as fi:
        json.dump(minkit.pdf_to_json(g), fi)

    with open(os.path.join(tmpdir, 'pdf.json'), 'rt') as fi:
        minkit.pdf_from_json(json.load(fi))
Example #30
0
def test_binned_maximum_likelihood():
    '''
    Tets the "binned_maximum_likelihood" FCN.
    '''
    # Simple fit to a Gaussian
    m = minkit.Parameter('m', bounds=(5, 15))
    c = minkit.Parameter('c', 10., bounds=(8, 12))
    s = minkit.Parameter('s', 1., bounds=(0.5, 2))
    g = minkit.Gaussian('gaussian', m, c, s)

    values, edges = np.histogram(np.random.normal(c.value, s.value, 10000),
                                 bins=100)

    data = minkit.BinnedDataSet.from_array(edges, m, values)

    with helpers.fit_test(g) as test:
        with minkit.minimizer('bml', g, data, minimizer='minuit') as minuit:
            test.result = minuit.migrad()

    # Add constraints
    cc = minkit.Parameter('cc', 10)
    sc = minkit.Parameter('sc', 0.1)
    gc = minkit.Gaussian('constraint', c, cc, sc)

    with helpers.fit_test(g) as test:
        with minkit.minimizer('bml',
                              g,
                              data,
                              minimizer='minuit',
                              constraints=[gc]) as minuit:
            test.result = minuit.migrad()

    # Test for a composed PDF
    k = minkit.Parameter('k', -0.1, bounds=(-1, 0))
    e = minkit.Exponential('e', m, k)

    y = minkit.Parameter('y', 0.5, bounds=(0, 1))

    pdf = minkit.AddPDFs.two_components('pdf', g, e, y)

    data = pdf.generate(10000)

    values, edges = np.histogram(minkit.as_ndarray(data[m.name]), bins=100)

    data = minkit.BinnedDataSet.from_array(edges, m, values)

    with helpers.fit_test(pdf) as test:
        with minkit.minimizer('bml', pdf, data) as minimizer:
            test.result = minimizer.migrad()

    # Test for a PDF with no "evaluate_binned" function defined
    m = minkit.Parameter('m', bounds=(0, 10))
    a = minkit.Parameter('a', 0)
    theta = minkit.Parameter('theta', 2, bounds=(0, 3))
    alpha = minkit.Parameter('alpha', 0.5)
    beta = minkit.Parameter('beta', 2)
    pdf = minkit.Amoroso('amoroso', m, a, theta, alpha, beta)

    data = pdf.generate(1000)

    values, edges = np.histogram(minkit.as_ndarray(data[m.name]),
                                 range=m.bounds,
                                 bins=100)

    data = minkit.BinnedDataSet.from_array(edges, m, values)

    with helpers.fit_test(pdf) as test:
        with minkit.minimizer('bml', pdf, data) as minimizer:
            test.result = minimizer.migrad()