Esempio n. 1
0
def test_extended_unbinned_nll():
    test_values = ztf.constant(test_values_np)
    test_values = zfit.data.Data.from_tensor(obs=obs1, tensor=test_values)
    nll_object = zfit.loss.ExtendedUnbinnedNLL(model=gaussian3,
                                               data=test_values,
                                               fit_range=(-20, 20))
    minimizer = MinuitMinimizer()
    status = minimizer.minimize(loss=nll_object, params=[mu3, sigma3, yield3])
    params = status.params
    assert params[mu3]['value'] == pytest.approx(np.mean(test_values_np), rel=0.005)
    assert params[sigma3]['value'] == pytest.approx(np.std(test_values_np), rel=0.005)
    assert params[yield3]['value'] == pytest.approx(yield_true, rel=0.005)
Esempio n. 2
0
def test_unbinned_simultaneous_nll():
    test_values = tf.constant(test_values_np)
    test_values = zfit.data.Data.from_tensor(obs=obs1, tensor=test_values)
    test_values2 = tf.constant(test_values_np2)
    test_values2 = zfit.data.Data.from_tensor(obs=obs1, tensor=test_values2)

    nll_object = zfit.loss.UnbinnedNLL(model=[gaussian1, gaussian2],
                                       data=[test_values, test_values2],
                                       fit_range=[(-np.infty, np.infty), (-np.infty, np.infty)]
                                       )
    minimizer = MinuitMinimizer()
    status = minimizer.minimize(loss=nll_object, params=[mu1, sigma1, mu2, sigma2])
    params = status.params
    assert params[mu1]['value'] == pytest.approx(np.mean(test_values_np), rel=0.005)
    assert params[mu2]['value'] == pytest.approx(np.mean(test_values_np2), rel=0.005)
    assert params[sigma1]['value'] == pytest.approx(np.std(test_values_np), rel=0.005)
    assert params[sigma2]['value'] == pytest.approx(np.std(test_values_np2), rel=0.005)
Esempio n. 3
0
def test_unbinned_nll(weights):
    gaussian1, mu1, sigma1 = create_gauss1()
    gaussian2, mu2, sigma2 = create_gauss2()

    test_values = tf.constant(test_values_np)
    test_values = zfit.data.Data.from_tensor(obs=obs1, tensor=test_values, weights=weights)
    nll_object = zfit.loss.UnbinnedNLL(model=gaussian1, data=test_values, fit_range=(-np.infty, np.infty))
    minimizer = MinuitMinimizer()
    status = minimizer.minimize(loss=nll_object, params=[mu1, sigma1])
    params = status.params
    rel_error = 0.005 if weights is None else 0.1  # more fluctuating with weights

    assert params[mu1]['value'] == pytest.approx(np.mean(test_values_np), rel=rel_error)
    assert params[sigma1]['value'] == pytest.approx(np.std(test_values_np), rel=rel_error)

    constraints = zfit.constraint.nll_gaussian(params=[mu2, sigma2],
                                               mu=[mu_constr[0], sigma_constr[0]],
                                               sigma=[mu_constr[1], sigma_constr[1]])
    nll_object = UnbinnedNLL(model=gaussian2, data=test_values, fit_range=(-np.infty, np.infty),
                             constraints=constraints)

    minimizer = MinuitMinimizer()
    status = minimizer.minimize(loss=nll_object, params=[mu2, sigma2])
    params = status.params
    if weights is None:
        assert params[mu2]['value'] > np.mean(test_values_np)
        assert params[sigma2]['value'] < np.std(test_values_np)
