コード例 #1
0
ファイル: core.py プロジェクト: mzks/zfitter
 def set_range(self, lower, upper):
     self.fit_lower = lower
     self.fit_upper = upper
     self._calc_fit_bins()
     self.obs = zfit.Space('x', limits=(self.fit_lower, self.fit_upper))
コード例 #2
0
ファイル: tobinned.py プロジェクト: zfit/zfit
 def integrate_one(limits):
     l, u = tf.unstack(limits)
     limits_space = zfit.Space(obs=self.obs, limits=[l, u])
     return pdf.integrate(limits_space, norm=False, options=options)
コード例 #3
0
ファイル: FOM_optimisation.py プロジェクト: abuonaura/myRLc
    cond   = cond & (abs(df['Lb_TRUEID']) == 5122) & (abs(df['Lc_TRUEID']) == 4122) & (abs(df['p_TRUEID']) == 2212) & (abs(df['K_TRUEID']) == 321) & (abs(df['pi_TRUEID']) == 211) & (abs(df['mu_TRUEID']) == 13)
    cond   = cond & (abs(df['mu_MC_MOTHER_ID']) == 15) & (df['Lc_BKGCAT'] < 30)
    #Below cuts only for FOM optimisation (ie. define signal region)
    cond   = cond & (df['Lc_M'] > limits[0][0][0]) & (df['Lc_M'] < limits[1][0][0])
    num_bef   = np.sum(df['Event_PIDCalibEffWeight'])
    num_after = np.sum(df[cond]['Event_PIDCalibEffWeight'])
    df     = df[cond]
    return df[['Lc_M', 'bdt', 'Event_PIDCalibEffWeight']], num_after, num_bef
###################

##################
#define space to fit in the upperside band region
upper       = (( 2327.,),)
lower       = (( 2310.,),); 
limits      = (lower, upper)
obs         = zfit.Space(obs=['Lc_M'], limits=limits)
#define a space which includes full sig + bkg regs
lowerfull   = (( 2260.,),)
upperfull   = (( 2327.,),)
limitsfull  = (lowerfull, upperfull)
obsfull     = zfit.Space(obs=['Lc_M'], limits=limitsfull)
##################

##################- Get real data read_root
path_data   = '/disk/lhcb_data2/amathad/Lb2Lclnu_analysis/Data/'
vars_data   = ['Lc_M', 'bdt', 'Lb_L0Global_TIS', 'Lc_L0HadronDecision_TOS', 'Lc_Hlt1TrackMVADecision_TOS', 'Lc_Hlt1TwoTrackMVADecision_TOS', 'Lb_Hlt2XcMuXForTauB2XcMuDecision_TOS', '*_PT', '*_P', 'nTracks', 'mu_PID*', 'mu_isMuon']
dfu = GetData(path_data+"Data_MagUp_2016_MVA.root",   vars_data, limits = limits)
dfd = GetData(path_data+"Data_MagDown_2016_MVA.root", vars_data, limits = limits)
data_df  = pd.concat([dfu,dfd], ignore_index=True)
print('Total Data', data_df.shape)
#plt.hist((zfit.run(data)).ravel(), bins=200)
コード例 #4
0
ファイル: test_pdf_template.py プロジェクト: zfit/zfit
def test_morphing_templates(alphas):
    bins1 = 15
    irregular_str = "irregular templates" if alphas is not None else ""

    counts1 = np.random.uniform(70, high=100, size=bins1)  # generate counts
    counts = [
        counts1 - np.random.uniform(high=20, size=bins1),
        counts1,
        counts1 + np.random.uniform(high=20, size=bins1),
    ]
    if alphas is not None:
        counts.append(counts1 + np.random.uniform(high=5, size=bins1))
    binning = zfit.binned.RegularBinning(bins1, 0, 10, name="obs1")
    obs = zfit.Space(obs="obs1", binning=binning)
    histos = [BinnedData.from_tensor(obs, count) for count in counts]
    pdfs = [zfit.pdf.HistogramPDF(h) for h in histos]
    if alphas is not None:
        pdfs = {a: p for a, p in zip(alphas, pdfs)}
    alpha = zfit.Parameter("alpha", 0, -5, 5)
    morph = SplineMorphingPDF(alpha=alpha, hists=pdfs)
    if alphas is None:
        alphas = [-1, 0, 1]
    for i, a in enumerate(alphas):
        alpha.set_value(a)
        np.testing.assert_allclose(morph.counts(), counts[i])
        if len(alphas) > i + 1:
            alpha.set_value((a + alphas[i + 1]) / 2)
            max_dist = (counts[i] - counts[i + 1])**2 + 5  # tolerance
            max_dist *= 1.1  # not strict, it can be a bit higher
            numpy.testing.assert_array_less((morph.counts() - counts[i])**2,
                                            max_dist)
            numpy.testing.assert_array_less(
                (morph.counts() - counts[i + 1])**2, max_dist)

    import matplotlib.cm as cm

    amin, amax = -2, 2
    n = 5

    template_alphas = np.array(list(alphas))

    for do_3d in [True, False]:
        plt.figure()
        if do_3d:
            ax = plt.gcf().add_subplot(111, projection="3d")
        else:
            ax = plt.gca()
        plotstyle = "3d plot" if do_3d else "hist plot"
        plt.title(f"Morphing with splines {irregular_str} {plotstyle}")

        for a in list(znp.linspace(amin, amax, n * 2)) + list(template_alphas):
            normed_a = (a - amin) / (amax -
                                     amin) / 1.3  # 3 is a scaling factor
            color = cm.get_cmap("winter")(normed_a)
            alpha.set_value(a)
            histo = morph.ext_pdf(None)
            histo = BinnedData.from_tensor(obs, histo)
            histo = histo.to_hist()
            values = histo.values()
            x = histo.axes.edges[0][:-1]
            y = np.broadcast_to(a, values.shape)
            z = values
            label = None
            if do_3d:
                ax.step(x, y, z, color=color, where="pre", label=label)
            else:
                if np.min((a - template_alphas)**2) < 0.0001:
                    label = f"alpha={a}"
                mplhep.histplot(histo, label=label, color=color)
        ax.set_xlabel("observable")
        ax.set_ylabel("alpha")
        if do_3d:
            ax.set_zlabel("ext_pdf")
        plt.legend()
        pytest.zfit_savefig()
