def test_unbinned_nll(weights, sigma, options): gaussian1, mu1, sigma1 = create_gauss1() gaussian2, mu2, sigma2 = create_gauss2() test_values = tf.constant(test_values_np) test_values = zfit.Data.from_tensor(obs=obs1, tensor=test_values, weights=weights) nll_object = zfit.loss.UnbinnedNLL(model=gaussian1, data=test_values, options=options) minimizer = Minuit(tol=1e-5) status = minimizer.minimize(loss=nll_object, params=[mu1, sigma1]) params = status.params rel_error = 0.12 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], observation=[mu_constr[0], sigma_constr[0]], uncertainty=sigma()) nll_object = UnbinnedNLL(model=gaussian2, data=test_values, constraints=constraints, options=options) minimizer = Minuit(tol=1e-4) status = minimizer.minimize(loss=nll_object, params=[mu2, sigma2]) params = status.params if weights is None: assert params[mu2]['value'] > np.average(test_values_np, weights=weights) assert params[sigma2]['value'] < np.std(test_values_np)
def test_sweights(): minimizer = Minuit() mass, p, loss, Nsig, Nbkg, sig_p, bkg_p = get_data_and_loss() with pytest.raises(ModelNotFittedToData): compute_sweights(loss.model[0], mass) minimizer.minimize(loss) model = loss.model[0] assert is_sum_of_extended_pdfs(model) yields = [Nsig, Nbkg] sweights = compute_sweights(loss.model[0], mass) assert np.allclose( [np.sum(sweights[y]) / get_value(y.value()) for y in yields], 1.0) nbins = 30 hist_conf = dict(bins=nbins, range=[0, 10]) hist_sig_true_p, _ = np.histogram(sig_p, **hist_conf) sel = hist_sig_true_p != 0 hist_sig_true_p = hist_sig_true_p[sel] hist_sig_sweights_p = np.histogram(p, weights=sweights[Nsig], **hist_conf)[0][sel] assert chisquare(hist_sig_sweights_p, hist_sig_true_p)[-1] < 0.01 hist_bkg_true_p, _ = np.histogram(bkg_p, **hist_conf) sel = hist_bkg_true_p != 0 hist_bkg_true_p = hist_bkg_true_p[sel] hist_bkg_sweights_p = np.histogram(p, weights=sweights[Nbkg], **hist_conf)[0][sel] assert chisquare(hist_bkg_sweights_p, hist_bkg_true_p)[-1] < 0.01 with pytest.warns(AboveToleranceWarning): compute_sweights( loss.model[0], np.concatenate([mass, np.random.normal(0.8, 0.1, 100)])) with pytest.raises(ModelNotFittedToData): compute_sweights( loss.model[0], np.concatenate([mass, np.random.normal(0.8, 0.1, 1000)]))
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 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_unbinned_simultaneous_nll(): test_values = tf.constant(test_values_np) test_values = zfit.Data.from_tensor(obs=obs1, tensor=test_values) test_values2 = tf.constant(test_values_np2) test_values2 = zfit.Data.from_tensor(obs=obs1, tensor=test_values2) gaussian1, mu1, sigma1 = create_gauss1() gaussian2, mu2, sigma2 = create_gauss2() gaussian2 = gaussian2.create_extended(zfit.Parameter('yield_gauss2', 5)) nll = zfit.loss.UnbinnedNLL( model=[gaussian1, gaussian2], data=[test_values, test_values2], ) minimizer = Minuit(tolerance=1e-5) status = minimizer.minimize(loss=nll, params=[mu1, sigma1, mu2, sigma2]) params = status.params assert set(nll.get_params()) == {mu1, mu2, sigma1, sigma2} assert params[mu1]['value'] == pytest.approx(np.mean(test_values_np), rel=0.007) assert params[mu2]['value'] == pytest.approx(np.mean(test_values_np2), rel=0.007) assert params[sigma1]['value'] == pytest.approx(np.std(test_values_np), rel=0.007) assert params[sigma2]['value'] == pytest.approx(np.std(test_values_np2), rel=0.007)
def test_binned_nll(weights): obs = zfit.Space("obs1", limits=(-15, 25)) gaussian1, mu1, sigma1 = create_gauss1(obs=obs) gaussian2, mu2, sigma2 = create_gauss2(obs=obs) test_values_np = np.random.normal(loc=mu_true, scale=4, size=(10000, 1)) test_values = tf.constant(test_values_np) test_values = zfit.Data.from_tensor(obs=obs, tensor=test_values, weights=weights) test_values_binned = test_values.create_hist( converter=zfit.hist.histogramdd, bin_kwargs={"bins": 100}) nll_object = zfit.loss.BinnedNLL(model=gaussian1, data=test_values_binned) minimizer = Minuit() status = minimizer.minimize(loss=nll_object, params=[mu1, sigma1]) params = status.params rel_error = 0.035 if weights is None else 0.15 # 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 = zfit.loss.BinnedNLL( model=gaussian2, data=test_values_binned, fit_range=(-np.infty, np.infty), constraints=constraints, ) minimizer = Minuit() 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)
def test_extended_unbinned_nll(): test_values = z.constant(test_values_np) test_values = zfit.Data.from_tensor(obs=obs1, tensor=test_values) gaussian3, mu3, sigma3, yield3 = create_gauss3ext() nll = zfit.loss.ExtendedUnbinnedNLL(model=gaussian3, data=test_values, fit_range=(-20, 20)) assert {mu3, sigma3, yield3} == nll.get_params() minimizer = Minuit() status = minimizer.minimize(loss=nll) params = status.params assert params[mu3]['value'] == pytest.approx(np.mean(test_values_np), rel=0.007) assert params[sigma3]['value'] == pytest.approx(np.std(test_values_np), rel=0.007) assert params[yield3]['value'] == pytest.approx(yield_true, rel=0.007)
def test_unbinned_simultaneous_nll(): mu1, mu2, nll, sigma1, sigma2 = create_simultaneous_loss() minimizer = Minuit(tol=1e-5) status = minimizer.minimize(loss=nll, params=[mu1, sigma1, mu2, sigma2]) params = status.params assert set(nll.get_params()) == {mu1, mu2, sigma1, sigma2} assert params[mu1]["value"] == pytest.approx(np.mean(test_values_np), rel=0.007) assert params[mu2]["value"] == pytest.approx(np.mean(test_values_np2), rel=0.007) assert params[sigma1]["value"] == pytest.approx(np.std(test_values_np), rel=0.007) assert params[sigma2]["value"] == pytest.approx(np.std(test_values_np2), rel=0.007)
def test_extended_unbinned_nll(size): if size is None: test_values = z.constant(test_values_np) size = test_values.shape[0] else: test_values = create_test_values(size) test_values = zfit.Data.from_tensor(obs=obs1, tensor=test_values) gaussian3, mu3, sigma3, yield3 = create_gauss3ext() nll = zfit.loss.ExtendedUnbinnedNLL(model=gaussian3, data=test_values, fit_range=(-20, 20)) assert {mu3, sigma3, yield3} == nll.get_params() minimizer = Minuit(tol=1e-4) status = minimizer.minimize(loss=nll) params = status.params assert params[mu3]['value'] == pytest.approx(zfit.run(tf.math.reduce_mean(test_values)), rel=0.05) assert params[sigma3]['value'] == pytest.approx(zfit.run(tf.math.reduce_std(test_values)), rel=0.05) assert params[yield3]['value'] == pytest.approx(size, rel=0.005)