Beispiel #1
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)),
    )
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)
def test_inequality() -> None:
    hist1 = BootstrapHistogram(bh.axis.Regular(100, -5.0, 5.0))
    hist2 = BootstrapHistogram(bh.axis.Regular(100, -5.0, 5.0))
    data = np.random.normal(size=1000)
    hist1.fill(data)
    hist2.fill(data)
    assert hist1 != hist2
Beispiel #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_sub_by_histogram() -> None:
    hist1 = BootstrapHistogram(bh.axis.Regular(2, 0.0, 2.0), rng=1234)
    hist2 = BootstrapHistogram(bh.axis.Regular(2, 0.0, 2.0), rng=1234)
    hist1.fill([0.0, 1.0, 0.0, 1.0])
    hist2.fill([0.0, 1.0, 0.0, 1.0, 0.0, 1.0])
    hist3 = hist1 - hist2
    assert np.array_equal(hist3.nominal.view(), [-1.0, -1.0])
    assert np.array_equal(hist3.samples, hist1.samples - hist2.samples)
def test_add_scalar() -> None:
    hist1 = BootstrapHistogram(bh.axis.Regular(2, 0.0, 2.0), rng=1234)
    hist1.fill([0.0, 0.0, 1.0])
    hist3 = hist1 + 2
    hist4 = 2 + hist1  # test radd method
    assert np.array_equal(hist3.nominal.view(), [4.0, 3.0])
    assert np.array_equal(hist3.samples, hist1.samples + 2)
    assert hist3 == hist4
def test_divide_by_histogram() -> None:
    hist1 = BootstrapHistogram(bh.axis.Regular(2, 0.0, 2.0), rng=1234)
    hist2 = BootstrapHistogram(bh.axis.Regular(2, 0.0, 2.0), rng=1234)
    hist1.fill([0.0, 1.0] * 400)
    hist2.fill([0.0, 1.0] * 200)
    hist3 = hist1 / hist2
    assert np.array_equal(hist3.nominal.view(), [2.0, 2.0])
    assert np.array_equal(hist3.samples, (hist1.samples / hist2.samples))
def test_divide_by_scalar_nominal() -> None:
    hist = BootstrapHistogram(bh.axis.Regular(1, -1.0, 1.0),
                              numsamples=10,
                              rng=1234)
    hist.fill(0.0)
    hist.fill(0.0)
    scaled = hist / 2.0
    assert np.array_equal(list(hist.nominal.view()), [2.0])
    assert np.array_equal(list(scaled.nominal.view()), [1.0])
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_fill_with_various_length_arrays(numsamples: int, numarray: int,
                                         withseed: bool,
                                         withweight: bool) -> None:
    hist = BootstrapHistogram(bh.axis.Regular(5, 0.0, 1.0),
                              numsamples=numsamples,
                              rng=1234)
    values = np.random.uniform(size=numarray)
    weight = np.random.uniform(size=numarray) if withweight else None
    seed = np.arange(numarray) if withseed else None
    hist.fill(values, weight=weight, seed=seed)
def test_multiply_by_scalar_nominal() -> None:
    hist = BootstrapHistogram(bh.axis.Regular(1, -1.0, 1.0),
                              numsamples=10,
                              rng=1234)
    hist.fill(0.0)
    scaled = hist * 2.0
    scaled2 = 2.0 * hist  # test rmul
    assert np.array_equal(list(hist.nominal.view()), [1.0])
    assert np.array_equal(list(scaled.nominal.view()), [2.0])
    assert scaled2 == scaled
def uniformhist(
    ax: axis.Axis = axis.Variable([0.0, 0.1, 0.3, 0.6, 1.0]),
    size: int = 100,
    numsamples: int = 10,
    low: float = 0.0,
    high: float = 1.0,
) -> BootstrapHistogram:
    hist = BootstrapHistogram(ax, numsamples=numsamples)
    hist.fill(np.random.uniform(low=0.0, high=1.0, size=size))
    return hist
def test_fill_with_record_id_seed() -> None:
    hist1 = BootstrapHistogram(bh.axis.Regular(100, 0.0, 1.0),
                               numsamples=100,
                               rng=1234)
    hist2 = BootstrapHistogram(bh.axis.Regular(100, 0.0, 1.0),
                               numsamples=100,
                               rng=5678)
    data = np.random.uniform(size=10000)
    seed = np.arange(len(data))
    hist1.fill(data, seed=seed)
    hist2.fill(data, seed=seed)
    assert hist1 == hist2
