Use pytest.approx in unit tests

This commit is contained in:
RunasSudo 2022-10-13 12:53:52 +11:00
parent 6b43034a50
commit 1fad506a5e
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
3 changed files with 16 additions and 18 deletions

View File

@ -14,6 +14,8 @@
# 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/>.
from pytest import approx
import numpy as np
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]
# 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():
"""Compare beta_ratio.ppf with empirical distribution"""
@ -59,7 +61,7 @@ def test_beta_ratio_ppf_vs_empirical():
y2 = np.quantile(sample, x)
# 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():
"""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))
# 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():
"""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))
# Allow 0.01 tolerance
assert np.abs(dist.var() - sample.var()) < 0.01
assert dist.var() == approx(sample.var(), abs=0.01)

View File

@ -14,6 +14,8 @@
# 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/>.
from pytest import approx
import numpy as np
from scipy import stats
@ -32,10 +34,7 @@ def test_beta_ratio_vs_jsaffer_pdf():
# Compare with expected values from jsaffer implementation
expected = np.load('tests/beta_ratio_vs_jsaffer.npy', allow_pickle=False)[0]
# Allow 1e-10 tolerance
diff = np.abs(y - expected)
assert (diff < 1e-10).all()
assert y == approx(expected)
def test_beta_ratio_vs_jsaffer_cdf():
"""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
expected = np.load('tests/beta_ratio_vs_jsaffer.npy', allow_pickle=False)[1]
# Allow 1e-10 tolerance
diff = np.abs(y - expected)
assert (diff < 1e-10).all()
assert y == approx(expected)
def _gen_beta_ratio_vs_jsaffer():
"""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
# 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():
"""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
# Allow 0.01 tolerance
assert np.abs(dist.var() - sample.var()) < 0.01
assert dist.var() == approx(sample.var(), abs=0.01)

View File

@ -14,11 +14,13 @@
# 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/>.
import yli
from pytest import approx
import numpy as np
from scipy import stats
import yli
def test_hdi_beta_vs_r():
"""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
expected = np.array([0.02594348, 0.0793006])
# Allow 1e-5 tolerance
diff = np.abs(np.array(hdi) - expected)
assert (diff < 1e-5).all()
assert hdi == approx(expected)