How to use zfit - 10 common examples

To help you get started, we’ve selected a few zfit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github marinang / lauztat / tests / calculators / test_calculator.py View on Github external
def test_with_zfit():

    import zfit
    from zfit.core.loss import UnbinnedNLL
    from zfit.minimizers.minimizer_minuit import MinuitMinimizer

    data = np.random.normal(1.2, 0.1, 10000)

    obs = zfit.Space('x', limits=(0.1, 2.0))

    mean = zfit.Parameter("mcalc", 1.2, 0.1, 2.)
    sigma = zfit.Parameter("scalc", 0.1, 0.02, 0.2)
    model = zfit.pdf.Gauss(obs=obs, mu=mean, sigma=sigma)

    data_ = zfit.data.Data.from_numpy(obs=obs, array=data)

    def lossbuilder(model, data, weights=None):
        loss = UnbinnedNLL(model=model, data=data, fit_range=[obs])
        return loss

    minimizer = MinuitMinimizer()

    config = Config(model, data_, lossbuilder, minimizer)

    calc = Calculator(config)

    assert calc.minimizer == minimizer
    calc.obsbestfit
    assert calc.obsbestfit == config.bestfit
github zfit / zfit / tests / test_strategy.py View on Github external
def test_fail_on_nan_strategy():
    sigma = zfit.Parameter("sigma", 2.)
    obs = zfit.Space("obs1", limits=(-4, 5))
    gauss = zfit.pdf.Gauss(1., sigma, obs=obs)

    sampler = gauss.create_sampler(3000)
    sampler.set_data_range(obs)
    nll = zfit.loss.UnbinnedNLL(model=gauss, data=sampler)
    minimizer = zfit.minimize.Minuit(strategy=ToyStrategyFail())
    sampler.resample()
    sigma.set_value(2.1)
    fitresult1 = minimizer.minimize(nll)
    assert fitresult1.converged

    sampler.resample()
    sigma.set_value(math.inf)
    fitresult2 = minimizer.minimize(nll)
    assert not fitresult2.converged
    assert fitresult2.edm == -999
    assert fitresult2.fmin == -999
github marinang / lauztat / tests / hypotests / test_hypotest_base.py View on Github external
def test_with_zfit():

    import zfit
    from zfit.core.loss import UnbinnedNLL
    from zfit.minimizers.minimizer_minuit import MinuitMinimizer

    data = np.random.normal(1.2, 0.1, 10000)

    obs = zfit.Space('x', limits=(0.1, 2.0))

    mean = zfit.Parameter("m_hypo", 1.2, 0.1, 2.)
    sigma = zfit.Parameter("s_hypo", 0.1, 0.02, 0.2)
    model = zfit.pdf.Gauss(obs=obs, mu=mean, sigma=sigma)

    data_ = zfit.data.Data.from_numpy(obs=obs, array=data)

    def lossbuilder(model, data, weights=None):
        loss = UnbinnedNLL(model=model, data=data, fit_range=[obs])
        return loss

    minimizer = MinuitMinimizer()

    config = Config(model, data_, lossbuilder, minimizer)

    calc = Calculator(config)

    poinull = POI(mean, value=np.linspace(1.0, 1.4, 15))

    with pytest.raises(TypeError):
github marinang / lauztat / tests / hypotests / test_upperlimit.py View on Github external
def test_with_zfit():

    import zfit
    from zfit.core.loss import ExtendedUnbinnedNLL
    from zfit.minimizers.minimizer_minuit import MinuitMinimizer

    obs = zfit.Space('x', limits=bounds)

    lambda_ = zfit.Parameter("lambda_UL", -2.0, -4.0, -0.5)
    mean = zfit.Parameter("mean_UL", 1.2, 0.1, 2., floating=False)
    sigma = zfit.Parameter("sigma_UL", 0.1, floating=False)
    Nsig = zfit.Parameter("Nsig_UL", 1., -20., len(data))
    Nbkg = zfit.Parameter("Nbkg_UL", len(data), 0., len(data)*1.1)

    model_bkg = zfit.pdf.Exponential(obs=obs, lambda_=lambda_)
    signal = Nsig * zfit.pdf.Gauss(obs=obs, mu=mean, sigma=sigma)
    background = Nbkg * model_bkg
    tot_model = signal + background

    data_ = zfit.data.Data.from_numpy(obs=obs, array=data)

    def lossbuilder(model, data, weights=None):
        constraint = zfit.constraint.nll_gaussian(params=[lambda_],
                                                  mu=[lambda_mu],
                                                  sigma=[lambda_sigma])
        loss = ExtendedUnbinnedNLL(model=model, data=data, fit_range=[obs],
                                   constraints=constraint)
        return loss

    config = Config(tot_model, data_, lossbuilder, MinuitMinimizer())

    poinull = POI(Nsig, value=np.linspace(1.0, 25, 15))