コード例 #5
0
ファイル: test_pdf_conditional.py プロジェクト: tgag17/zfit
def test_conditional_pdf_simple():
    xobs = zfit.Space('x', (-2, 5))
    muobs = zfit.Space('y', (-1, 15))
    sigmaobs = zfit.Space('z', (0.2, 13))

    mu = zfit.Parameter('mu', 2, -5, 25)

    xmuobs = xobs * muobs
    obs = xobs * muobs * sigmaobs
    nsample = 5000
    uniform_sample = np.random.uniform(size=(nsample, 1), low=-1, high=4)
    normal_sample1 = np.random.normal(loc=2, scale=3.5, size=(nsample, 1))
    normal_sample2 = np.abs(np.random.normal(loc=5, scale=2, size=(nsample, 1))) + 0.9
    sigma = zfit.Parameter('sigma', 3, 0.1, 15)

    gauss = zfit.pdf.Gauss(obs=xobs, mu=mu, sigma=sigma)
    # gauss = zfit.pdf.Gauss(xmuobs=muobs, mu=mu, sigma=sigma)

    data2d = zfit.Data.from_numpy(array=np.stack([uniform_sample, normal_sample1], axis=-1),
                                  obs=xmuobs)
    data1d = zfit.Data.from_numpy(array=uniform_sample, obs=xobs)
    data3d = zfit.Data.from_numpy(array=np.stack([uniform_sample, normal_sample1, normal_sample2], axis=-1),
                                  obs=obs)
    data1dmu = zfit.Data.from_numpy(array=uniform_sample, obs=muobs)

    cond_gauss2d = zfit.pdf.ConditionalPDFV1(pdf=gauss, cond={mu: muobs}, use_vectorized_map=False)
    prob2 = cond_gauss2d.pdf(data2d)
    assert prob2.shape[0] == data2d.nevents
    assert prob2.shape.rank == 1

    prob_check2 = np.ones_like(prob2)
    for i, (xval, muval) in enumerate(data2d.value()):
        mu.assign(muval)
        prob_check2[i] = gauss.pdf(xval)
    np.testing.assert_allclose(prob_check2, prob2, rtol=1e-5)

    cond_gauss1d = zfit.pdf.ConditionalPDFV1(pdf=gauss, cond={mu: xobs})
    prob1 = cond_gauss1d.pdf(data1d)
    assert prob1.shape[0] == data1d.nevents
    assert prob1.shape.rank == 1

    prob_check1 = np.ones_like(prob1)
    for i, xval in enumerate(data1d.value()[:, 0]):
        mu.assign(xval)
        prob_check1[i] = gauss.pdf(xval)
    np.testing.assert_allclose(prob_check1, prob1, rtol=1e-5)

    cond_gauss3d = zfit.pdf.ConditionalPDFV1(pdf=gauss, cond={mu: muobs, sigma: sigmaobs})
    prob3 = cond_gauss3d.pdf(data3d)
    assert prob3.shape[0] == data3d.nevents
    assert prob3.shape.rank == 1

    prob_check3 = np.ones_like(prob3)
    for i, (xval, muval, sigmaval) in enumerate(data3d.value()):
        zfit.param.assign_values([mu, sigma], [muval, sigmaval])
        prob_check3[i] = gauss.pdf(xval)
    np.testing.assert_allclose(prob_check3, prob3, rtol=1e-5)

    nll = zfit.loss.UnbinnedNLL(model=cond_gauss2d, data=data2d)
    nll.value()

    minimizer = zfit.minimize.Minuit()
    result = minimizer.minimize(nll)
    assert result.valid

    integrals2 = cond_gauss2d.integrate(limits=xobs, x=data2d)
    assert integrals2.shape[0] == data2d.nevents
    assert integrals2.shape.rank == 1
    np.testing.assert_allclose(integrals2, np.ones_like(integrals2), atol=1e-5)

    sample2 = cond_gauss2d.sample(n=data1dmu.nevents, limits=xobs, x=data1dmu)
    assert sample2.value().shape == (data1dmu.nevents, cond_gauss2d.n_obs)
コード例 #6
0
ファイル: test_pdf_polynomials.py プロジェクト: zfit/zfit
#  Copyright (c) 2022 zfit
import copy

import numpy as np
import pytest
import tensorflow as tf

import zfit

obs1_random = zfit.Space(obs="obs1", limits=(-1.5, 1.2))
obs1 = zfit.Space(obs="obs1", limits=(-1, 1))

coeffs_parametrization = [
    1.4,
    [0.6],
    [1.42, 1.2],
    [0.2, 0.8, 0.5],
    [8.1, 1.4, 3.6, 4.1],
    [1.1, 1.42, 1.2, 0.4, 0.7],
    [11.1, 1.4, 5.6, 3.1, 18.1, 3.1],
]

rel_integral = 7e-2
default_sampling = 100000

