def test_bca_confidence_interval_bounded_estimator(ci_method, rng):
    def fn(data):
        return max(np.mean(data), 0)

    data = (-3, -2, -1)
    ci = confidence_interval(fn,
                             data,
                             ci_method=ci_method,
                             size=5,
                             random_state=rng)
    assert_allclose((0.0, 0.0), ci)
def test_bca_confidence_interval_estimator_returns_int(rng):
    def fn(data):
        return int(np.mean(data))

    data = (1, 2, 3)
    ci = confidence_interval(fn,
                             data,
                             ci_method="bca",
                             size=5,
                             random_state=rng)
    assert_allclose((1.0, 2.0), ci)
Example #3
0
def test_confidence_interval(ci_method, rng):
    data = rng.normal(size=1000)
    par = stats.norm.fit(data)
    dist = stats.norm(par[0], par[1] /
                      len(data)**0.5)  # accuracy of mean is sqrt(n) better
    cl = 0.9
    ci_ref = dist.ppf(0.05), dist.ppf(0.95)
    ci = confidence_interval(np.mean,
                             data,
                             cl=cl,
                             size=1000,
                             ci_method=ci_method)
    assert_almost_equal(ci_ref, ci, decimal=2)
def test_confidence_interval_deprecation(rng):
    from numpy import VisibleDeprecationWarning

    d = [1, 2, 3]
    with pytest.warns(VisibleDeprecationWarning):
        r = confidence_interval(np.mean, d, 0.6, random_state=1)
    assert_equal(r, confidence_interval(np.mean, d, cl=0.6, random_state=1))

    with pytest.warns(VisibleDeprecationWarning):
        r = confidence_interval(np.mean, d, 0.6, "percentile", random_state=1)
    assert_equal(
        r,
        confidence_interval(np.mean,
                            d,
                            cl=0.6,
                            ci_method="percentile",
                            random_state=1),
    )

    with pytest.warns(VisibleDeprecationWarning):
        with pytest.raises(ValueError):
            confidence_interval(np.mean, d, 0.6, "percentile", 1)
def test_confidence_interval_invalid_ci_method_raises():
    msg = "method must be 'percentile' or 'bca'"
    with pytest.raises(ValueError, match=msg):
        confidence_interval(np.mean, (1, 2, 3), ci_method="foobar")
def test_confidence_interval_invalid_p_raises():
    msg = "must be between zero and one"
    with pytest.raises(ValueError, match=msg):
        confidence_interval(np.mean, (1, 2, 3), cl=2)
def run_confidence_interval(n, ci_method):
    x = np.arange(n)
    confidence_interval(np.mean, x, ci_method=ci_method)