Esempio n. 4
0
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)
        r = discovery_test.result()
        return r

    def test_freq():
        calc = FrequentistCalculator(config, ntoysnull=5000)
        calc.readtoys_from_hdf5(Nsig, "{0}/toys_Disco_Nsig.hdf5".format(pwd))
        discovery_test = Discovery(poinull, calc)
        r = discovery_test.result()
        discovery_test.plot_qdist()
        return r

    ra = test_asy()
    rf = test_freq()

    assert ra["pnull"] == pytest.approx(rf["pnull"], abs=0.05)
    assert rf["significance"] == pytest.approx(ra["significance"], abs=0.05)
    assert rf["significance"] >= 3
Esempio n. 5
0
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("mean", 1.2, 0.1, 2.)
    sigma = zfit.Parameter("sigma", 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

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

    bf = config.bestfit

    assert bf.params[mean]["value"] == pytest.approx(1.2, abs=0.01)
    assert bf.params[sigma]["value"] == pytest.approx(0.1, abs=0.01)

    # test sampling

    sampler = config.sampler(n=len(data))
    toys = config.sample(sampler, 2)
    next(toys)
    toy1 = zfit.run(sampler)

    assert np.mean(toy1) == pytest.approx(1.2, abs=0.01)
    assert np.std(toy1) == pytest.approx(0.1, abs=0.01)

    next(toys)
    toy2 = zfit.run(sampler)
    assert np.mean(toy2) == pytest.approx(1.2, abs=0.01)
    assert np.std(toy2) == pytest.approx(0.1, abs=0.01)

    assert np.mean(toy1) == pytest.approx(np.mean(toy2), abs=0.01)
    assert np.std(toy1) == pytest.approx(np.std(toy2), abs=0.01)

    with pytest.raises(StopIteration):
        next(toys)
Esempio n. 6
0
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):
        HypoTest(poinull=poinull, calculator=calc, poialt="poialt")
        HypoTest(poinull="poinull", calculator=calc)
        HypoTest(poinull=poinull, calculator="calc")
        HypoTest(poinull=calc, calculator=poinull)
        HypoTest(calculator="calc", poinull="poinull")
        HypoTest(poinull, "calc")
        HypoTest("poinull", calc)

    test = HypoTest(poinull=poinull, calculator=calc)

    assert test.calculator == calc
    assert test.poinull == poinull
    assert test.poialt is None
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("mean_ci", 1.2, 0.5, 2.0)
    sigma = zfit.Parameter("sigma_ci", 0.1, 0.02, 0.2)
    lambda_ = zfit.Parameter("lambda_ci", -2.0, -4.0, -1.0)
    Nsig = zfit.Parameter("Nsig_ci", 20., 0., len(data))
    Nbkg = zfit.Parameter("Nbkg_ci", 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):
        loss = ExtendedUnbinnedNLL(model=model, data=data, fit_range=[obs])
        return loss

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

    poinull = POI(mean, value=np.linspace(1.15, 1.26, 30))
    mean_bf = config.bestfit.params[mean]["value"]

    def test_asy():
        calc = AsymptoticCalculator(config)
        ci = ConfidenceInterval(poinull, calc)
        ret = ci.interval()
        ci.plot()
        return ret

    ra = test_asy()

    assert ra["band_m"] <= mean_bf <= ra["band_p"]
    assert ra["band_m"] == pytest.approx(1.1890518753693258, rel=0.01)
    assert ra["band_p"] == pytest.approx(1.2249924635033214, rel=0.01)
Esempio n. 8
0
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

    mean_poi = POI(mean, [1.15, 1.2, 1.25])
    mean_nll = calc.obs_nll(mean_poi)

    assert mean_nll[0] >= mean_nll[1]
    assert mean_nll[2] >= mean_nll[1]

    assert calc.obs_nll(mean_poi[0]) == mean_nll[0]
    assert calc.obs_nll(mean_poi[1]) == mean_nll[1]
    assert calc.obs_nll(mean_poi[2]) == mean_nll[2]