def test_mean() -> None:
    size = 100000
    hist = BootstrapHistogram(bh.axis.Regular(100, 0.0, 1.0),
                              numsamples=100,
                              rng=1234)
    data = np.random.uniform(size=size)
    hist.fill(data)
    nbins = len(hist.axes[0])
    assert array_almost_equal(hist.mean(),
                              size / nbins,
                              delta=5.0 * np.sqrt(size / nbins))
    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]
def stddev_bootstrap_1d_eventID(nevents=100, numbins=1, numsamples=1000):
    # create a 2D histogram
    histX = BootstrapHistogram(axis.Regular(numbins, 0.0, 1.0),
                               numsamples=numsamples)
    histY = BootstrapHistogram(axis.Regular(numbins, 0.0, 1.0),
                               numsamples=numsamples)
    # fill with some random data
    eventID = np.arange(nevents)
    X = np.random.uniform(size=nevents)
    Y = np.random.uniform(size=nevents)
    histX = histX.fill(X, seed=eventID)
    histY = histY.fill(Y, seed=eventID)
    histSum = histX + histY
    return histSum.std()[0]
def test_std() -> 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)
    nbins = len(hist.axes[0])
    assert array_almost_equal(
        hist.std(),
        np.sqrt(size / nbins),
        delta=5.0 *
        _standard_error_std(size=numsamples, sigma=np.sqrt(size / nbins)),
    )
def stddev_bootstrap_1d(nevents=100, numbins=1, numsamples=1000):
    # create a 2D histogram
    histX = BootstrapHistogram(axis.Regular(numbins, 0.0, 1.0),
                               numsamples=numsamples,
                               rng=1234)
    histY = BootstrapHistogram(axis.Regular(numbins, 0.0, 1.0),
                               numsamples=numsamples,
                               rng=1234)
    # fill with some random data
    X = np.random.uniform(size=nevents)
    Y = np.random.uniform(size=nevents)
    histX = histX.fill(X)
    histY = histY.fill(Y)
    histSum = histX + histY
    return histSum.std()[0]
Beispiel #19
0
def uniformhist(
    ax: Tuple[axis.Axis, axis.Axis] = (
        axis.Regular(10, 0.0, 1.0),
        axis.Regular(10, 0.0, 1.0),
    ),
    size: int = 100,
    numsamples: int = 10,
    low: float = 0.0,
    high: float = 1.0,
) -> BootstrapHistogram:
    hist = BootstrapHistogram(*ax, numsamples=numsamples)
    hist.fill(
        np.random.uniform(low=0.0, high=1.0, size=size),
        np.random.uniform(low=0.0, high=1.0, size=size),
    )
    return hist
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
Beispiel #21
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)
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)))
def test_fill_with_invalid_data() -> None:
    hist1d = BootstrapHistogram(bh.axis.Regular(100, -5.0, 5.0),
                                numsamples=10,
                                rng=1234)
    hist2d = BootstrapHistogram(
        bh.axis.Regular(100, -5.0, 5.0),
        bh.axis.Regular(100, -5.0, 5.0),
        numsamples=10,
        rng=1234,
    )
    with pytest.raises(ValueError):
        hist1d.fill()
    with pytest.raises(ValueError):
        hist2d.fill([1, 2], [1])
    with pytest.raises(ValueError):
        hist2d.fill([1], [1, 2])
    with pytest.raises(ValueError):
        hist1d.fill([1, 2], weight=[1])
    with pytest.raises(ValueError):
        hist1d.fill([1], weight=[1, 2])
    with pytest.raises(ValueError):
        hist1d.fill([1], seed=[1, 2])
    with pytest.raises(ValueError):
        hist1d.fill([1, 2], seed=[1])
    return
def test_pickle() -> None:
    hist1 = BootstrapHistogram(bh.axis.Regular(100, -5.0, 5.0), rng=1234)
    hist1.fill(np.random.normal(size=1000))
    hist2 = pickle.loads(pickle.dumps(hist1))
    assert hist1 == hist2
def test_fill_with_integer_weights() -> None:
    hist = BootstrapHistogram(bh.axis.Regular(5, 0.0, 5.0), rng=1234)
    values = [0.0, 1.0, 2.0, 3.0, 3.0, 4.0]
    weights = [0, 1, 2, 3, 4, -1]
    hist.fill(values, weight=weights)
    assert np.array_equal(hist.nominal.view(), [0.0, 1.0, 2.0, 7.0, -1.0])
def test_nominal() -> None:
    hist = BootstrapHistogram(bh.axis.Regular(100, -5.0, 5.0), rng=1234)
    data = np.random.normal(size=1000)
    hist.fill(data)
    arr, _ = np.histogram(data, bins=hist.axes[0].edges)
    assert np.array_equal(hist.nominal.view(), arr)