Use pytest.approx in unit tests
This commit is contained in:
parent
6b43034a50
commit
1fad506a5e
@ -14,6 +14,8 @@
|
|||||||
# You should have received a copy of the GNU Affero General Public License
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
from pytest import approx
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from scipy import stats
|
from scipy import stats
|
||||||
|
|
||||||
@ -38,7 +40,7 @@ def test_beta_ratio_cdf_vs_empirical():
|
|||||||
y2 = [(sample < xx).sum()/sample.size for xx in x]
|
y2 = [(sample < xx).sum()/sample.size for xx in x]
|
||||||
|
|
||||||
# Allow 0.01 tolerance
|
# Allow 0.01 tolerance
|
||||||
assert (np.abs(y1 - y2) < 0.01).all()
|
assert y1 == approx(y2, abs=0.01)
|
||||||
|
|
||||||
def test_beta_ratio_ppf_vs_empirical():
|
def test_beta_ratio_ppf_vs_empirical():
|
||||||
"""Compare beta_ratio.ppf with empirical distribution"""
|
"""Compare beta_ratio.ppf with empirical distribution"""
|
||||||
@ -59,7 +61,7 @@ def test_beta_ratio_ppf_vs_empirical():
|
|||||||
y2 = np.quantile(sample, x)
|
y2 = np.quantile(sample, x)
|
||||||
|
|
||||||
# Allow 0.01 tolerance
|
# Allow 0.01 tolerance
|
||||||
assert (np.abs(y1 - y2) < 0.01).all()
|
assert y1 == approx(y2, abs=0.01)
|
||||||
|
|
||||||
def test_beta_ratio_mean_vs_empirical():
|
def test_beta_ratio_mean_vs_empirical():
|
||||||
"""Compare beta_ratio.mean (vs _munp) with empirical mean"""
|
"""Compare beta_ratio.mean (vs _munp) with empirical mean"""
|
||||||
@ -75,7 +77,7 @@ def test_beta_ratio_mean_vs_empirical():
|
|||||||
sample = (samples_p1 / (1 - samples_p1)) / (samples_p2 / (1 - samples_p2))
|
sample = (samples_p1 / (1 - samples_p1)) / (samples_p2 / (1 - samples_p2))
|
||||||
|
|
||||||
# Allow 0.01 tolerance
|
# Allow 0.01 tolerance
|
||||||
assert np.abs(dist.mean() - sample.mean()) < 0.01
|
assert dist.mean() == approx(sample.mean(), abs=0.01)
|
||||||
|
|
||||||
def test_beta_ratio_var_vs_empirical():
|
def test_beta_ratio_var_vs_empirical():
|
||||||
"""Compare beta_ratio.var (vs _munp) with empirical variance"""
|
"""Compare beta_ratio.var (vs _munp) with empirical variance"""
|
||||||
@ -91,4 +93,4 @@ def test_beta_ratio_var_vs_empirical():
|
|||||||
sample = (samples_p1 / (1 - samples_p1)) / (samples_p2 / (1 - samples_p2))
|
sample = (samples_p1 / (1 - samples_p1)) / (samples_p2 / (1 - samples_p2))
|
||||||
|
|
||||||
# Allow 0.01 tolerance
|
# Allow 0.01 tolerance
|
||||||
assert np.abs(dist.var() - sample.var()) < 0.01
|
assert dist.var() == approx(sample.var(), abs=0.01)
|
||||||
|
@ -14,6 +14,8 @@
|
|||||||
# You should have received a copy of the GNU Affero General Public License
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
from pytest import approx
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from scipy import stats
|
from scipy import stats
|
||||||
|
|
||||||
@ -32,10 +34,7 @@ def test_beta_ratio_vs_jsaffer_pdf():
|
|||||||
|
|
||||||
# Compare with expected values from jsaffer implementation
|
# Compare with expected values from jsaffer implementation
|
||||||
expected = np.load('tests/beta_ratio_vs_jsaffer.npy', allow_pickle=False)[0]
|
expected = np.load('tests/beta_ratio_vs_jsaffer.npy', allow_pickle=False)[0]
|
||||||
|
assert y == approx(expected)
|
||||||
# Allow 1e-10 tolerance
|
|
||||||
diff = np.abs(y - expected)
|
|
||||||
assert (diff < 1e-10).all()
|
|
||||||
|
|
||||||
def test_beta_ratio_vs_jsaffer_cdf():
|
def test_beta_ratio_vs_jsaffer_cdf():
|
||||||
"""Compare beta_ratio.cdf with result from https://github.com/jsaffer/beta_quotient_distribution"""
|
"""Compare beta_ratio.cdf with result from https://github.com/jsaffer/beta_quotient_distribution"""
|
||||||
@ -50,10 +49,7 @@ def test_beta_ratio_vs_jsaffer_cdf():
|
|||||||
|
|
||||||
# Compare with expected values from jsaffer implementation
|
# Compare with expected values from jsaffer implementation
|
||||||
expected = np.load('tests/beta_ratio_vs_jsaffer.npy', allow_pickle=False)[1]
|
expected = np.load('tests/beta_ratio_vs_jsaffer.npy', allow_pickle=False)[1]
|
||||||
|
assert y == approx(expected)
|
||||||
# Allow 1e-10 tolerance
|
|
||||||
diff = np.abs(y - expected)
|
|
||||||
assert (diff < 1e-10).all()
|
|
||||||
|
|
||||||
def _gen_beta_ratio_vs_jsaffer():
|
def _gen_beta_ratio_vs_jsaffer():
|
||||||
"""Generate beta_ratio_vs_jsaffer.npy for test_beta_ratio_vs_jsaffer_pdf/cdf"""
|
"""Generate beta_ratio_vs_jsaffer.npy for test_beta_ratio_vs_jsaffer_pdf/cdf"""
|
||||||
@ -82,7 +78,7 @@ def test_beta_ratio_mean_vs_empirical():
|
|||||||
sample = samples_p1 / samples_p2
|
sample = samples_p1 / samples_p2
|
||||||
|
|
||||||
# Allow 0.01 tolerance
|
# Allow 0.01 tolerance
|
||||||
assert np.abs(dist.mean() - sample.mean()) < 0.01
|
assert dist.mean() == approx(sample.mean(), abs=0.01)
|
||||||
|
|
||||||
def test_beta_ratio_var_vs_empirical():
|
def test_beta_ratio_var_vs_empirical():
|
||||||
"""Compare beta_ratio.var (via beta_ratio._munp) with empirical variance"""
|
"""Compare beta_ratio.var (via beta_ratio._munp) with empirical variance"""
|
||||||
@ -98,4 +94,4 @@ def test_beta_ratio_var_vs_empirical():
|
|||||||
sample = samples_p1 / samples_p2
|
sample = samples_p1 / samples_p2
|
||||||
|
|
||||||
# Allow 0.01 tolerance
|
# Allow 0.01 tolerance
|
||||||
assert np.abs(dist.var() - sample.var()) < 0.01
|
assert dist.var() == approx(sample.var(), abs=0.01)
|
||||||
|
@ -14,11 +14,13 @@
|
|||||||
# You should have received a copy of the GNU Affero General Public License
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import yli
|
from pytest import approx
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from scipy import stats
|
from scipy import stats
|
||||||
|
|
||||||
|
import yli
|
||||||
|
|
||||||
def test_hdi_beta_vs_r():
|
def test_hdi_beta_vs_r():
|
||||||
"""Compare yli.hdi for beta distribution with R binom.bayes"""
|
"""Compare yli.hdi for beta distribution with R binom.bayes"""
|
||||||
|
|
||||||
@ -37,6 +39,4 @@ def test_hdi_beta_vs_r():
|
|||||||
#1 bayes 12 250 13 239 0.0515873 0.02594348 0.0793006 0.05
|
#1 bayes 12 250 13 239 0.0515873 0.02594348 0.0793006 0.05
|
||||||
expected = np.array([0.02594348, 0.0793006])
|
expected = np.array([0.02594348, 0.0793006])
|
||||||
|
|
||||||
# Allow 1e-5 tolerance
|
assert hdi == approx(expected)
|
||||||
diff = np.abs(np.array(hdi) - expected)
|
|
||||||
assert (diff < 1e-5).all()
|
|
||||||
|
Loading…
Reference in New Issue
Block a user