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 create_loss(constraint=False): 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) if constraint: loss.add_constraints( zfit.constraint.GaussianConstraint(params=mean, observation=true_mu, uncertainty=0.01)) return loss, (mean, sigma)
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_loss_counting(): n = 370 nbkg = 340 Nsig = zfit.Parameter("Nsig", 0, -100.0, 100) Nbkg = zfit.Parameter("Nbkg", nbkg, floating=False) Nobs = zfit.ComposedParameter("Nobs", lambda a, b: a + b, params=[Nsig, Nbkg]) obs = zfit.Space("N", limits=(0, 800)) model = Poisson(obs=obs, lamb=Nobs) data = zfit.data.Data.from_numpy(obs=obs, array=np.array([n])) loss = UnbinnedNLL(model=model, data=data) return loss, Nsig
def test_simple_kde_1d(): # test special properties here # zfit.settings.options['numerical_grad'] = True data = tf.random.normal(shape=(100, 1)) # data = np.random.normal(size=(100, 1)) sigma = zfit.Parameter("sigma", 0.5) lower = ((-5, ), ) upper = ((5, ), ) obs = zfit.Space(["obs1"], limits=(lower, upper)) kde = zphys.unstable.pdf.GaussianKDE(data=data, bandwidth=sigma, obs=obs) probs = kde.pdf(x=data + 0.03) from zfit.loss import UnbinnedNLL data = tf.random.normal(shape=(100, 1)) data = zfit.Data.from_tensor(tensor=data, obs=obs) nll = UnbinnedNLL(model=kde, data=data) minimizer = zfit.minimize.Minuit() minimum = minimizer.minimize(loss=nll) assert minimum.converged
def test_simple_kde_3d(): # test special properties here data = np.random.normal(size=(100, 3)) sigma = zfit.Parameter("sigma", 0.5) sigmas = [sigma, 1., 2] lower = ((-5, -5, -5), ) upper = ((5, 5, 5), ) obs = zfit.Space(["obs1", "obs2", "obs3"], limits=(lower, upper)) kde = zphys.unstable.pdf.GaussianKDE(data=data, bandwidth=sigmas, obs=obs) probs = kde.pdf(x=data + 0.03) probs_np = probs.numpy() from zfit.loss import UnbinnedNLL data = np.random.normal(size=(100, 3)) data = zfit.Data.from_tensor(tensor=data, obs=obs) nll = UnbinnedNLL(model=kde, data=data) minimizer = zfit.minimize.Minuit() minimum = minimizer.minimize(loss=nll) assert minimum.converged