def test_BinnedNLL_mask(): c = BinnedNLL([5, 1000, 1], [0, 1, 2, 3], expon_cdf) c_unmasked = c(1) c.mask = np.arange(3) != 1 assert c(1) < c_unmasked
def test_weighted_BinnedNLL(): def cdf(x, a): return 1 - np.exp(-a * x) xe = np.array([0, 1, 10]) p = np.diff(cdf(xe, 1)) n = p * 1000 m1 = Minuit(BinnedNLL(n, xe, cdf), 1) m1.migrad() assert m1.values[0] == pytest.approx(1, rel=1e-2) w = np.transpose((n, 4 * n)) m2 = Minuit(BinnedNLL(w, xe, cdf), 1) m2.migrad() assert m2.values[0] == pytest.approx(1, rel=1e-2) assert m2.errors[0] == pytest.approx(2 * m1.errors[0], rel=1e-2)
def test_BinnedNLL(binned, verbose): mle, nx, xe = binned def cdf(x, mu, sigma): return norm(mu, sigma).cdf(x) cost = BinnedNLL(nx, xe, cdf, verbose=verbose) m = Minuit(cost, mu=0, sigma=1, limit_sigma=(0, None)) m.migrad() # binning loses information compared to unbinned case assert_allclose(m.args, mle[1:], rtol=0.15) assert m.errors["mu"] == pytest.approx(1000**-0.5, rel=0.05)
def test_BinnedNLL(binned, verbose): mle, nx, xe = binned def cdf(x, mu, sigma): return norm(mu, sigma).cdf(x) cost = BinnedNLL(nx, xe, cdf, verbose=verbose) assert cost.ndata == len(nx) m = Minuit(cost, mu=0, sigma=1) m.limits["sigma"] = (0, None) m.migrad() # binning loses information compared to unbinned case assert_allclose(m.values, mle[1:], rtol=0.15) assert m.errors["mu"] == pytest.approx(1000**-0.5, rel=0.05) assert m.ndof == len(nx) - 2 assert_allclose(m.fmin.reduced_chi2, 1, atol=0.15)
def test_BinnedNLL_properties(): def cdf(x, a, b): return 0 c = BinnedNLL([1], [1, 2], cdf) assert c.cdf is cdf with pytest.raises(AttributeError): c.cdf = None assert_equal(c.n, [1]) assert_equal(c.xe, [1, 2]) c.n = [2] c.xe = [2, 3] assert_equal(c.n, [2]) assert_equal(c.xe, [2, 3]) with pytest.raises(ValueError): c.n = [1, 2] with pytest.raises(ValueError): c.xe = [1, 2, 3]
def test_BinnedNLL_bad_input_3(): with pytest.raises(ValueError): BinnedNLL([[1, 2, 3]], [1], lambda x, a: 0)
def test_BinnedNLL_mask(): c = BinnedNLL([1, 1000, 2], [0, 1, 2, 3], lambda x, a: x + a) assert c(2) == pytest.approx(-7000, rel=0.1) c.mask = np.arange(3) != 1 assert c(2) == pytest.approx(-3, rel=0.1)