How to use the zfit.pdf.SumPDF 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 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 / tests / test_functor_pdf.py View on Github external
def test_combine_range():
    gauss1 = zfit.pdf.Gauss(1., 4., obs=space1)
    gauss4 = zfit.pdf.Gauss(1., 4., obs=space4)
    gauss5 = zfit.pdf.Gauss(1., 4., obs=space4)

    sum1 = zfit.pdf.SumPDF(pdfs=[gauss1, gauss4], fracs=0.4)
    assert sum1.obs == (obs1, obs2)
    assert sum1.norm_range == space5

    sum1 = zfit.pdf.SumPDF(pdfs=[gauss1, gauss4, gauss5], fracs=[0.4, 0.1])
    assert sum1.obs == (obs1, obs2)
    assert sum1.norm_range == space5
github zfit / zfit / tests / test_extended.py View on Github external
def test_extract_extended_pdfs():
    gauss1 = zfit.pdf.Gauss(obs=obs1, mu=1.3, sigma=5.4)
    gauss2 = zfit.pdf.Gauss(obs=obs1, mu=1.3, sigma=5.4)
    gauss3 = zfit.pdf.Gauss(obs=obs1, mu=1.3, sigma=5.4)
    gauss4 = zfit.pdf.Gauss(obs=obs1, mu=1.3, sigma=5.4)
    gauss5 = zfit.pdf.Gauss(obs=obs1, mu=1.3, sigma=5.4)
    gauss6 = zfit.pdf.Gauss(obs=obs1, mu=1.3, sigma=5.4)

    yield1 = zfit.Parameter('yield123' + str(np.random.random()), 200.)

    # sum1 = 0.3 * gauss1 + gauss2
    gauss3_ext = gauss3.create_extended(45)
    gauss4_ext = gauss4.create_extended(100)
    sum2_ext_daughters = gauss3_ext + gauss4_ext
    sum3 = zfit.pdf.SumPDF((gauss5, gauss6), 0.4)
    sum3_ext = sum3.create_extended(yield1)

    sum_all = zfit.pdf.SumPDF(pdfs=[sum2_ext_daughters, sum3_ext])
    sum_all.set_norm_range((-5, 5))

    extracted_pdfs = extract_extended_pdfs(pdfs=sum_all)
    assert frozenset(extracted_pdfs) == {gauss3_ext, gauss4_ext, sum3_ext}

    limits = zfit.Space(obs=obs1, limits=(-4, 5))
    limits = limits.with_autofill_axes()
    extended_sample = extended_sampling(pdfs=sum_all, limits=limits)
    extended_sample_np = extended_sample.numpy()
    assert np.shape(extended_sample_np)[0] == pytest.approx(expected=(45 + 100 + 200), rel=0.1)
    samples_from_pdf = sum_all.sample(n='extended', limits=limits)
    samples_from_pdf_np = samples_from_pdf.numpy()
    assert np.shape(samples_from_pdf_np)[0] == pytest.approx(expected=(45 + 100 + 200), rel=0.1)
github zfit / zfit / tests / test_pdf_sumpdf.py View on Github external
        @zfit.supports()
        def _sample(self, n, limits):
            raise SpecificFunctionNotImplementedError  # fallback to the default sampling

    sample_size = 100000
    tolerance = 0.1
    mu1, mu2 = 0, 10
    frac = 0.9
    true_mu = mu1 * frac + mu2 * (1 - frac)

    obs = zfit.Space('obs1', (mu1 - 5, mu2 + 5))
    gauss1 = zfit.pdf.Gauss(obs=obs, mu=mu1, sigma=1)
    gauss2 = zfit.pdf.Gauss(obs=obs, mu=mu2, sigma=1)

    sumpdf = zfit.pdf.SumPDF([gauss1, gauss2], frac)
    sumpdf_true = SimpleSampleSumPDF([gauss1, gauss2], frac)

    sample = sumpdf.sample(sample_size).value().numpy()[:, 0]
    sample_true = sumpdf_true.sample(sample_size).value().numpy()[:, 0]

    assert true_mu == pytest.approx(np.mean(sample_true),
                                    abs=tolerance)  # if this is not True, it's a problem, the test is flawed
    assert true_mu == pytest.approx(np.mean(sample), abs=tolerance)
    assert np.std(sample_true) == pytest.approx(np.std(sample), abs=tolerance)

    assert scipy.stats.ks_2samp(sample_true, sample).pvalue > 0.05
