-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_binoculars.py
58 lines (44 loc) · 1.96 KB
/
test_binoculars.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import numpy as np
import pytest
import binoculars
METHODS = ["jeffrey", "wilson", "normal", "clopper-pearson"]
@pytest.mark.parametrize("method", METHODS)
@pytest.mark.parametrize("n", [5, 1e2, 1e3, 1e5, 1e8])
@pytest.mark.parametrize("p", [0.01, 0.5, 0.99])
def test_lower_less_upper(method, n, p):
"""Test the obvious things. lower < p < upper.
I also added some quick tests of the tail selector.
"""
l, u = binoculars.binomial_confidence(p, n, method=method)
assert l < u
assert l < p
assert u > p
assert l == binoculars.binomial_confidence(p, n, method=method, tail="lower")
assert u == binoculars.binomial_confidence(p, n, method=method, tail="upper")
@pytest.mark.parametrize("method", METHODS)
@pytest.mark.parametrize("lower_n, greater_n", [(2, 3), (10, 20), (100, 200)])
def test_more_certain_with_n(method, lower_n, greater_n):
"""Test that certainty diminishes with greater N."""
p = 0.5
lower_l, lower_u = binoculars.binomial_confidence(p, lower_n, method=method)
greater_l, greater_u = binoculars.binomial_confidence(p, greater_n, method=method)
assert lower_l < greater_l
assert lower_u > greater_u
@pytest.mark.parametrize("method", METHODS)
@pytest.mark.parametrize("lower_z, greater_z", [(1, 1.01), (1.96, 2.58)])
def test_z_certainty(method, lower_z, greater_z):
"""Test that the interval tightens with lower Z"""
p, N = 0.5, 100
lower_l, lower_u = binoculars.binomial_confidence(p, N, method=method, z=lower_z)
greater_l, greater_u = binoculars.binomial_confidence(
p, N, method=method, z=greater_z
)
assert lower_l > greater_l
assert lower_u < greater_u
@pytest.mark.parametrize("method", METHODS)
def test_invalid_tail_error(method):
with pytest.raises(ValueError):
binoculars.binomial_confidence(0.1, 10, tail="NOPE", method=method)
def test_invalid_method_error():
with pytest.raises(ValueError):
binoculars.binomial_confidence(0.1, 10, method="NOPE")