github marinang / lauztat / tests / hypotests / test_discovery.py View on Github external
def test_with_zfit():

    import zfit
    from zfit.core.loss import ExtendedUnbinnedNLL
    from zfit.minimizers.minimizer_minuit import MinuitMinimizer

    obs = zfit.Space('x', limits=bounds)

    mean = zfit.Parameter("m_disco", 1.2, 0.1, 2., floating=False)
    sigma = zfit.Parameter("s_disco", 0.1, floating=False)
    lambda_ = zfit.Parameter("l_disco", -2.0, -4.0, -1.0)
    Nsig = zfit.Parameter("Ns_disco", 20., -20., len(data))
    Nbkg = zfit.Parameter("Nbkg_disco", len(data), 0., len(data)*1.1)

    signal = Nsig * zfit.pdf.Gauss(obs=obs, mu=mean, sigma=sigma)
    background = Nbkg * zfit.pdf.Exponential(obs=obs, lambda_=lambda_)
    tot_model = signal + background

    data_ = zfit.data.Data.from_numpy(obs=obs, array=data)

    def lossbuilder(model, data):
        loss = ExtendedUnbinnedNLL(model=model, data=data, fit_range=[obs])
        return loss

    config = Config(tot_model, data_, lossbuilder, MinuitMinimizer())

    poinull = POI(Nsig, value=0)

    def test_asy():
        calc = AsymptoticCalculator(config)
        discovery_test = Discovery(poinull, calc)
github zfit / zfit / tests / test_limit.py View on Github external
assert limit2.has_rect_limits ^ bool(limit_fn)
        assert limit2.has_limits
        assert limit2.limits_are_set
        assert not limit2.limits_are_false
        inside21 = limit2.inside(2)
        inside22 = limit2.inside(2, guarantee_limits=True)
        outside2 = limit2.inside(4)
        if graph:
            equal = limit.equal(limit2, allow_graph=allow_graph)
        else:
            equal = limit == limit2

        return inside, inside2, outside, equal, inside21, inside22, outside2

    if graph:
        test = z.function(test)
    inside, inside2, outside, equal, inside21, inside22, outside2 = test(limit_fn=limit_fn)
    assert not (equal ^ bool(limit_fn))  # if a limit_fn is specified, this has precedency over the rect
    assert inside
    assert inside2
    assert not outside
    assert inside21
    assert inside22
    assert bool(outside2) ^ bool(limit_fn)  # if limit_fn, this is outside
github zfit / zfit / tests / test_coordinates.py View on Github external
with pytest.raises(ValueError):
            coords.reorder_x(x, x_axes=x_axes)
        with pytest.raises(ValueError):
            coords.reorder_x(x, func_axes=func_axes)

        coords = testclass(axes=axes)
        with pytest.raises(ValueError):
            coords.reorder_x(x, x_obs=x_obs)
        with pytest.raises(ValueError):
            coords.reorder_x(x, func_obs=func_obs)

        return x_all

    if graph:
        test = z.function(test)
    all_x = test()
    true_x = tf.constant([1, 2, 3, 4], dtype=tf.float64)
    for x in all_x:
        assert np.allclose(x, true_x)
github zfit / zfit / tests / test_operations.py View on Github external
def test_param_func():
    param1 = Parameter('param1', 1.)
    param2 = Parameter('param2', 2.)
    param3 = Parameter('param3', 3., floating=False)
    param4 = Parameter('param4', 4.)
    a = z.math.log(3. * param1) * tf.square(param2) - param3
    func = SimpleFunc(func=lambda self, x: a * x, obs=obs1)

    new_func = param4 * func

    new_func_equivalent = func * param4

    result1 = new_func.func(x=rnd_test_values).numpy()
    result1_equivalent = new_func_equivalent.func(x=rnd_test_values).numpy()
    result2 = func.func(x=rnd_test_values) * param4
    np.testing.assert_array_equal(result1, result2)
    np.testing.assert_array_equal(result1_equivalent, result2)
github zfit / zfit / tests / test_parameter.py View on Github external
def test_set_values():
    init_values = [1, 2, 3]
    second_values = [5, 6, 7]
    params = [zfit.Parameter(f'param_{i}', val) for i, val in enumerate(init_values)]

    with zfit.param.set_values(params, second_values):
        for param, val in zip(params, second_values):
            assert param.value().numpy() == val

    for param, val in zip(params, init_values):
        assert param.value().numpy() == val

    zfit.param.set_values(params, second_values)
    for param, val in zip(params, second_values):
        assert param.value().numpy() == val

    zfit.param.set_values(params, init_values)
    for param, val in zip(params, init_values):
        assert param.value().numpy() == val
github zfit / zfit / tests / test_sampling.py View on Github external
def create_gauss1():
    mu = zfit.Parameter("mu_sampling1", mu_true, mu_true - 2., mu_true + 7.)
    sigma = zfit.Parameter("sigma_sampling1", sigma_true, sigma_true - 10., sigma_true + 5.)

    gauss_params1 = zfit.pdf.Gauss(mu=mu, sigma=sigma, obs=obs1, name="gauss_params1_sampling1")
    return gauss_params1, mu, sigma