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)
Пример #2
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)),
    )
Пример #3
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 stddev_bootstrap_2d(nevents=100, numbins=1, numsamples=1000):
    # create a 2D histogram
    hist2d = BootstrapHistogram(
        axis.Regular(numbins, 0.0, 1.0),
        axis.Regular(numbins, 0.0, 1.0),
        numsamples=numsamples,
    )
    # fill with some random data
    hist2d.fill(np.random.uniform(size=nevents),
                np.random.uniform(size=nevents))
    histX = hist2d.project(0)
    histY = hist2d.project(1)
    histSum = histX + histY
    return histSum.std()[0]
Пример #5
0
def test_projection3() -> None:
    hist = BootstrapHistogram(
        bh.axis.Regular(3, 0.0, 3.0),
        bh.axis.Regular(2, 0.0, 2.0),
        numsamples=1000,
        rng=1234,
    )
    X = [0.0, 1.0, 1.0, 2.0, 2.0, 2.0]
    Y = [0.0, 1.0, 0.0, 1.0, 0.0, 0.0]
    hist.fill(X, Y)
    hx = hist.project(0)
    hy = hist.project(1)
    assert np.array_equal(hx.nominal.view(), np.array([1.0, 2.0, 3.0]))
    assert np.array_equal(hy.nominal.view(), np.array([4.0, 2.0]))
    delta = 0.1
    assert array_almost_equal(
        np.average(hx.samples.view(), axis=1),
        np.array([1.0, 2.0, 3.0]),
        delta=delta,
    )
    assert array_almost_equal(np.average(hy.samples.view(), axis=1),
                              np.array([4.0, 2.0]),
                              delta=delta)