def create_loss(): bounds = (0.1, 3.0) obs = zfit.Space("x", limits=bounds) # Data and signal np.random.seed(0) tau = -2.0 beta = -1 / tau bkg = np.random.exponential(beta, 300) peak = np.random.normal(1.2, 0.1, 80) data = np.concatenate((bkg, peak)) data = data[(data > bounds[0]) & (data < bounds[1])] N = len(data) data = zfit.data.Data.from_numpy(obs=obs, array=data) mean = zfit.Parameter("mean", 1.2, 0.5, 2.0) sigma = zfit.Parameter("sigma", 0.1, 0.02, 0.2) lambda_ = zfit.Parameter("lambda", -2.0, -4.0, -1.0) Nsig = zfit.Parameter("Ns", 20.0, -20.0, N) Nbkg = zfit.Parameter("Nbkg", N, 0.0, N * 1.1) signal = zfit.pdf.Gauss(obs=obs, mu=mean, sigma=sigma).create_extended(Nsig) background = zfit.pdf.Exponential(obs=obs, lambda_=lambda_).create_extended(Nbkg) tot_model = zfit.pdf.SumPDF([signal, background]) loss = ExtendedUnbinnedNLL(model=tot_model, data=data) return loss, mean
def create_loss(): with tf.variable_scope("func1"): a_param = zfit.Parameter("variable_a15151", 1.5, -1., 20., step_size=ztf.constant(0.1)) b_param = zfit.Parameter("variable_b15151", 3.5) c_param = zfit.Parameter("variable_c15151", -0.04) obs1 = zfit.Space(obs='obs1', limits=(-2.4, 9.1)) # load params for sampling a_param.load(true_a) b_param.load(true_b) c_param.load(true_c) gauss1 = zfit.pdf.Gauss(mu=a_param, sigma=b_param, obs=obs1) exp1 = zfit.pdf.Exponential(lambda_=c_param, obs=obs1) sum_pdf1 = 0.9 * gauss1 + exp1 sampled_data = sum_pdf1.create_sampler(n=15000) sampled_data.resample() loss = zfit.loss.UnbinnedNLL(model=sum_pdf1, data=sampled_data, fit_range=obs1) return loss, (a_param, b_param, c_param)
def test_set_values(): upper1 = 5.33 lower1 = 0. param1 = zfit.Parameter('param1', 2., lower1, upper1) param1.set_value(lower1) assert pytest.approx(zfit.run(param1.value()), lower1) param1.set_value(upper1) assert pytest.approx(zfit.run(param1.value()), upper1) with pytest.raises(ValueError): param1.set_value(lower1 - 0.001) with pytest.raises(ValueError): param1.set_value(upper1 + 0.001) fitresult = create_fitresult((zfit.minimize.Minuit, {}, True)) result = fitresult['result'] param_a = fitresult['a_param'] param_b = fitresult['b_param'] param_c = fitresult['c_param'] val_a = fitresult['a'] val_b = fitresult['b'] val_c = fitresult['c'] with pytest.raises(ValueError): param_b.set_value(999) param_c.assign(9999) zfit.param.set_values([param_c, param_b], values=result) assert param_a.value() == val_a assert param_b.value() == val_b assert param_c.value() == val_c param_d = zfit.Parameter("param_d", 12) with pytest.raises(ValueError): zfit.param.set_values([param_d], result)
def test_with_gauss_qtilde(n, min_x): sigma_x = 0.032 minimizer = Minuit() bounds = (-10, 10) obs = zfit.Space("x", limits=bounds) mean = zfit.Parameter("mean", n * sigma_x) sigma = zfit.Parameter("sigma", 1.0) model = zfit.pdf.Gauss(obs=obs, mu=mean, sigma=sigma) data = model.sample(n=1000) nll = UnbinnedNLL(model=model, data=data) minimum = minimizer.minimize(loss=nll) minimum.hesse() x = minimum.params[mean]["value"] x_err = minimum.params[mean]["minuit_hesse"]["error"] x_min = x - x_err * 3 x_max = x + x_err * 3 x_min = max([x_min, min_x]) poinull = POIarray(mean, np.linspace(x_min, x_max, 50)) calculator = AsymptoticCalculator(nll, minimizer) ci = ConfidenceInterval(calculator, poinull, qtilde=True) ci.interval(alpha=0.05, printlevel=1)
def create_loss(): bounds = (0.1, 3.0) obs = zfit.Space('x', limits=bounds) # Data and signal np.random.seed(0) tau = -2.0 beta = -1 / tau bkg = np.random.exponential(beta, 300) peak = np.random.normal(1.2, 0.1, 25) data = np.concatenate((bkg, peak)) data = data[(data > bounds[0]) & (data < bounds[1])] N = len(data) data = zfit.data.Data.from_numpy(obs=obs, array=data) lambda_ = zfit.Parameter("lambda", -2.0, -4.0, -1.0) Nsig = zfit.Parameter("Ns", 20., -20., N) Nbkg = zfit.Parameter("Nbkg", N, 0., N * 1.1) signal = Nsig * zfit.pdf.Gauss(obs=obs, mu=1.2, sigma=0.1) background = Nbkg * zfit.pdf.Exponential(obs=obs, lambda_=lambda_) tot_model = signal + background loss = ExtendedUnbinnedNLL(model=[tot_model], data=[data], fit_range=[obs]) return loss, (Nsig, Nbkg)
def create_loss(n=15000, weights=None): avalue = 1.5 a_param = zfit.Parameter("variable_a15151", avalue, -1., 20., step_size=z.constant(0.1)) a_param.init_val = avalue bvalue = 3.5 b_param = zfit.Parameter("variable_b15151", bvalue, 0, 20) b_param.init_val = bvalue cvalue = -0.04 c_param = zfit.Parameter("variable_c15151", cvalue, -1, 0.) c_param.init_val = cvalue obs1 = zfit.Space(obs='obs1', limits=(-2.4, 9.1)) # load params for sampling a_param.set_value(true_a) b_param.set_value(true_b) c_param.set_value(true_c) gauss1 = zfit.pdf.Gauss(mu=a_param, sigma=b_param, obs=obs1) exp1 = zfit.pdf.Exponential(lam=c_param, obs=obs1) sum_pdf1 = zfit.pdf.SumPDF((gauss1, exp1), 0.7) sampled_data = sum_pdf1.create_sampler(n=n) sampled_data.resample() if weights is not None: sampled_data.set_weights(weights) loss = zfit.loss.UnbinnedNLL(model=sum_pdf1, data=sampled_data) return loss, (a_param, b_param, c_param)
def test_with_gauss_fluctuations(): x_true = -2.0 minimizer = Minuit() bounds = (-10, 10) obs = zfit.Space("x", limits=bounds) mean = zfit.Parameter("mean", 0) sigma = zfit.Parameter("sigma", 1.0) model = zfit.pdf.Gauss(obs=obs, mu=mean, sigma=sigma) npzfile = f"{notebooks_dir}/toys/FC_toys_{x_true}.npz" data = zfit.data.Data.from_numpy(obs=obs, array=np.load(npzfile)["x"]) nll = UnbinnedNLL(model=model, data=data) minimum = minimizer.minimize(loss=nll) minimum.hesse() toys_fname = f"{notebooks_dir}/toys/FC_toys_{x_true}.yml" calculator = FrequentistCalculator.from_yaml(toys_fname, minimum, minimizer) keys = np.unique([k[0].value for k in calculator.keys()]) keys.sort() poinull = POIarray(mean, keys) ci = ConfidenceInterval(calculator, poinull, qtilde=False) with pytest.warns(UserWarning): ci.interval(alpha=0.05, printlevel=0) ci = ConfidenceInterval(calculator, poinull, qtilde=True) ci.interval(alpha=0.05, printlevel=0)
def test_cb_integral(): 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) cbl = CrystalBall(obs=obs, mu=mu_, sigma=sigma_, alpha=alphal_, n=nl_) int_limits = (-1, 3) integral_numeric = cbl.numeric_integrate(limits=int_limits, norm=False) integral = cbl.analytic_integrate(limits=int_limits, norm=False) integral_numeric = zfit.run(integral_numeric) integral = zfit.run(integral) assert pytest.approx(integral_numeric, integral, 1e-5) rnd_limits = sorted(np.random.uniform(*bounds, 13)) integrals = [] for low, up in zip(rnd_limits[:-1], rnd_limits[1:]): integrals.append(cbl.integrate((low, up), norm=False)) integral = np.sum(integrals) integral_full = zfit.run(cbl.integrate(bounds, norm=False)) assert pytest.approx(integral_full, integral)
def test_conv_simple(interpolation): n_points = 2432 obs = zfit.Space("obs1", limits=(-5, 5)) param1 = zfit.Parameter('param1', -3) param2 = zfit.Parameter('param2', 0.3) gauss = 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) conv = zfit.pdf.FFTConvPDFV1(func=func, kernel=gauss, n=100, interpolation=interpolation) if interpolation == 'spline:5': assert conv._conv_spline_order == 5 elif interpolation == 'spline:3': assert conv._conv_spline_order == 3 x = tf.linspace(-5., 5., n_points) probs = conv.pdf(x=x) # true convolution true_conv = true_conv_np(func, gauss, obs, x, xkernel=tf.linspace(*obs.limit1d, num=n_points)) integral = conv.integrate(limits=obs) probs_np = probs.numpy() np.testing.assert_allclose(probs, true_conv, rtol=0.01, atol=0.01) assert pytest.approx(1, rel=1e-3) == integral.numpy() assert len(probs_np) == n_points
def plot_conv_comparison(): # test special properties here n_point_plotting = 2000 obs = zfit.Space("obs1", limits=(-5, 5)) param1 = zfit.Parameter("param1", -3) param2 = zfit.Parameter("param2", 0.3) gauss1 = zfit.pdf.Gauss(0.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) n_points_conv = 50 conv_lin = zfit.pdf.FFTConvPDFV1(func=func, kernel=gauss1, n=n_points_conv, interpolation="linear") conv_spline = zfit.pdf.FFTConvPDFV1(func=func, kernel=gauss1, n=n_points_conv, interpolation="spline") x = tf.linspace(-5.0, 3.0, n_point_plotting) probs_lin = conv_lin.pdf(x=x) probs_spline = conv_spline.pdf(x=x) plt.figure() plt.plot(x, probs_lin, label="linear") plt.plot(x, probs_spline, label="spline") plt.legend() plt.title(f"FFT Conv with interpolation: {n_points_conv} points") plt.show(block=True)
def test_gaussian_constraint_orderbug2( ): # as raised in #162, failed before fixing param1 = zfit.Parameter("param1", 1500) param5 = zfit.Parameter("param2", 0.5) param2 = zfit.Parameter("param3", 1.0) param3 = zfit.Parameter("param4", 1.0) param4 = zfit.Parameter("param5", 1.0) constraint = { "params": [param1, param2, param3, param4, param5], "mu": [1500, 1.0, 1.0, 1.0, 0.5], "sigma": [0.05 * 1500, 0.001, 0.01, 0.1, 0.05 * 0.5] } constr1 = zfit.constraint.GaussianConstraint(**constraint) # param_vals = [1500, 1.0, 1.0, 1.0, 0.5] constraint['params'] = zfit.run(constraint['params']) true_val = true_gauss_constr_value(**constraint) value_tensor = constr1.value() constr_np = zfit.run(value_tensor) assert constr_np == pytest.approx(true_val) assert true_val < 1000 assert true_val == pytest.approx( -8.592, abs=0.1) # if failing, change value. Hardcoded for additional layer
def create_loss(n=15000): a_param = zfit.Parameter("variable_a15151", 1.5, -1., 20., step_size=z.constant(0.1)) b_param = zfit.Parameter("variable_b15151", 3.5, 0, 20) c_param = zfit.Parameter("variable_c15151", -0.04, -1, 0.) obs1 = zfit.Space(obs='obs1', limits=(-2.4, 9.1)) # load params for sampling a_param.set_value(true_a) b_param.set_value(true_b) c_param.set_value(true_c) gauss1 = zfit.pdf.Gauss(mu=a_param, sigma=b_param, obs=obs1) exp1 = zfit.pdf.Exponential(lambda_=c_param, obs=obs1) sum_pdf1 = zfit.pdf.SumPDF((gauss1, exp1), 0.7) sampled_data = sum_pdf1.create_sampler(n=n) sampled_data.resample() loss = zfit.loss.UnbinnedNLL(model=sum_pdf1, data=sampled_data) return loss, (a_param, b_param, c_param)
def test_gaussian_constraint_orderbug2( ): # as raised in #162, failed before fixing param1 = zfit.Parameter("param1", 1500) param5 = zfit.Parameter("param2", 0.5) param2 = zfit.Parameter("param3", 1.0) param3 = zfit.Parameter("param4", 1.0) param4 = zfit.Parameter("param5", 1.0) constraint = { "params": [param1, param2, param3, param4, param5], "observation": [1500, 1.0, 1.0, 1.0, 0.5], "uncertainty": [0.05 * 1500, 0.001, 0.01, 0.1, 0.05 * 0.5], } constr1 = GaussianConstraint(**constraint) # param_vals = [1500, 1.0, 1.0, 1.0, 0.5] constraint["x"] = [m.numpy() for m in constraint["params"]] true_val = true_gauss_constr_value(x=constraint["x"], mu=constraint["observation"], sigma=constraint["uncertainty"]) value_tensor = constr1.value() constr_np = value_tensor.numpy() assert constr_np == pytest.approx(true_val) assert true_val < 1000 assert true_val == pytest.approx( -8.592, abs=0.1) # if failing, change value. Hardcoded for additional layer
def create_test_gauss1(): mu2 = zfit.Parameter("mu2_sampling1", mu_true, mu_true - 2.0, mu_true + 7.0) sigma2 = zfit.Parameter( "sigma2_sampling1", sigma_true, sigma_true - 10.0, sigma_true + 5.0 ) test_gauss1 = TmpGaussian(name="test_gauss1", mu=mu2, sigma=sigma2, obs=obs1) return test_gauss1, mu2, sigma2
def create_mu_sigma_2(nameadd=""): import zfit mu2 = zfit.Parameter("mu2" + nameadd, mu_true, mu_true - 2.0, mu_true + 7.0) sigma2 = zfit.Parameter( "sigma2" + nameadd, sigma_true, sigma_true - 10.0, sigma_true + 5.0 ) return mu2, sigma2
def create_loss_1(): obs = zfit.Space("x", limits=(0.1, 2.0)) data = zfit.data.Data.from_numpy(obs=obs, array=np.random.normal(1.2, 0.1, 10000)) mean = zfit.Parameter("mu", 1.2) sigma = zfit.Parameter("sigma", 0.1) model = zfit.pdf.Gauss(obs=obs, mu=mean, sigma=sigma) loss = UnbinnedNLL(model=model, data=data) return loss
def create_gauss3(nameadd=""): import zfit obs1 = zfit.Space('obs1', (low, high)) mu3 = zfit.Parameter("mu3" + nameadd, mu_true, mu_true - 2., mu_true + 7.) sigma3 = zfit.Parameter("sigma3" + nameadd, sigma_true, sigma_true - 10., sigma_true + 5.) gauss3 = zfit.pdf.Gauss(mu=mu3, sigma=sigma3, obs=obs1) return gauss3
def create_initial_model(obs, sample): # Crystal Ball mu = zfit.Parameter(f"mu_{sample}", 80., 60., 120.) sigma = zfit.Parameter(f'sigma_{sample}', 8., 1., 100.) alpha = zfit.Parameter(f'alpha_{sample}', -.5, -10., 0.) n = zfit.Parameter(f'n_{sample}', 120., 0.01, 200.) model = zfit.pdf.CrystalBall(obs=obs, mu=mu, sigma=sigma, alpha=alpha, n=n) return model
def test_overloaded_operators(): param1 = zfit.Parameter('param1', 5) param2 = zfit.Parameter('param2', 3) param_a = ComposedParameter('param_a', lambda p1: p1 * 4, params=param1) param_b = ComposedParameter('param_b', lambda p2: p2, params=param2) param_c = param_a * param_b assert not isinstance(param_c, zfit.Parameter) param_d = ComposedParameter("param_d", lambda pa, pb: pa + pa * pb ** 2, params=[param_a, param_b]) param_d_val = param_d.numpy() assert param_d_val == (param_a + param_a * param_b ** 2).numpy()
def create_test_pdf_overriden_gauss1(): mu2 = zfit.Parameter("mu2_sampling1", mu_true, mu_true - 2., mu_true + 7.) sigma2 = zfit.Parameter("sigma2_sampling1", sigma_true, sigma_true - 10., sigma_true + 5.) test_gauss1 = TmpGaussianPDFNonNormed(name="test_pdf_nonnormed_gauss1", mu=mu2, sigma=sigma2, obs=obs1) return test_gauss1, mu2, sigma2
def create_loss(): obs = zfit.Space('x', limits=(0.1, 2.0)) data = zfit.data.Data.from_numpy(obs=obs, array=np.random.normal(1.2, 0.1, 10000)) mean = zfit.Parameter("mu", true_mu) sigma = zfit.Parameter("sigma", true_sigma) model = zfit.pdf.Gauss(obs=obs, mu=mean, sigma=sigma) loss = UnbinnedNLL(model=[model], data=[data], fit_range=[obs]) return loss, (mean, sigma)
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
def test_numerical_hessian(): param1 = zfit.Parameter('param1', 4.) param2 = zfit.Parameter('param2', 5.) param3 = zfit.Parameter('param3', 2.) def func1(): return param1 * param2 ** 2 + param3 ** param1 num_hessian = numerical_hessian(func1, params=[param1, param2, param3]) tf_hessian = autodiff_hessian(func1, params=[param1, param2, param3]) np.testing.assert_allclose(num_hessian, tf_hessian, rtol=1e-5, atol=1e-10)
def create_params2(nameadd=""): mu2 = zfit.Parameter("mu25" + nameadd, z.to_real(mu_true) - 0.2, mu_true - 1.0, mu_true + 1.0) sigma2 = zfit.Parameter( "sigma25" + nameadd, z.to_real(sigma_true) - 0.3, sigma_true - 2.0, sigma_true + 2.0, ) return mu2, sigma2
def test_numerical_gradient(): param1 = zfit.Parameter('param1', 4.) param2 = zfit.Parameter('param2', 5.) param3 = zfit.Parameter('param3', 2.) def func1(): return param1 * param2**2 + param3**param1 num_gradients = numerical_gradient(func1, params=[param1, param2, param3]) tf_gradients = autodiff_gradient(func1, params=[param1, param2, param3]) np.testing.assert_allclose(num_gradients, tf_gradients)
def create_params1(nameadd=""): mu1 = zfit.Parameter("mu1" + nameadd, z.to_real(mu_true) - 0.2, mu_true - 5.0, mu_true + 5.0) sigma1 = zfit.Parameter( "sigma1" + nameadd, z.to_real(sigma_true) - 0.3, sigma_true - 4.0, sigma_true + 4.0, ) return mu1, sigma1
def create_params3(nameadd=""): mu3 = zfit.Parameter("mu35" + nameadd, z.to_real(mu_true) - 0.2, mu_true - 1.0, mu_true + 1.0) sigma3 = zfit.Parameter( "sigma35" + nameadd, z.to_real(sigma_true) - 0.3, sigma_true - 2.0, sigma_true + 2.0, ) yield3 = zfit.Parameter("yield35" + nameadd, yield_true + 300, 0, 10000000) return mu3, sigma3, yield3
def test_gaussian_constraint_matrix(): param1 = zfit.Parameter("Param1", 5) param2 = zfit.Parameter("Param2", 6) params = [param1, param2] mu = [3., 6.1] sigma = np.array([[1, 0.3], [0.3, 0.5]]) constr = GaussianConstraint(params=params, mu=mu, sigma=sigma) constr_np = zfit.run(constr.value()) assert constr_np == pytest.approx(3.989638) assert constr.get_dependents() == set(params)
def create_gauss1(nameadd=""): import zfit obs1 = zfit.Space("obs1", (low, high)) mu = zfit.Parameter("mu" + nameadd, mu_true, mu_true - 2.0, mu_true + 7.0) sigma = zfit.Parameter( "sigma" + nameadd, sigma_true, sigma_true - 10.0, sigma_true + 5.0 ) gauss_params1 = zfit.pdf.Gauss(mu=mu, sigma=sigma, obs=obs1, name="gauss_params1") return gauss_params1
def test_gaussian_constraint_shape_errors(): param1 = zfit.Parameter("Param1", 5) param2 = zfit.Parameter("Param2", 6) with pytest.raises(ShapeIncompatibleError): GaussianConstraint([param1, param2], mu=[4, 2, 3], sigma=5) with pytest.raises(ShapeIncompatibleError): GaussianConstraint([param1, param2], mu=[4, 2], sigma=5) with pytest.raises(ShapeIncompatibleError): GaussianConstraint([param1, param2], mu=2, sigma=[1, 4]) with pytest.raises(ShapeIncompatibleError): GaussianConstraint(param1, mu=[4, 2], sigma=[2, 3])