Пример #1
0
def test_sample():
    h1 = Hist1D.from_random("norm", bins="10,-5,5", size=1e4)
    h2 = Hist1D(h1.sample(size=1e5), bins=h1.edges)
    # fitting the ratio of the two should give a horizontal line at y=1
    ret = (h1.normalize() / h2.normalize()).fit("slope*x+offset")
    assert abs(ret["params"]["slope"]["value"]) < 0.05
    assert abs(ret["params"]["offset"]["value"] - 1) < 0.01
Пример #2
0
def test_lookup():
    h = Hist1D.from_random(size=50, bins="7,-3,3", random_state=42)
    allclose(h.lookup(h.bin_centers), h.counts)
    assert h.lookup([-10.0]) == h.counts[0]
    assert h.lookup([10.0]) == h.counts[-1]
    assert h.lookup(-10.0) == h.counts[0]
    assert h.lookup(10.0) == h.counts[-1]
Пример #3
0
def test_likelihood():
    h = Hist1D(np.arange(5) * 2, bins="10,0,10")
    f = "p0+0*x"
    ret_chi2 = h.fit(f, draw=False, likelihood=False)
    ret_like = h.fit(f, draw=False, likelihood=True)
    allclose(ret_chi2["params"]["p0"]["value"], 1.0)
    allclose(ret_like["params"]["p0"]["value"], 0.5)

    # all relative errors within <1% when counts are large
    h = Hist1D.from_random("norm",
                           params=[0, 1],
                           size=2000,
                           random_state=42,
                           bins="20,-3,3")
    ret_chi2 = h.fit("a*np.exp(-(x-mu)**2./(2*sigma**2.))",
                     draw=False,
                     likelihood=False)
    ret_like = h.fit("a*np.exp(-(x-mu)**2./(2*sigma**2.))",
                     draw=False,
                     likelihood=True)
    keys = ret_chi2["params"].keys()
    ret_chi2_errors = np.array(
        [ret_chi2["params"][key]["error"] for key in keys])
    ret_like_errors = np.array(
        [ret_like["params"][key]["error"] for key in keys])
    ret_chi2_values = np.array(
        [ret_chi2["params"][key]["value"] for key in keys])
    v = (ret_chi2_errors - ret_like_errors) / ret_chi2_values
    assert (np.abs(v) < 0.01).mean() == 1.0
Пример #4
0
def test_extent():
    h_full = Hist1D.from_random(
        "uniform", params=[-1, 1], size=1e3,
        bins="100,-1,1") + Hist1D.from_random(
            "uniform", params=[0, +1], size=2e3, bins="100,-1,1")

    h_restricted = h_full.restrict(0, 1)
    fit_full = h_full.fit("a+b*x", extent=[0, 1], color="C0", draw=False)
    fit_restricted = h_restricted.fit("a+b*x", color="C3", draw=False)

    # fitted values should be the same since the domain is the same
    assert fit_full["params"] == fit_restricted["params"]

    # check that the histograms of the fit match the input domain
    assert h_restricted._check_consistency(fit_restricted["hfit"])
    assert h_full._check_consistency(fit_full["hfit"])
Пример #5
0
def test_return_func():
    h = Hist1D.from_random("uniform")
    fit = h.fit("a+b*x")
    func = fit["func"]
    hfit = fit["hfit"]

    xs = h.bin_centers
    ys = func(xs)

    allclose(ys, hfit.counts)
Пример #6
0
def test_gaussian():
    np.random.seed(42)
    for mean, sigma in [[0, 1], [1, 2]]:
        h1 = Hist1D.from_random("norm",
                                params=[mean, sigma],
                                bins="10,-5,5",
                                size=1e4,
                                overflow=False)
        for likelihood in [True, False]:
            fit = h1.fit("gaus", likelihood=likelihood)
            assert abs(fit["params"]["mean"]["value"] - h1.mean()) < 0.2
            assert abs(fit["params"]["sigma"]["value"] -
                       h1.std()) / h1.std() < 0.1
            assert (abs(fit["params"]["constant"]["value"] - h1.counts.max()) /
                    h1.counts.max() < 0.2)
Пример #7
0
def test_fromrandom():
    h = Hist1D.from_random("norm", params=[0, 1], size=1e3, random_state=42)
    assert abs(h.mean()) < 0.1
    assert 0.9 < h.std() < 1.1