poly_pdfs = [
    (zfit.pdf.Legendre, default_sampling),
    (zfit.pdf.Chebyshev, default_sampling),
    (zfit.pdf.Chebyshev2, default_sampling),
    (zfit.pdf.Hermite, default_sampling * 20),
    (zfit.pdf.Laguerre, default_sampling * 20),
コード例 #7
0
ファイル: test_pdf_poisson.py プロジェクト: tgag17/zfit
#  Copyright (c) 2021 zfit
import numpy as np
import pytest

import zfit
from zfit import Parameter
from zfit.models.dist_tfp import Poisson

lamb_true = 50

obs = zfit.Space(obs="Nobs", limits=(0, 200))

test_values = np.random.uniform(low=0, high=100, size=100)


def create_poisson():
    N = Parameter("N", lamb_true)
    poisson = Poisson(obs=obs, lamb=N)
    return poisson


def create_poisson_composed_rate():
    N1 = Parameter("N1", lamb_true / 2)
    N2 = Parameter("N2", lamb_true / 2)
    N = zfit.param.ComposedParameter("N",
                                     lambda n1, n2: n1 + n2,
                                     params=[N1, N2])

    poisson = Poisson(obs=obs, lamb=N)
    return poisson
コード例 #8
0
    param1 = params['super_param']
    param2 = params['param2']
    param3 = params['param3']

    lower = z.convert_to_tensor(lower)
    upper = z.convert_to_tensor(upper)

    # calculate the integral here, dummy integral, wrong!
    integral = param1 * param2 * param3 + z.reduce_sum([lower, upper])
    return integral


# define the space over which it is defined. Here, we use the axes
lower_full = (-10, zfit.Space.ANY_LOWER)
upper_full = (10, zfit.Space.ANY_UPPER)
integral_full_limits = zfit.Space(axes=(0, 1),
                                  limits=(lower_full, upper_full))

CustomPDF2D.register_analytic_integral(func=integral_full,
                                       limits=integral_full_limits)


# define the partial integral function
def integral_axis1(x, limits, norm_range, params, model):
    data_0 = x.unstack_x()  # data from axis 0

    param1 = params['super_param']
    param2 = params['param2']
    param3 = params['param3']

    lower, upper = limits.limit1d
    lower = z.convert_to_tensor(lower)  # the limits are now 1-D, for axis 1
コード例 #9
0
from zfit.core.space import Limit
from zfit.util.exception import AnalyticSamplingNotImplemented


@pytest.fixture(autouse=True, scope="module")
def setup_teardown_vectors():
    Limit._experimental_allow_vectors = True
    yield
    Limit._experimental_allow_vectors = False


mu_true = 1.5
sigma_true = 1.2
low, high = -3.8, 2.9

obs1 = zfit.Space("obs1", (low, high))


class GaussNoAnalyticSampling(zfit.pdf.Gauss):
    def _analytic_sample(self, n, limits: Space):
        raise AnalyticSamplingNotImplemented  # HACK do make importance sampling work


class UniformNoAnalyticSampling(zfit.pdf.Uniform):
    def _analytic_sample(self, n, limits: Space):
        raise AnalyticSamplingNotImplemented  # HACK do make importance sampling work


def create_gauss1():
    mu = zfit.Parameter("mu_sampling1", mu_true, mu_true - 2.0, mu_true + 7.0)
    sigma = zfit.Parameter(
コード例 #10
0
ファイル: test_sampling.py プロジェクト: sleepy-owl/zfit
    setup_func_general()


def teardown_function():
    Limit._experimental_allow_vectors = False
    teardown_func_general()


ztf = z
from zfit.core.sample import accept_reject_sample

mu_true = 1.5
sigma_true = 1.2
low, high = -3.8, 2.9

obs1 = zfit.Space('obs1', (low, high))


class GaussNoAnalyticSampling(zfit.pdf.Gauss):
    def _analytic_sample(self, n, limits: Space):
        raise AnalyticSamplingNotImplementedError  # HACK do make importance sampling work


class UniformNoAnalyticSampling(zfit.pdf.Uniform):
    def _analytic_sample(self, n, limits: Space):
        raise AnalyticSamplingNotImplementedError  # HACK do make importance sampling work


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.,
コード例 #11
0
ファイル: test_pdf_sumpdf.py プロジェクト: zfit/zfit
def test_analytic_integral():
    obs = zfit.Space("obs1", (-2, 5))
コード例 #12
0
ファイル: test_pdf_fftconv.py プロジェクト: sleepy-owl/zfit
def test_conv_2D_simple():
    zfit.run.set_graph_mode(False)  # TODO: remove, just for debugging
    raise WorkInProgressError("2D convolution not yet implemented, re-activate if so")
    n_points = 1000
    obs1 = zfit.Space("obs1", limits=(-2, 4))
    obs2 = zfit.Space("obs2", limits=(-6, 4))
    obskernel = obs1 * obs2

    param2 = zfit.Parameter('param2', 0.4)
    gauss1 = zfit.pdf.Gauss(1., 0.5, obs=obs1)
    gauss22 = zfit.pdf.CrystalBall(0.0, param2, -0.2, 3, obs=obs2)

    obs1func = zfit.Space("obs1", limits=(4, 10))
    obs2func = zfit.Space("obs2", limits=(-6, 4))
    obs_func = obs1func * obs2func

    gauss21 = zfit.pdf.Gauss(-0.5, param2, obs=obs2func)
    func1 = zfit.pdf.Uniform(5, 8, obs=obs1func)
    func2 = zfit.pdf.Uniform(6, 7, obs=obs1func)
    func = zfit.pdf.SumPDF([func1, func2], 0.5)
    func = func * gauss21
    gauss = gauss1 * gauss22
    conv = zfit.pdf.FFTConvPDFV1(func=func, kernel=gauss)

    start = obs_func.rect_lower
    stop = obs_func.rect_upper
    x_tensor = tf.random.uniform((n_points, 2), start, stop)
    x_tensor = tf.reshape(x_tensor, (-1, 2))
    linspace = tf.linspace(start, stop, num=n_points)
    linspace = tf.transpose(tf.meshgrid(*tf.unstack(linspace, axis=-1)))
    linspace_func = tf.reshape(linspace, (-1, 2))

    # linspace_full = tf.linspace((-8, -8), (12, 12), num=n_points)
    # linspace_full = tf.transpose(tf.meshgrid(*tf.unstack(linspace_full, axis=-1)))
    # linspace_full = tf.reshape(linspace_full, (-1, 2))

    linspace_kernel = tf.linspace(obskernel.rect_lower,
                                  obskernel.rect_upper, num=n_points)
    linspace_kernel = tf.transpose(tf.meshgrid(*tf.unstack(linspace_kernel, axis=-1)))
    linspace_kernel = tf.reshape(linspace_kernel, (-1, 2))
    # linspace_kernel = obskernel.filter(linspace_full)
    # linspace_func = obs_func.filter(linspace_full)

    x = zfit.Data.from_tensor(obs=obs_func, tensor=x_tensor)
    linspace_data = zfit.Data.from_tensor(obs=obs_func, tensor=linspace)
    probs_rnd = conv.pdf(x=x)
    probs = conv.pdf(x=linspace_data)

    # Numpy doesn't support ndim convolution?
    true_probs = true_conv_2d_np(func, gauss, obsfunc=obs_func,
                                 xfunc=linspace_func, xkernel=linspace_kernel)
    import matplotlib.pyplot as plt
    # np.testing.assert_allclose(probs, true_probs, rtol=0.2, atol=0.1)
    integral = conv.integrate(limits=obs_func)
    assert pytest.approx(1, rel=1e-3) == integral.numpy()
    probs_np = probs_rnd.numpy()
    assert len(probs_np) == n_points
    # probs_plot = np.reshape(probs_np, (-1, n_points))
    # x_plot = linspace[0:, ]
    # probs_plot_projx = np.sum(probs_plot, axis=0)
    # plt.plot(x_plot, probs_np)
    # probs_plot = np.reshape(probs_np, (n_points, n_points))
    # plt.imshow(probs_plot)
    # plt.show()

    true_probsr = tf.reshape(true_probs, (n_points, n_points))
    probsr = tf.reshape(probs, (n_points, n_points))
    plt.figure()
    plt.imshow(true_probsr, label='true probs')
    plt.title('true probs')

    plt.figure()
    plt.imshow(probsr, label='zfit conv')
    plt.title('zfit conv')

    plt.show(block=False)

    # test the sampling
    conv_nosample = FFTConvPDFV1NoSampling(func=func, kernel=gauss)

    npoints_sample = 10000
    sample = conv.sample(npoints_sample)
    sample_nosample = conv_nosample.sample(npoints_sample)
    x, y = z.unstack_x(sample)
    xns, yns = z.unstack_x(sample_nosample)

    plt.figure()
    plt.hist2d(x, y, bins=30)
    plt.title('custom sampling, addition')

    plt.figure()
    plt.hist2d(xns, yns, bins=30)
    plt.title('fallback sampling, accept-reject')

    plt.figure()
    plt.hist(x.numpy(), bins=50, label='custom', alpha=0.5)
    plt.hist(xns.numpy(), bins=50, label='fallback', alpha=0.5)
    plt.legend()
    plt.title('x')
    plt.show()

    plt.figure()
    plt.hist(y.numpy(), bins=50, label='custom', alpha=0.5)
    plt.hist(yns.numpy(), bins=50, label='fallback', alpha=0.5)
    plt.title('y')
    plt.legend()
    plt.show()
コード例 #13
0
ファイル: test_limit.py プロジェクト: tgag17/zfit
from zfit.core.space import ANY, Limit

rect_limits = (1., 3)
rect_limits_tf = (tf.constant(1.), tf.constant(3))
rect_limit_enlarged = rect_limits[0] - 1, rect_limits[1] + 1.5
rect_limits_any = (ANY, ANY)


def inside_rect_limits(x):
    return tf.logical_and(rect_limits[0] < x, x < rect_limits[1])[:, 0]


@pytest.mark.parametrize('graph', [True, False])
@pytest.mark.parametrize('rect_limits', [rect_limits, rect_limits_tf])
@pytest.mark.parametrize('testclass', [
    Limit, lambda limit_fn=None, rect_limits=None: zfit.Space(
        'obs1', limits=limit_fn, rect_limits=rect_limits)
])
def test_rect_limits_1d(graph, rect_limits, testclass):
    def test(allow_graph=True):
        limit = testclass(rect_limits)
        limit2 = testclass(rect_limits=rect_limits)

        lower, upper = limit.rect_limits
        lower2, upper2 = limit2.rect_limits
        assert limit.has_rect_limits
        inside = limit.inside(2)
        inside2 = limit.inside(2, guarantee_limits=True)
        outside = limit.inside(4)
        if graph:
            equal = limit.equal(limit2, allow_graph=allow_graph)
            less_equal = limit.less_equal(limit2, allow_graph=allow_graph)
コード例 #14
0
ファイル: core.py プロジェクト: mzks/zfitter
 def set_bins(self, bins):
     self.bins = bins
     self._calc_fit_bins()
     self.obs = zfit.Space('x', limits=(self.fit_lower, self.fit_upper))
コード例 #15
0
import zfit
from math import pi

costk = zfit.Space('costk', (-1, 1))
costl = zfit.Space('costl', (-1, 1))
phi = zfit.Space('phi', (-pi, pi))


def getobs():
    obs = costk * costl * phi
    return obs


def getobslist():
    obslist = [costk, costl, phi]
    return obslist
コード例 #16
0
from zfit.core.space import Space
from zfit.models.dist_tfp import Gauss
from zfit.models.functor import ProductPDF, SumPDF

low, high = -0.64, 5.9

mu1_true = 1.
mu2_true = 2.
mu3_true = 0.6
sigma1_true = 1.4
sigma2_true = 2.3
sigma3_true = 1.8

fracs = [0.3, 0.15]

obs1 = zfit.Space('obs1', (-3, 6))
obs2 = zfit.Space('obs2', (-2, 5))
obs3 = zfit.Space('obs3', (-3.5, 4))


def true_gaussian_sum(x):
    def norm(sigma):
        return np.sqrt(2 * np.pi) * sigma

    sum_gauss = fracs[0] * np.exp(-(x - mu1_true)**2 /
                                  (2 * sigma1_true**2)) / norm(sigma1_true)
    sum_gauss += fracs[1] * np.exp(-(x - mu2_true)**2 /
                                   (2 * sigma2_true**2)) / norm(sigma2_true)
    sum_gauss += (1. - sum(fracs)) * np.exp(
        -(x - mu3_true)**2 / (2 * sigma3_true**2)) / norm(sigma3_true)
    return sum_gauss
コード例 #17
0
    def get_folded_pdf(self, name):
        pdf_class, param_names, _ = self.FOLDS[name]

        def get_params(param_list):
            out = {}
            for param in param_list:
                if param not in self.params:
                    config = [0.8, 0, 1] if param == "FL" else [0.0, -1, 1]
                    self.params.update({param: zfit.Parameter(param, *config)})
                out[param] = self.params[param]
            return out

        # Make sure params exist
        params = get_params(param_names)
        pdf = pdf_class(obs=self.obs, **params)
        return pdf

    def fold_dataset(self, name, dataset):
        *_, data_transform = self.FOLDS[name]
        return data_transform(dataset, self.obs.obs)


if __name__ == "__main__":
    costheta_l = zfit.Space("costhetal", limits=(-1.0, 1.0))
    costheta_k = zfit.Space("costhetaK", limits=(-1.0, 1.0))
    phi = zfit.Space("phi", limits=(-pi, pi))
    decay = B2Kstll(costheta_l, costheta_k, phi)
    pdf = decay.get_folded_pdf("P5p")

# EOFs
コード例 #18
0
ファイル: test_extended.py プロジェクト: mozgit/zfit
#  Copyright (c) 2020 zfit

import numpy as np
import pytest

import zfit
from zfit.core.sample import extract_extended_pdfs, extended_sampling
# noinspection PyUnresolvedReferences
from zfit.core.testing import setup_function, teardown_function, tester

obs1 = zfit.Space('obs1', limits=(-3, 4))


@pytest.mark.flaky(reruns=3)  # poissonian sampling
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)
コード例 #19
0
class CustomPDF(zfit.pdf.ZPDF):
    """3-dimensional PDF calculating doing something fancy. Takes x, y, z as data."""
    _PARAMS = ['alpha', 'beta']  # specify which parameters to take
    _N_OBS = 3

    def _unnormalized_pdf(self, x):  # implement function
        x, y, z = x.unstack_x()
        alpha = self.params['alpha']
        beta = self.params['beta']
        x_new = tf.math.cos(alpha) * x
        y_new = tf.math.sinh(beta) * y
        z_new = z + 4.2
        return x_new**2 + y_new**2 + z_new**2


xobs = zfit.Space('xobs', (-4, 4))
yobs = zfit.Space('yobs', (-3, 3))
zobs = zfit.Space('z', (-2, 2))
obs = xobs * yobs * zobs

alpha = zfit.Parameter("alpha", 0.2)  # floating
beta = zfit.Parameter("beta", 0.4, floating=False)  # non-floating
custom_pdf = CustomPDF(obs=obs, alpha=alpha, beta=beta)

integral = custom_pdf.integrate(limits=obs)  # = 1 since normalized
sample = custom_pdf.sample(n=1000)  # DO NOT USE THIS FOR TOYS!
prob = custom_pdf.pdf(sample)  # DO NOT USE THIS FOR TOYS!

integral_np, sample_np, prob_np = zfit.run([integral, sample, prob])
コード例 #20
0
ファイル: test_pdf_normal.py プロジェクト: chm-ipmu/zfit
from zfit import Parameter
from zfit.core.testing import setup_function, teardown_function, tester
from zfit.models.dist_tfp import Gauss

mu1_true = 1.
mu2_true = 2.
mu3_true = 0.6
sigma1_true = 1.4
sigma2_true = 2.3
sigma3_true = 1.8

test_values = np.random.uniform(low=-3, high=5, size=100)
norm_range1 = (-4., 2.)

obs1 = 'obs1'
limits1 = zfit.Space(obs=obs1, limits=(-0.3, 1.5))


def create_gauss():
    mu1 = Parameter("mu1a", mu1_true)
    mu2 = Parameter("mu2a", mu2_true)
    mu3 = Parameter("mu3a", mu3_true)
    sigma1 = Parameter("sigma1a", sigma1_true)
    sigma2 = Parameter("sigma2a", sigma2_true)
    sigma3 = Parameter("sigma3a", sigma3_true)
    gauss1 = Gauss(mu=mu1, sigma=sigma1, obs=obs1, name="gauss1a")
    normal1 = Gauss(mu=mu1, sigma=sigma1, obs=obs1, name="normal1a")
    gauss2 = Gauss(mu=mu2, sigma=sigma2, obs=obs1, name="gauss2a")
    normal2 = Gauss(mu=mu2, sigma=sigma2, obs=obs1, name="normal2a")
    gauss3 = Gauss(mu=mu3, sigma=sigma3, obs=obs1, name="gauss3a")
    normal3 = Gauss(mu=mu3, sigma=sigma3, obs=obs1, name="normal3a")
コード例 #21
0
ファイル: test_pdf_template.py プロジェクト: zfit/zfit
def test_binned_template_pdf_bbfull(TemplateLikePDF):
    bins1 = 15
    bins2 = 10

    counts1 = np.random.uniform(high=150,
                                size=(bins1, bins2))  # generate counts
    counts2 = np.random.normal(loc=50, size=(bins1, bins2))
    counts3 = (np.linspace(10, 100, num=bins1)[:, None] *
               np.linspace(10, 500, num=bins2)[None, :])
    binnings = [
        zfit.binned.RegularBinning(bins1, 0, 10, name="obs1"),
        zfit.binned.RegularBinning(bins2, -10, 7, name="obs2"),
    ]
    binning = binnings
    obs = zfit.Space(obs=["obs1", "obs2"], binning=binning)

    mc1 = BinnedData.from_tensor(space=obs,
                                 values=counts1,
                                 variances=znp.ones_like(counts1) * 1.3)
    mc2 = BinnedData.from_tensor(obs, counts2)
    mc3 = BinnedData.from_tensor(obs, counts3)

    counts_mc = counts1 + counts2 + counts3

    counts1_data = np.random.uniform(high=150,
                                     size=(bins1, bins2))  # generate counts
    counts2_data = np.random.normal(loc=50, size=(bins1, bins2))
    counts3_data = (np.linspace(10, 100, num=bins1)[:, None] *
                    np.linspace(20, 490, num=bins2)[None, :])
    counts_data = counts1_data + counts2_data + counts3_data
    counts_data *= 1.1
    data = BinnedData.from_tensor(space=obs, values=counts_data)

    pdf1 = TemplateLikePDF(data=mc1, sysshape=True)
    pdf2 = TemplateLikePDF(data=mc2, sysshape=True)
    pdf3 = TemplateLikePDF(data=mc3, sysshape=True)
    assert len(pdf1.counts()) > 0
    pdf_sum = BinnedSumPDF(pdfs=[pdf1, pdf2, pdf3], obs=obs)
    counts1_flat = np.reshape(counts1, -1)
    constraints1 = zfit.constraint.GaussianConstraint(
        pdf1.params.values(),
        observation=np.ones_like(counts1_flat),
        uncertainty=np.sqrt(counts1_flat) / counts1_flat,
    )
    counts2_flat = np.reshape(counts2, -1)
    constraints2 = zfit.constraint.GaussianConstraint(
        pdf2.params.values(),
        observation=np.ones_like(counts2_flat),
        uncertainty=np.sqrt(counts2_flat) / counts2_flat,
    )
    counts3_flat = np.reshape(counts3, -1)
    constraints3 = zfit.constraint.GaussianConstraint(
        pdf3.params.values(),
        observation=np.ones_like(counts3_flat),
        uncertainty=np.sqrt(counts3_flat) / counts3_flat,
    )
    # constraints2 = zfit.constraint.PoissonConstraint(pdf2.params.values(), np.reshape(counts2, -1))
    # constraints3 = zfit.constraint.PoissonConstraint(pdf3.params.values(), np.reshape(counts3, -1))
    loss = ExtendedBinnedNLL(
        pdf_sum, data, constraints=[constraints1, constraints2, constraints3])
    # for i in progressbar.progressbar(range(1000000)):
    loss.value()
    loss.gradients()
    print("start minimization")
    minimizer = zfit.minimize.Minuit(verbosity=8, gradient=False)
    minimizer.minimize(loss)

    counts = pdf_sum.counts()
    np.testing.assert_array_less(
        counts_mc, counts_data
    )  # this is an assumption, if that is wrong, the test is flawed
    np.testing.assert_array_less(counts, counts_data)
    np.testing.assert_array_less(counts_mc, counts)
コード例 #22
0
# DoubleCB.register_analytic_integral(func=double_crystalball_integral, limits=crystalball_integral_limits)

if __name__ == '__main__':
    mu = ztf.constant(0)
    sigma = ztf.constant(0.5)
    alpha = ztf.constant(3)
    n = ztf.constant(1)
    # res = crystalball_func(np.random.random(size=100), mu, sigma, alpha, n)
    # int1 = crystalball_integral(limits=zfit.Space(obs='obs1', limits=(-3, 5)),
    #                             params={'mu': mu, "sigma": sigma, "alpha": alpha, "n": n})
    from tensorflow.contrib import autograph
    import matplotlib.pyplot as plt

    new_code = autograph.to_code(crystalball_integral)
    obs = zfit.Space(obs='obs1', limits=(-3, 1))
    cb1 = CrystalBall(mu, sigma, alpha, n, obs=obs)
    res = cb1.pdf(np.random.random(size=100))
    int1 = cb1.integrate(limits=(-0.01, 2), norm_range=obs)
    # tf.add_check_numerics_ops()

    x = np.linspace(-5, 1, num=1000)
    vals = cb1.pdf(x=x)
    y = zfit.run(vals)[0]
    plt.plot(x, y)
    plt.show()

    # print(new_code)
    print(zfit.run(res))
    print(zfit.run(int1))
コード例 #23
0
#  Copyright (c) 2021 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.5, 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 with a hessian approximation
param_errors = result.hesse()

# or here with minos
コード例 #24
0
import zfit

simple_limit1 = zfit.Space(obs='obs1', limits=(-5, 1))
simple_limit2 = zfit.Space(obs='obs1', limits=(3, 7.5))

added_limits = simple_limit1 + simple_limit2
# OR equivalently
added_limits = simple_limit1.add(simple_limit2)

first_limit_lower = (-5, 6)
first_limit_upper = (5, 10)

second_limit_lower = (7, 12)
second_limit_upper = (9, 15)

third_limit_lower = (13, 20)
third_limit_upper = (14, 25)

lower = (first_limit_lower, second_limit_lower, third_limit_lower)
upper = (first_limit_upper, second_limit_upper, third_limit_upper)

limits = (lower, upper)

space1 = zfit.Space(obs=['obs1', 'obs2'], limits=limits)

assert space1.obs == ('obs1', 'obs2')
assert space1.n_obs == 2
assert space1.n_limits == 3
コード例 #25
0
ファイル: test_dimensions.py プロジェクト: chm-ipmu/zfit
# noinspection PyUnresolvedReferences
from zfit.core.testing import setup_function, teardown_function, tester
from zfit.util.exception import (
    LimitsIncompatibleError,
    LimitsNotSpecifiedError,
    SpaceIncompatibleError,
)

os.environ["CUDA_VISIBLE_DEVICES"] = "-1"

import zfit
from zfit.core.dimension import add_spaces, limits_consistent, limits_overlap, combine_spaces

obs = ['obs' + str(i) for i in range(4)]
space1 = zfit.Space(obs=obs)
space2 = zfit.Space(obs=reversed(obs))
space3 = zfit.Space(obs=obs)
space4 = zfit.Space(obs=obs[0:2])

lower1d_1 = ((1, ), )
upper1d_1 = ((2, ), )

lower1d_12 = ((3, ), )
upper1d_12 = ((4, ), )

lower1d_22 = ((5, ), )
upper1d_22 = ((6, ), )

combined_lim_1d_12_and_22 = (((3, ), (5, )), ((4, ), (6, )))
コード例 #26
0
ファイル: test_dependencies.py プロジェクト: zfit/zfit
def test_get_params():
    obs = zfit.Space("obs", (-4, 5))
    mu_nofloat = zfit.Parameter("mu_nofloat", 1, floating=False)
    mu2 = zfit.Parameter("mu2", 1)
    sigma2 = zfit.Parameter("sigma2", 2)
    sigma_comp = zfit.ComposedParameter("sigma_comp",
                                        lambda s: s * 0.7,
                                        params=sigma2)

    yield1 = zfit.Parameter("yield1", 10)
    yield2 = zfit.Parameter("yield2", 200)
    yield2_comp = zfit.ComposedParameter("yield2_comp",
                                         lambda y: y * 0.9,
                                         params=yield2)

    gauss = zfit.pdf.Gauss(mu_nofloat, sigma_comp, obs)
    gauss2 = zfit.pdf.Gauss(mu2, sigma2, obs)
    gauss_ext = gauss.create_extended(yield1)
    gauss2_ext = gauss2.create_extended(yield2_comp)

    frac = zfit.Parameter("frac", 0.4)
    sum1 = zfit.pdf.SumPDF([gauss, gauss2], fracs=frac)
    sum1_ext = zfit.pdf.SumPDF([gauss_ext, gauss2_ext])

    assert set(gauss.get_params()) == {sigma2}

    with pytest.raises(ValueError):
        set(
            gauss.get_params(floating=True,
                             is_yield=False,
                             extract_independent=False))

    with pytest.raises(ValueError):
        set(
            gauss_ext.get_params(floating=True,
                                 is_yield=False,
                                 extract_independent=False))
    assert set(
        gauss_ext.get_params(floating=None,
                             is_yield=False,
                             extract_independent=False)) == {
                                 mu_nofloat, sigma_comp
                             }
    with pytest.raises(ValueError):
        set(
            gauss_ext.get_params(floating=False,
                                 is_yield=False,
                                 extract_independent=False))

    with pytest.raises(ValueError):
        set(
            gauss_ext.get_params(floating=True,
                                 is_yield=None,
                                 extract_independent=False))

    assert set(
        gauss_ext.get_params(floating=None,
                             is_yield=None,
                             extract_independent=False)) == {
                                 mu_nofloat, sigma_comp, yield1
                             }
    with pytest.raises(ValueError):
        set(
            gauss_ext.get_params(floating=False,
                                 is_yield=None,
                                 extract_independent=False))

    assert (set(
        gauss_ext.get_params(floating=False,
                             is_yield=True,
                             extract_independent=False)) == set())
    assert set(
        gauss_ext.get_params(floating=None,
                             is_yield=True,
                             extract_independent=False)) == {yield1}
    assert set(
        gauss_ext.get_params(floating=True,
                             is_yield=True,
                             extract_independent=False)) == {yield1}

    # with extract deps
    assert set(
        gauss_ext.get_params(floating=True,
                             is_yield=False,
                             extract_independent=True)) == {sigma2}
    assert set(
        gauss_ext.get_params(floating=None,
                             is_yield=False,
                             extract_independent=True)) == {
                                 mu_nofloat, sigma2
                             }
    assert set(
        gauss_ext.get_params(floating=False,
                             is_yield=False,
                             extract_independent=True)) == {mu_nofloat}

    assert set(
        gauss_ext.get_params(floating=True,
                             is_yield=None,
                             extract_independent=True)) == {sigma2, yield1}
    assert set(
        gauss_ext.get_params(floating=None,
                             is_yield=None,
                             extract_independent=True)) == {
                                 mu_nofloat, sigma2, yield1
                             }
    assert set(
        gauss_ext.get_params(floating=False,
                             is_yield=None,
                             extract_independent=True)) == {mu_nofloat}

    assert set(
        gauss_ext.get_params(floating=True,
                             is_yield=True,
                             extract_independent=True)) == {yield1}
    assert set(
        gauss_ext.get_params(floating=None,
                             is_yield=True,
                             extract_independent=True)) == {yield1}
    assert (set(
        gauss_ext.get_params(floating=False,
                             is_yield=True,
                             extract_independent=True)) == set())

    # Gauss ext2
    assert set(
        gauss2_ext.get_params(floating=True,
                              is_yield=False,
                              extract_independent=False)) == {mu2, sigma2}
    assert set(
        gauss2_ext.get_params(floating=None,
                              is_yield=False,
                              extract_independent=False)) == {mu2, sigma2}
    assert (set(
        gauss2_ext.get_params(floating=False,
                              is_yield=False,
                              extract_independent=False)) == set())

    with pytest.raises(ValueError):
        set(
            gauss2_ext.get_params(floating=True,
                                  is_yield=None,
                                  extract_independent=False))
    assert set(
        gauss2_ext.get_params(floating=None,
                              is_yield=None,
                              extract_independent=False)) == {
                                  mu2, sigma2, yield2_comp
                              }
    with pytest.raises(ValueError):
        set(
            gauss2_ext.get_params(floating=False,
                                  is_yield=None,
                                  extract_independent=False))

    with pytest.raises(ValueError):
        set(
            gauss2_ext.get_params(floating=False,
                                  is_yield=True,
                                  extract_independent=False))
    assert set(
        gauss2_ext.get_params(floating=None,
                              is_yield=True,
                              extract_independent=False)) == {yield2_comp}
    with pytest.raises(ValueError):
        set(
            gauss2_ext.get_params(floating=True,
                                  is_yield=True,
                                  extract_independent=False))

    # with extract deps
    assert set(
        gauss2_ext.get_params(floating=True,
                              is_yield=False,
                              extract_independent=True)) == {mu2, sigma2}
    assert set(
        gauss2_ext.get_params(floating=None,
                              is_yield=False,
                              extract_independent=True)) == {mu2, sigma2}
    yield2.floating = False
    assert (set(
        gauss2_ext.get_params(floating=False,
                              is_yield=False,
                              extract_independent=True)) == set())
    yield2.floating = True
    assert (set(
        gauss2_ext.get_params(floating=False,
                              is_yield=False,
                              extract_independent=True)) == set())

    assert set(
        gauss2_ext.get_params(floating=True,
                              is_yield=None,
                              extract_independent=True)) == {
                                  mu2, sigma2, yield2
                              }
    assert set(
        gauss2_ext.get_params(floating=None,
                              is_yield=None,
                              extract_independent=True)) == {
                                  mu2, sigma2, yield2
                              }
    assert (set(
        gauss2_ext.get_params(floating=False,
                              is_yield=None,
                              extract_independent=True)) == set())

    assert set(
        gauss2_ext.get_params(floating=True,
                              is_yield=True,
                              extract_independent=True)) == {yield2}
    assert set(
        gauss2_ext.get_params(floating=None,
                              is_yield=True,
                              extract_independent=True)) == {yield2}
    assert (set(
        gauss2_ext.get_params(floating=False,
                              is_yield=True,
                              extract_independent=True)) == set())

    # sum extended
    with pytest.raises(ValueError):
        assert set(
            sum1_ext.get_params(floating=True,
                                is_yield=False,
                                extract_independent=False))
    frac0 = sum1_ext.fracs[0]
    frac1 = sum1_ext.fracs[1]
    assert set(
        sum1_ext.get_params(floating=None,
                            is_yield=False,
                            extract_independent=False)) == {
                                mu_nofloat, sigma_comp, mu2, sigma2, frac0,
                                frac1
                            }
    with pytest.raises(ValueError):
        assert set(
            sum1_ext.get_params(floating=False,
                                is_yield=False,
                                extract_independent=False))

    with pytest.raises(ValueError):
        set(
            sum1_ext.get_params(floating=True,
                                is_yield=None,
                                extract_independent=False))
    assert set(
        sum1_ext.get_params(floating=None,
                            is_yield=None,
                            extract_independent=False)) == {
                                mu_nofloat, sigma_comp, mu2, sigma2, frac0,
                                frac1,
                                sum1_ext.get_yield()
                            }
    with pytest.raises(ValueError):
        set(
            sum1_ext.get_params(floating=False,
                                is_yield=None,
                                extract_independent=False))

    with pytest.raises(ValueError):
        set(
            sum1_ext.get_params(floating=False,
                                is_yield=True,
                                extract_independent=False))
    assert set(
        sum1_ext.get_params(floating=None,
                            is_yield=True,
                            extract_independent=False)) == {
                                sum1_ext.get_yield()
                            }
    with pytest.raises(ValueError):
        set(
            sum1_ext.get_params(floating=True,
                                is_yield=True,
                                extract_independent=False))

    # with extract deps
    assert set(
        sum1_ext.get_params(floating=True,
                            is_yield=False,
                            extract_independent=True)) == {
                                mu2,
                                sigma2,
                                yield1,
                                yield2,  # fracs depend on them
                            }
    assert set(
        sum1_ext.get_params(floating=None,
                            is_yield=False,
                            extract_independent=True)) == {
                                mu_nofloat, mu2, sigma2, yield1, yield2
                            }
    assert set(
        sum1_ext.get_params(floating=False,
                            is_yield=False,
                            extract_independent=True)) == {mu_nofloat}

    assert set(
        sum1_ext.get_params(floating=True,
                            is_yield=None,
                            extract_independent=True)) == {
                                mu2, sigma2, yield1, yield2, yield1, yield2
                            }
    assert set(
        sum1_ext.get_params(floating=None,
                            is_yield=None,
                            extract_independent=True)) == {
                                mu_nofloat, mu2, sigma2, yield1, yield2
                            }
    assert set(
        sum1_ext.get_params(floating=False,
                            is_yield=None,
                            extract_independent=True)) == {mu_nofloat}
    yield1.floating = False
    assert set(
        sum1_ext.get_params(floating=False,
                            is_yield=None,
                            extract_independent=True)) == {mu_nofloat, yield1}
    yield1.floating = True

    assert set(
        sum1_ext.get_params(floating=True,
                            is_yield=True,
                            extract_independent=True)) == {yield1, yield2}
    assert set(
        sum1_ext.get_params(floating=None,
                            is_yield=True,
                            extract_independent=True)) == {yield1, yield2}
    assert (set(
        sum1_ext.get_params(floating=False,
                            is_yield=True,
                            extract_independent=True)) == set())
コード例 #27
0
ファイル: simple_fit.py プロジェクト: olantwin/zfit
import numpy as np
import zfit

# 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)

# 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.Data.from_numpy(obs=obs, array=normal_np)

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

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

# do the error calculations, here with minos
param_errors = result.error()
コード例 #28
0
#  Copyright (c) 2022 zfit

import matplotlib.pyplot as plt
import mplhep
import numpy as np

import zfit
import zfit.models.tobinned

n_bins = 50

# create space
obs_binned = zfit.Space("x",
                        binning=zfit.binned.RegularBinning(50,
                                                           -10,
                                                           10,
                                                           name="x"))
obs = obs_binned.with_binning(None)

# parameters
mu = zfit.Parameter("mu", 1.0, -4, 6)
sigma = zfit.Parameter("sigma", 1.0, 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)
コード例 #29
0
def test_cb_dcb():
    obs = zfit.Space("x", limits=bounds)

    mu_ = zfit.Parameter("mu_cb5", mu)
    sigma_ = zfit.Parameter("sigma_cb5", sigma)
    alphal_ = zfit.Parameter("alphal_cb5", alphal)
    nl_ = zfit.Parameter("nl_cb5", nl)
    alphar_ = zfit.Parameter("alphar_cb5", alphar)
    nr_ = zfit.Parameter("nr_cb5", nr)

    cbl = CrystalBall(obs=obs, mu=mu_, sigma=sigma_, alpha=alphal_, n=nl_)
    cbr = CrystalBall(obs=obs, mu=mu_, sigma=sigma_, alpha=-alphar_, n=nr_)
    dcb = DoubleCB(obs=obs,
                   mu=mu_,
                   sigma=sigma_,
                   alphal=alphal_,
                   nl=nl_,
                   alphar=alphar_,
                   nr=nr_)

    sample_testing(cbl)
    sample_testing(cbr)
    sample_testing(dcb)

    x = np.random.normal(mu, sigma, size=10000)

    probsl = eval_testing(cbl, x)
    probsr = eval_testing(cbr, x)

    assert not any(np.isnan(probsl))
    assert not any(np.isnan(probsr))

    probsl_scipy = crystalball.pdf(x, beta=alphal, m=nl, loc=mu, scale=sigma)
    probsr_scipy = crystalball.pdf(-x + 2 * mu,
                                   beta=alphar,
                                   m=nr,
                                   loc=mu,
                                   scale=sigma)

    # We take the ration as the normalization is not fixed
    ratio_l = probsl_scipy / probsl
    ratio_r = probsr_scipy / probsr

    assert np.allclose(ratio_l, ratio_l[0])
    assert np.allclose(ratio_r, ratio_r[0])

    kwargs = dict(limits=(-5.0, mu), norm_range=lbounds)
    intl = cbl.integrate(**kwargs) - dcb.integrate(**kwargs)
    assert pytest.approx(intl.numpy(), abs=1e-3) == 0.0
    intl = cbr.integrate(**kwargs) - dcb.integrate(**kwargs)
    assert pytest.approx(intl.numpy(), abs=1e-3) != 0

    # TODO: update test to fixed DCB integral
    kwargs = dict(limits=(mu, 2.0), norm_range=rbounds)
    dcb_integr1 = dcb.integrate(**kwargs)
    intr = cbr.integrate(**kwargs) - dcb_integr1
    assert pytest.approx(intr.numpy(), abs=1e-3) == 0.0
    intr = cbl.integrate(**kwargs) - dcb.integrate(**kwargs)
    assert pytest.approx(intr.numpy(), abs=1e-3) != 0.0

    xl = x[x <= mu]
    xr = x[x > mu]

    probs_dcb_l = eval_testing(dcb, xl)
    probs_dcb_r = eval_testing(dcb, xr)

    probsl_scipy = crystalball.pdf(xl, beta=alphal, m=nl, loc=mu, scale=sigma)
    probsr_scipy = crystalball.pdf(-xr + 2 * mu,
                                   beta=alphar,
                                   m=nr,
                                   loc=mu,
                                   scale=sigma)

    ratio_l = probsl_scipy / probs_dcb_l
    ratio_r = probsr_scipy / probs_dcb_r

    assert np.allclose(ratio_l, ratio_l[0])
    assert np.allclose(ratio_r, ratio_r[0])

    rnd_limits = sorted(np.random.uniform(*bounds, 130))
    integrals = []
    for low, up in zip(rnd_limits[:-1], rnd_limits[1:]):
        integrals.append(dcb.integrate((low, up), norm=False))

        integral = np.sum(integrals)
        integral_full = zfit.run(dcb.integrate((bounds[0], up), norm=False))
        assert pytest.approx(integral_full, integral)
コード例 #30
0
def test_cb_dcb():
    obs = zfit.Space('x', limits=bounds)

    mu_ = zfit.Parameter('mu_cb', mu)
    sigma_ = zfit.Parameter('sigma_cb', sigma)
    alphal_ = zfit.Parameter('alphal_cb', alphal)
    nl_ = zfit.Parameter('nl_cb', nl)
    alphar_ = zfit.Parameter('alphar_cb', alphar)
    nr_ = zfit.Parameter('nr_cb', nr)

    cbl = CrystalBall(obs=obs, mu=mu_, sigma=sigma_, alpha=alphal_, n=nl_)
    cbr = CrystalBall(obs=obs, mu=mu_, sigma=sigma_, alpha=-alphar_, n=nr_)
    dcb = DoubleCB(obs=obs,
                   mu=mu_,
                   sigma=sigma_,
                   alphal=alphal_,
                   nl=nl_,
                   alphar=alphar_,
                   nr=nr_)

    sample_testing(cbl)
    sample_testing(cbr)
    sample_testing(dcb)

    x = np.random.normal(mu, sigma, size=10000)

    probsl = eval_testing(cbl, x)
    probsr = eval_testing(cbr, x)

    assert not any(np.isnan(probsl))
    assert not any(np.isnan(probsr))

    probsl_scipy = crystalball.pdf(x, beta=alphal, m=nl, loc=mu, scale=sigma)
    probsr_scipy = crystalball.pdf(-x, beta=alphar, m=nr, loc=mu, scale=sigma)

    ratio_l = probsl_scipy / probsl
    ratio_r = probsr_scipy / probsr

    assert np.allclose(ratio_l, ratio_l[0])
    assert np.allclose(ratio_r, ratio_r[0])

    kwargs = dict(limits=(-5.0, mu), norm_range=lbounds)
    intl = cbl.integrate(**kwargs) - dcb.integrate(**kwargs)
    assert pytest.approx(zfit.run(intl)) == 0.
    intl = cbr.integrate(**kwargs) - dcb.integrate(**kwargs)
    assert pytest.approx(zfit.run(intl)) != 0

    kwargs = dict(limits=(mu, 2.0), norm_range=rbounds)
    intr = cbr.integrate(**kwargs) - dcb.integrate(**kwargs)
    assert pytest.approx(zfit.run(intr)) == 0.
    intr = cbl.integrate(**kwargs) - dcb.integrate(**kwargs)
    assert pytest.approx(zfit.run(intr)) != 0.

    xl = x[x <= mu]
    xr = x[x > mu]

    probs_dcb_l = eval_testing(dcb, xl)
    probs_dcb_r = eval_testing(dcb, xr)

    probsl_scipy = crystalball.pdf(xl, beta=alphal, m=nl, loc=mu, scale=sigma)
    probsr_scipy = crystalball.pdf(-xr, beta=alphar, m=nr, loc=mu, scale=sigma)

    ratio_l = probsl_scipy / probs_dcb_l
    ratio_r = probsr_scipy / probs_dcb_r

    assert np.allclose(ratio_l, ratio_l[0])
    assert np.allclose(ratio_r, ratio_r[0])