@@ -17,60 +17,61 @@ class TestBootstrap(unittest.TestCase):
17
17
def test_with_sklearn (self ):
18
18
"""Test that we can bootstrap sklearn estimators."""
19
19
for n_jobs in [None , - 1 ]: # test parallelism
20
- x = np .random .normal (size = (1000 , 1 ))
21
- y = x * 0.5 + np .random .normal (size = (1000 , 1 ))
22
- y = y .flatten ()
23
-
24
- est = LinearRegression ()
25
- est .fit (x , y )
26
-
27
- bs = BootstrapEstimator (est , 50 , n_jobs = n_jobs )
28
- # test that we can fit with the same arguments as the base estimator
29
- bs .fit (x , y )
30
-
31
- # test that we can get the same attribute for the bootstrap as the original, with the same shape
32
- self .assertEqual (np .shape (est .coef_ ), np .shape (bs .coef_ ))
33
-
34
- # test that we can get an interval for the same attribute for the bootstrap as the original,
35
- # with the same shape for the lower and upper bounds
36
- lower , upper = bs .coef__interval ()
37
- for bound in [lower , upper ]:
38
- self .assertEqual (np .shape (est .coef_ ), np .shape (bound ))
39
-
40
- # test that the lower and upper bounds differ
41
- assert (lower <= upper ).all ()
42
- assert (lower < upper ).any ()
43
-
44
- # test that we can do the same thing once we provide percentile bounds
45
- lower , upper = bs .coef__interval (lower = 10 , upper = 90 )
46
- for bound in [lower , upper ]:
47
- self .assertEqual (np .shape (est .coef_ ), np .shape (bound ))
48
-
49
- # test that the lower and upper bounds differ
50
- assert (lower <= upper ).all ()
51
- assert (lower < upper ).any ()
52
-
53
- # test that we can do the same thing with the results of a method, rather than an attribute
54
- self .assertEqual (np .shape (est .predict (x )), np .shape (bs .predict (x )))
55
-
56
- # test that we can get an interval for the same attribute for the bootstrap as the original,
57
- # with the same shape for the lower and upper bounds
58
- lower , upper = bs .predict_interval (x )
59
- for bound in [lower , upper ]:
60
- self .assertEqual (np .shape (est .predict (x )), np .shape (bound ))
61
-
62
- # test that the lower and upper bounds differ
63
- assert (lower <= upper ).all ()
64
- assert (lower < upper ).any ()
65
-
66
- # test that we can do the same thing once we provide percentile bounds
67
- lower , upper = bs .predict_interval (x , lower = 10 , upper = 90 )
68
- for bound in [lower , upper ]:
69
- self .assertEqual (np .shape (est .predict (x )), np .shape (bound ))
70
-
71
- # test that the lower and upper bounds differ
72
- assert (lower <= upper ).all ()
73
- assert (lower < upper ).any ()
20
+ for kind in ['percentile' , 'standard' ]: # test both percentile and pivot intervals
21
+ x = np .random .normal (size = (1000 , 1 ))
22
+ y = x * 0.5 + np .random .normal (size = (1000 , 1 ))
23
+ y = y .flatten ()
24
+
25
+ est = LinearRegression ()
26
+ est .fit (x , y )
27
+
28
+ bs = BootstrapEstimator (est , 50 , n_jobs = n_jobs , bootstrap_type = kind )
29
+ # test that we can fit with the same arguments as the base estimator
30
+ bs .fit (x , y )
31
+
32
+ # test that we can get the same attribute for the bootstrap as the original, with the same shape
33
+ self .assertEqual (np .shape (est .coef_ ), np .shape (bs .coef_ ))
34
+
35
+ # test that we can get an interval for the same attribute for the bootstrap as the original,
36
+ # with the same shape for the lower and upper bounds
37
+ lower , upper = bs .coef__interval ()
38
+ for bound in [lower , upper ]:
39
+ self .assertEqual (np .shape (est .coef_ ), np .shape (bound ))
40
+
41
+ # test that the lower and upper bounds differ
42
+ assert (lower <= upper ).all ()
43
+ assert (lower < upper ).any ()
44
+
45
+ # test that we can do the same thing once we provide percentile bounds
46
+ lower , upper = bs .coef__interval (lower = 10 , upper = 90 )
47
+ for bound in [lower , upper ]:
48
+ self .assertEqual (np .shape (est .coef_ ), np .shape (bound ))
49
+
50
+ # test that the lower and upper bounds differ
51
+ assert (lower <= upper ).all ()
52
+ assert (lower < upper ).any ()
53
+
54
+ # test that we can do the same thing with the results of a method, rather than an attribute
55
+ self .assertEqual (np .shape (est .predict (x )), np .shape (bs .predict (x )))
56
+
57
+ # test that we can get an interval for the same attribute for the bootstrap as the original,
58
+ # with the same shape for the lower and upper bounds
59
+ lower , upper = bs .predict_interval (x )
60
+ for bound in [lower , upper ]:
61
+ self .assertEqual (np .shape (est .predict (x )), np .shape (bound ))
62
+
63
+ # test that the lower and upper bounds differ
64
+ assert (lower <= upper ).all ()
65
+ assert (lower < upper ).any ()
66
+
67
+ # test that we can do the same thing once we provide percentile bounds
68
+ lower , upper = bs .predict_interval (x , lower = 10 , upper = 90 )
69
+ for bound in [lower , upper ]:
70
+ self .assertEqual (np .shape (est .predict (x )), np .shape (bound ))
71
+
72
+ # test that the lower and upper bounds differ
73
+ assert (lower <= upper ).all ()
74
+ assert (lower < upper ).any ()
74
75
75
76
def test_with_econml (self ):
76
77
"""Test that we can bootstrap econml estimators."""
0 commit comments