How to use the zfit.pdf function in zfit

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 / 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_dependencies.py View on Github external
def create_pdf(params):
        param1, param2, param3, param4 = params
        gauss1 = zfit.pdf.Gauss(param1, param2, obs=obs)
        gauss2 = zfit.pdf.Gauss(param2, param2, obs=obs)
        gauss3 = zfit.pdf.Gauss(param2, param4, obs=obs)
        gauss4 = zfit.pdf.Gauss(param3, param1, obs=obs)
        sum_pdf = zfit.pdf.SumPDF([gauss1, gauss2, gauss3, gauss4], fracs=[0.1, 0.3, 0.4])
        return sum_pdf
github zfit / zfit / tests / test_extended.py View on Github external
def test_set_yield():
    gauss6 = zfit.pdf.Gauss(obs=obs1, mu=1.3, sigma=5.4)

    yield1 = zfit.Parameter('yield123' + str(np.random.random()), 200.)
    assert not gauss6.is_extended
    gauss6.set_yield(yield1)
    assert gauss6.is_extended
github zfit / zfit / tests / test_functor_pdf.py View on Github external
def test_norm_range():
    gauss1 = zfit.pdf.Gauss(1., 4., obs=space1)
    gauss2 = zfit.pdf.Gauss(1., 4., obs=space1)
    gauss3 = zfit.pdf.Gauss(1., 4., obs=space2)

    sum1 = zfit.pdf.SumPDF(pdfs=[gauss1, gauss2], fracs=0.4)
    assert sum1.obs == (obs1,)
    assert sum1.norm_range == space1

    sum2 = zfit.pdf.SumPDF(pdfs=[gauss1, gauss3], fracs=0.34)
    assert sum2.norm_range.limits is None

    sum2.set_norm_range(space2)
    with sum2.set_norm_range(space3):
        assert sum2.norm_range == space3
    assert sum2.norm_range == space2
github zfit / zfit / examples / simple_fit.py View on Github external
#  Copyright (c) 2020 zfit

import numpy as np
import zfit

# create space
obs = zfit.Space("x", limits=(-2, 3))

# parameters
mu = zfit.Parameter("mu", 1.2, -4, 6)
sigma = zfit.Parameter("sigma", 1.3, 0.1, 10)

# model building, pdf creation
gauss = zfit.pdf.Gauss(mu=mu, sigma=sigma, obs=obs)

# data
normal_np = np.random.normal(loc=2., scale=3., size=10000)
data = zfit.Data.from_numpy(obs=obs, array=normal_np)

# create NLL
nll = zfit.loss.UnbinnedNLL(model=gauss, data=data)

# create a minimizer
minimizer = zfit.minimize.Minuit()
result = minimizer.minimize(nll)

# do the error calculations, here with minos
param_errors, _ = result.errors()
github zfit / zfit / examples / signal_bkg_mass_extended_fit.py View on Github external
import numpy as np
import zfit
import matplotlib.pyplot as plt

n_bins = 50

# create space
obs = zfit.Space("x", limits=(-10, 10))

# parameters
mu = zfit.Parameter("mu", 1., -4, 6)
sigma = zfit.Parameter("sigma", 1., 0.1, 10)
lambd = zfit.Parameter("lambda", -0.06, -1, -0.01)

# model building, pdf creation
gauss = zfit.pdf.Gauss(mu=mu, sigma=sigma, obs=obs)
exponential = zfit.pdf.Exponential(lambd, obs=obs)

n_bkg = zfit.Parameter('n_bkg', 20000)
n_sig = zfit.Parameter('n_sig', 1000)
gauss_extended = gauss.create_extended(n_sig)
exp_extended = exponential.create_extended(n_bkg)
model = zfit.pdf.SumPDF([gauss_extended, exp_extended])

# data
# n_sample = 10000

data = model.create_sampler(n=21200)
data.resample()

# set the values to a start value for the fit
mu.set_value(0.5)
github zfit / zfit / examples / cb_example.py View on Github external
n0 = zfit.Parameter("n0", 1, 0, 10.)
# n0 = zfit.Parameter("n0", 1)
n1 = zfit.Parameter("n1", 1., 0000001., 10.)
# n1 = zfit.Parameter("n1", 1)

frac = zfit.Parameter("frac", 0.5, 0., 1.)

# gauss = zfit.pdf.Gauss(obs=Bmass, mu=mu, sigma=sigma)
CB1 = zfit.pdf.CrystalBallPDF(obs=Bmass, mu=mu, sigma=sigma, alpha=a0, n=n0)
CB2 = zfit.pdf.CrystalBallPDF(obs=Bmass, mu=mu, sigma=sigma, alpha=a1, n=n1)
# CB1 = zfit.pdf.Gauss(obs=Bmass, mu=mu, sigma=sigma)
# CB2 = zfit.pdf.Gauss(obs=Bmass, mu=mu2, sigma=sigma2)

# Double Crystal Ball with a common mean and width
DoubleCB = zfit.pdf.SumPDF(pdfs=[CB1, CB2], fracs=frac)
# DoubleCB = CB1
# CB = frac*CB1 +CB2

# Create the negative log likelihood
from zfit.core.loss import UnbinnedNLL

probs = DoubleCB.pdf(x=np.linspace(5469, 5938, num=1000))

nll = UnbinnedNLL(model=[DoubleCB], data=[data], fit_range=[Bmass])
# minimize_params = [mu, sigma, a0, a1]
minimize_params = None
# tf.gradients(nll.value(), minimize_params)
# nll.value()
# tf.add_check_numerics_ops()

# Load and instantiate a tensorflow minimizer