11import unittest
22import numpy as np
33import scipy
4- import random
54import bayesian_bootstrap as bb
65from bayesian_bootstrap import (
76 mean ,
1413)
1514from sklearn .linear_model import LinearRegression
1615
16+ RNG = np .random .default_rng (1337 ) # repeatable pseudorandomness
17+
1718
1819class TestMoments (unittest .TestCase ):
1920 def test_mean (self ):
@@ -23,18 +24,18 @@ def test_mean(self):
2324 self .assertAlmostEqual (len ([s for s in posterior_samples if s < 0 ]), 5000 , delta = 1000 )
2425
2526 def test_variance (self ):
26- X = np . random .uniform (- 1 , 1 , 500 )
27+ X = RNG .uniform (- 1 , 1 , 500 )
2728 posterior_samples = var (X , 10000 )
2829 self .assertAlmostEqual (np .mean (posterior_samples ), 1 / 3.0 , delta = 0.05 )
2930
3031 def test_self_covar (self ):
31- X = np . random .uniform (- 1 , 1 , 500 )
32+ X = RNG .uniform (- 1 , 1 , 500 )
3233 posterior_samples = covar (X , X , 10000 )
3334 self .assertAlmostEqual (np .mean (posterior_samples ), np .var (X ), delta = 0.05 )
3435
3536 def test_covar (self ):
36- X = np . random .uniform (- 1 , 1 , 500 )
37- Y = np . random .uniform (- 1 , 1 , 500 )
37+ X = RNG .uniform (- 1 , 1 , 500 )
38+ Y = RNG .uniform (- 1 , 1 , 500 )
3839 posterior_samples = covar (X , Y , 10000 )
3940 self .assertAlmostEqual (np .mean (posterior_samples ), 0 , delta = 0.05 )
4041
@@ -48,25 +49,25 @@ def test_mean_resample(self):
4849 self .assertAlmostEqual (len ([s for s in posterior_samples if s < 0 ]), 5000 , delta = 1000 )
4950
5051 def test_var_resample (self ):
51- X = np . random .uniform (- 1 , 1 , 500 )
52+ X = RNG .uniform (- 1 , 1 , 500 )
5253 posterior_samples = bayesian_bootstrap (X , np .var , 10000 , 5000 , low_mem = True )
5354 self .assertAlmostEqual (np .mean (posterior_samples ), 1 / 3.0 , delta = 0.05 )
54- X = np . random .uniform (- 1 , 1 , 500 )
55+ X = RNG .uniform (- 1 , 1 , 500 )
5556 posterior_samples = bayesian_bootstrap (X , np .var , 10000 , 5000 , low_mem = False )
5657 self .assertAlmostEqual (np .mean (posterior_samples ), 1 / 3.0 , delta = 0.05 )
5758
5859
5960class TestIntervals (unittest .TestCase ):
6061 def test_central_credible_interval (self ):
61- l , r = central_credible_interval (self ._shuffle (list ( range (10 ) )), alpha = 0.2 )
62- self .assertEqual (l , 1 )
63- self .assertEqual (r , 8 )
64- l , r = central_credible_interval (self ._shuffle (list ( range (10 ) )), alpha = 0.19 )
65- self .assertEqual (l , 1 )
66- self .assertEqual (r , 8 )
67- l , r = central_credible_interval (self ._shuffle (list ( range (20 ) )), alpha = 0.1 )
68- self .assertEqual (l , 1 )
69- self .assertEqual (r , 18 )
62+ l , r = central_credible_interval (self ._shuffle (range (10 )), alpha = 0.2 )
63+ self .assertEqual (l , 0.9 )
64+ self .assertEqual (r , 8.1 )
65+ l , r = central_credible_interval (self ._shuffle (range (10 )), alpha = 0.19 )
66+ self .assertEqual (l , 0.855 )
67+ self .assertEqual (r , 8.145 )
68+ l , r = central_credible_interval (self ._shuffle (range (20 )), alpha = 0.1 )
69+ self .assertAlmostEqual (l , 0.95 )
70+ self .assertEqual (r , 18.05 )
7071
7172 def test_hpdi (self ):
7273 l , r = highest_density_interval (self ._shuffle ([0 , 10 , 1 ] + [1.1 ] * 7 ), alpha = 0.2 )
@@ -78,14 +79,14 @@ def test_hpdi(self):
7879
7980 def _shuffle (self , x ):
8081 x = list (x )
81- random .shuffle (x )
82+ RNG .shuffle (x )
8283 return x
8384
8485
8586class TestRegression (unittest .TestCase ):
8687 def test_parameter_estimation_resampling_low_memory (self ):
87- X = np . random .uniform (0 , 4 , 1000 )
88- y = X + np . random .normal (0 , 1 , 1000 )
88+ X = RNG .uniform (0 , 4 , 1000 )
89+ y = X + RNG .normal (0 , 1 , 1000 )
8990 m = BayesianBootstrapBagging (LinearRegression (), 10000 , 1000 , low_mem = True )
9091 m .fit (X .reshape (- 1 , 1 ), y )
9192 coef_samples = [b .coef_ for b in m .base_models_ ]
@@ -107,8 +108,8 @@ def test_parameter_estimation_resampling_low_memory(self):
107108 self .assertGreater (r , 0 )
108109
109110 def test_parameter_estimation_resampling (self ):
110- X = np . random .uniform (0 , 4 , 1000 )
111- y = X + np . random .normal (0 , 1 , 1000 )
111+ X = RNG .uniform (0 , 4 , 1000 )
112+ y = X + RNG .normal (0 , 1 , 1000 )
112113 m = BayesianBootstrapBagging (LinearRegression (), 10000 , 1000 , low_mem = False )
113114 m .fit (X .reshape (- 1 , 1 ), y )
114115 coef_samples = [b .coef_ for b in m .base_models_ ]
@@ -130,8 +131,8 @@ def test_parameter_estimation_resampling(self):
130131 self .assertGreater (r , 0 )
131132
132133 def test_parameter_estimation_bayes (self ):
133- X = np . random .uniform (0 , 4 , 1000 )
134- y = X + np . random .normal (0 , 1 , 1000 )
134+ X = RNG .uniform (0 , 4 , 1000 )
135+ y = X + RNG .normal (0 , 1 , 1000 )
135136 m = BayesianBootstrapBagging (LinearRegression (), 10000 , low_mem = False )
136137 m .fit (X .reshape (- 1 , 1 ), y )
137138 coef_samples = [b .coef_ for b in m .base_models_ ]
@@ -153,8 +154,8 @@ def test_parameter_estimation_bayes(self):
153154 self .assertGreater (r , 0 )
154155
155156 def test_parameter_estimation_bayes_low_memory (self ):
156- X = np . random .uniform (0 , 4 , 1000 )
157- y = X + np . random .normal (0 , 1 , 1000 )
157+ X = RNG .uniform (0 , 4 , 1000 )
158+ y = X + RNG .normal (0 , 1 , 1000 )
158159 m = BayesianBootstrapBagging (LinearRegression (), 10000 , low_mem = True )
159160 m .fit (X .reshape (- 1 , 1 ), y )
160161 coef_samples = [b .coef_ for b in m .base_models_ ]
@@ -182,12 +183,10 @@ def test_pearsonr():
182183 assert np .mean (bb .pearsonr (x , y , 10000 )) == 1
183184 assert np .mean (bb .pearsonr (x , - y , 10000 )) == - 1
184185
185- np .random .seed (1337 )
186186 x = [0 , 1 , 3 , 6 ]
187187 y = [1 , 2 , 5 , 7 ]
188188 assert np .isclose (np .mean (bb .pearsonr (x , y , 10000 )), scipy .stats .pearsonr (x , y )[0 ], atol = 0.001 )
189189
190- np .random .seed (1337 )
191190 x = np .linspace (- 10 , 10 , 10000 )
192191 y = np .abs (x )
193192 assert np .isclose (scipy .stats .pearsonr (x , y )[0 ], np .mean (bb .pearsonr (x , y , 1000 )), atol = 0.001 )
0 commit comments