Exemplo n.º 1
0
    def __exit__(self, exc_type, exc_value, traceback):
        '''
        Check the results.
        '''
        if any(map(lambda e: e is not None, (exc_type, exc_value, traceback))):
            return

        if self.result is not None:

            # Print the result
            print(self.result)

            # Check that the fit run fine
            assert not self.result.fmin.hesse_failed

            # Check the values of the parameters
            results = minkit.minuit_to_registry(self.result.params)
            for n, v in self.initials.items():
                rv = results.get(n)
                assert np.allclose(v, rv.value, atol=self.nsigma * rv.error)

            # Reset the values of the PDF(s)
            if self.simultaneous:
                for c in self.proxy:
                    c.pdf.set_values(**self.initials)
            else:
                self.proxy.set_values(**self.initials)
        else:
            raise RuntimeError(
                'Must set the attribute "result" to the result from the minimization')
Exemplo n.º 2
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)
Exemplo n.º 3
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])
Exemplo n.º 4
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()
Exemplo n.º 5
0
def test_minuit_to_registry():
    '''
    Test the "minuit_to_registry" function.
    '''
    r = minkit.minuit_to_registry(pytest.shared_result.params)
    assert all(n in r.names for n in pytest.shared_names)