def test_bias_on_unbiased(method, rng): data = (0, 1, 2, 3) r = bias(np.mean, data, method=method, random_state=rng) if method == "balanced": # bias is exactly zero for linear functions with the balanced bootstrap assert r == 0 else: # bias is not exactly zero for ordinary bootstrap assert r == pytest.approx(0)
def test_bias_on_biased(method, rng): def biased(x): return np.var(x, ddof=0) data = np.arange(100) bad = biased(data) correct = np.var(data, ddof=1) r = bias(biased, data, method=method, size=10000, random_state=rng) sample_bias = bad - correct assert r == pytest.approx(sample_bias, rel=0.05)
def test_bias_on_biased_2(method, rng): def biased(x): n = len(x) return (np.sum(x) + 2) / n data = np.arange(100) bad = biased(data) correct = np.mean(data) r = bias(biased, data, method=method, size=10000, random_state=rng) sample_bias = bad - correct assert r == pytest.approx(sample_bias, rel=0.1)