Esempio n. 9
0
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))
    poialt = POI(Nsig, value=0)

    def test_asy():
        calc = AsymptoticCalculator(config)
        ul_test = UpperLimit(poinull, poialt, calc, CLs=False, qtilde=True)
        ul_test.CLs = True
        ul_test.qtilde = False
        ul_test.plot()
        return ul_test.upperlimit()

    def test_freq():
        calc = FrequentistCalculator(config, ntoysnull=5000, ntoysalt=5000)
        calc.readtoys_from_hdf5(Nsig, "{0}/toys_UL_Nsig.hdf5".format(pwd))
        ul_test = UpperLimit(poinull, poialt, calc, CLs=True, qtilde=False)
        ul_test.plot()
        ul_test.plot_qdist(poinull[-1])
        return ul_test.upperlimit()

    ra = test_asy()
    rf = test_freq()

    assert ra["observed"] == pytest.approx(16.17701, abs=0.5)
    assert ra["exp"] == pytest.approx(11.6035, abs=0.5)
    assert ra["exp_p1"] == pytest.approx(16.1408, abs=0.5)
    assert ra["exp_p2"] == pytest.approx(21.6444, abs=0.5)
    assert ra["exp_m1"] == pytest.approx(8.3593, abs=0.5)
    assert ra["exp_m2"] == pytest.approx(6.2267, abs=0.5)

    assert ra["observed"] == pytest.approx(rf["observed"], abs=1.0)
    assert ra["exp"] == pytest.approx(rf["exp"], abs=2.0)
    assert ra["exp_p1"] == pytest.approx(rf["exp_p1"], abs=2.0)
    assert ra["exp_p2"] == pytest.approx(rf["exp_p2"], abs=2.0)
    assert ra["exp_m1"] == pytest.approx(rf["exp_m1"], abs=2.0)
    assert ra["exp_m2"] == pytest.approx(rf["exp_m2"], abs=2.0)
Esempio n. 10
0
def test_with_zfit():

    import zfit
    from zfit.core.loss import UnbinnedNLL
    from zfit.minimizers.minimizer_minuit import MinuitMinimizer
    zfit.settings.set_seed(45)

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

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

    mean = zfit.Parameter("m_fcalc", 1.2, 0.1, 2.5)
    sigma = zfit.Parameter("s_fcalc", 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()

    def sampler(models, *args, **kwargs):
        samplers = []
        for m in models:
            sampler = m.create_sampler(n=10000, fixed_params=[sigma])
            samplers.append(sampler)
        return samplers

    def sampling(samplers, ntoys, param, value):
        for i in range(ntoys):
            with param.set_value(value):
                for s in samplers:
                    s.resample()
            yield i

    config = Config(model, data_, lossbuilder, minimizer, sampler=sampler,
                    sample_method=sampling)

    calc = FrequentistCalculator(config, ntoysnull=100, ntoysalt=100)

    assert calc.minimizer == minimizer
    calc.obsbestfit
    assert calc.obsbestfit == config.bestfit

    mean_poi = POI(mean, [1.15, 1.2, 1.25])
    mean_nll = calc.obs_nll(mean_poi)

    assert mean_nll[0] >= mean_nll[1]
    assert mean_nll[2] >= mean_nll[1]

    assert calc.obs_nll(mean_poi[0]) == mean_nll[0]
    assert calc.obs_nll(mean_poi[1]) == mean_nll[1]
    assert calc.obs_nll(mean_poi[2]) == mean_nll[2]

    poinull = POI(mean, 1.2)
    poialt = POI(mean, 1.6)

    calc.dotoys_null(poinull, poialt)
    calc.dotoys_alt(poialt, poinull)

    pnull, palt = calc.pvalue(poinull, poialt)

    assert 0 < pnull < 0.5
    assert palt == pytest.approx(0., 0.1)

    calc.toys_to_hdf5("toys.hdf5")

    calc2 = FrequentistCalculator(config)

    calc2.readtoys_from_hdf5(mean, "toys.hdf5")

    calc.pvalue(poinull, poialt)