def test_add() -> None:
    hist1 = BootstrapHistogram(bh.axis.Regular(100, -5.0, 5.0), rng=1234)
    hist2 = BootstrapHistogram(bh.axis.Regular(100, -5.0, 5.0), rng=1234)
    hist1.fill(np.random.normal(size=1000))
    hist2.fill(np.random.normal(size=1000))
    a1 = hist1.view()
    a2 = hist2.view()
    hist3 = hist1 + hist2
    assert np.array_equal(hist3.view(), a1 + a2)
def test_divide_by_scalar_samples() -> None:
    hist1 = BootstrapHistogram(bh.axis.Regular(100, -5.0, 5.0), rng=1234)
    hist1.fill(np.random.normal(size=1000))
    scale = 2.0
    a1 = hist1.view() / scale
    hist3 = hist1 / scale
    assert np.array_equal(hist3.view(), a1)
Exemplo n.º 3
0
def test_projection() -> None:
    numsamples = 100
    hist = BootstrapHistogram(
        bh.axis.Regular(10, 0.0, 1.0),
        bh.axis.Regular(10, 0.0, 1.0),
        numsamples=numsamples,
        rng=1234,
    )
    size = 100000
    xdata = np.random.uniform(size=size)
    ydata = np.random.uniform(size=size)
    hist.fill(xdata, ydata)
    hist = hist.project(0)
    X = hist.view()
    mean = np.average(X, axis=1)
    std = np.std(X, axis=1)
    nbins = len(hist.axes[0])
    assert array_almost_equal(mean,
                              size / nbins,
                              delta=5.0 * np.sqrt(size / nbins))
    assert array_almost_equal(
        std,
        np.sqrt(size / nbins),
        delta=5.0 *
        _standard_error_std(size=numsamples, sigma=np.sqrt(size / nbins)),
    )
Exemplo n.º 4
0
def test_projection2() -> None:
    hist = BootstrapHistogram(
        bh.axis.Regular(100, -5.0, 5.0),
        bh.axis.Regular(100, -5.0, 5.0),
        numsamples=10,
        rng=1234,
    )
    size = 100000
    x = np.random.normal(loc=0.0, scale=1.0, size=size)
    y = np.random.normal(loc=0.0, scale=1.0, size=size)
    hist.fill(x, y)
    hist = hist.project(0)
    y = hist.view()[:, np.random.randint(0, hist.numsamples)]
    binwidth = hist.axes[0].edges[1] - hist.axes[0].edges[0]
    mean = np.average(hist.axes[0].centers, weights=y)
    std = np.average((hist.axes[0].centers - mean)**2, weights=y)
    assert array_almost_equal(mean,
                              0.0,
                              delta=5.0 * _standard_error_mean(size=size) +
                              binwidth)
    assert array_almost_equal(std,
                              1.0,
                              delta=5.0 * _standard_error_std(size=size) +
                              binwidth)
    return
def test_view_property() -> None:
    numsamples = 10
    nbins = 5
    hist = BootstrapHistogram(bh.axis.Regular(nbins, -5.0, 5.0),
                              numsamples=numsamples,
                              rng=1234)
    view = hist.view()
    assert np.array_equal(view, np.zeros(shape=(nbins, numsamples)))
def test_project() -> None:
    xax = bh.axis.Regular(2, 0.0, 2.0)
    yax = bh.axis.Regular(3, 0.0, 3.0)
    hist = BootstrapHistogram(xax, yax, numsamples=9, rng=1234)
    assert hist.view(flow=True).shape == (2 + 2, 3 + 2, 9)
    assert hist.project(0).view(flow=True).shape == (2 + 2, 9)
    assert hist.project(1).view(flow=True).shape == (3 + 2, 9)
    assert hist.project(2).view(flow=True).shape == (9, )
    assert hist.project(0, 1).view(flow=True).shape == (2 + 2, 3 + 2, 9)
def test_samples() -> None:
    numsamples = 100
    hist = BootstrapHistogram(bh.axis.Regular(100, 0.0, 1.0),
                              numsamples=numsamples,
                              rng=1234)
    size = 100000
    data = np.random.uniform(size=size)
    hist.fill(data)
    y = hist.view()
    mean = np.average(y, axis=1)
    std = np.std(y, axis=1)
    nbins = len(hist.axes[0])
    assert array_almost_equal(mean,
                              size / nbins,
                              delta=5.0 * np.sqrt(size / nbins))
    assert array_almost_equal(
        std,
        np.sqrt(size / nbins),
        delta=5.0 *
        _standard_error_std(size=numsamples, sigma=np.sqrt(size / nbins)),
    )
    return
def test_fill_with_empty_weighted_array() -> None:
    hist = BootstrapHistogram(bh.axis.Regular(2, 0.0, 2.0),
                              numsamples=9,
                              rng=1234)
    hist.fill([], weight=[])
    assert np.all(hist.view(flow=True) == np.zeros((2 + 2, 9)))