github zfit / zfit / tests / test_pdf_fftconv.py View on Github external
def test_conv_simple():
    # test special properties  here
    n_points = 200
    obs = zfit.Space("obs1", limits=(-5, 5))
    param1 = zfit.Parameter('param1', -3)
    param2 = zfit.Parameter('param2', 0.3)
    gauss1 = zfit.pdf.Gauss(0., param2, obs=obs)
    func1 = zfit.pdf.Uniform(param1, param2, obs=obs)
    func2 = zfit.pdf.Uniform(-1.2, -1, obs=obs)
    func = zfit.pdf.SumPDF([func1, func2], 0.5)
    # func = zfit.pdf.(-0.1, obs=obs)
    conv = zfit.models.convolution.FFTConv1DV1(func=func,
                                               kernel=gauss1,
                                               # limits_kernel=(-1, 1)
                                               )

    x = tf.linspace(-5., 5., n_points)
    probs = conv.pdf(x=x)
    # probs = func.pdf(x=x)
    integral = conv.integrate(limits=obs)
    probs_np = probs.numpy()
    assert pytest.approx(1, rel=1e-3) == integral.numpy()
    assert len(probs_np) == n_points
    # import matplotlib.pyplot as plt
github zfit / zfit / tests / test_minimizer.py View on Github external
def create_loss(obs1):
    mu_param = zfit.Parameter("mu", 4.3, -5., 9.,
                              step_size=0.03)
    sigma_param = zfit.Parameter("sigma", 1.7, 0.01, 10, step_size=0.03)
    lambda_param = zfit.Parameter("lambda", -0.04, -0.5, -0.0003, step_size=0.001)

    gauss1 = zfit.pdf.Gauss(mu=mu_param, sigma=sigma_param, obs=obs1)
    exp1 = zfit.pdf.Exponential(lambda_=lambda_param, obs=obs1)

    sum_pdf1 = zfit.pdf.SumPDF([gauss1, exp1], 0.8)
    # load params for sampling
    with mu_param.set_value(true_mu):
        with sigma_param.set_value(true_sigma):
            with lambda_param.set_value(true_lambda):
                sampled_data = sum_pdf1.create_sampler(n=30000)
                sampled_data.resample()

                loss = zfit.loss.UnbinnedNLL(model=sum_pdf1, data=sampled_data)
                minimum = loss.value().numpy()

    return loss, minimum, (mu_param, sigma_param, lambda_param)
github zfit / zfit / tests / test_pdf_sumpdf.py View on Github external
def test_frac_behavior(yields):
    gauss1, gauss2, gauss3 = create_gaussians(yields)

    frac1 = zfit.Parameter('frac1', 0.4)
    frac2 = zfit.Parameter('frac2', 0.6)
    for fracs in [frac1, [frac1, frac2]]:
        sumpdf1 = zfit.pdf.SumPDF([gauss1, gauss2], fracs)
        assert sumpdf1.fracs[0] == frac1
        assert len(sumpdf1.fracs) == 2
        assert len(sumpdf1.params) == 2
        assert sumpdf1.params['frac_0'] == frac1
        assert sumpdf1.params['frac_1'] == sumpdf1.fracs[1]
        assert not sumpdf1.is_extended

        frac2_val = 1 - frac1.value()
        assert pytest.approx(frac2_val.numpy(), sumpdf1.params['frac_1'].value().numpy())
        if isinstance(fracs, list) and len(fracs) == 2:
            assert sumpdf1.params['frac_1'] == frac2

    frac1.set_value(0.3)
    frac3 = zfit.Parameter('frac3', 0.1)

    for fracs in [[frac1, frac2], [frac1, frac2, frac3]]:
github zfit / zfit / examples / plot_sig_bkg.py View on Github external
import zfit
import matplotlib.pyplot as plt

# 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)
frac = zfit.Parameter("fraction", 0.3, 0, 1)

# model building, pdf creation
gauss = zfit.pdf.Gauss(mu=mu, sigma=sigma, obs=obs)
exponential = zfit.pdf.Exponential(lambd, obs=obs)
model = zfit.pdf.SumPDF([gauss, exponential], fracs=frac)

# data
n_sample = 10000

exp_data = exponential.sample(n=n_sample * (1 - frac)).numpy()

gauss_data = gauss.sample(n=n_sample * frac).numpy()

data = model.create_sampler(n_sample, limits=obs)
data.resample()

mu.set_value(0.5)
sigma.set_value(1.2)
lambd.set_value(-0.05)
frac.set_value(0.07)