Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolo Musmeci authored and Nicolo Musmeci committed Sep 3, 2020
1 parent e4e073a commit 2318a93
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
21 changes: 21 additions & 0 deletions tests/test_computeBayesTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest
from bayes import computeBayesTest

def test_monotonicity_rates_v1():
_,P = computeBayesTest([0.2,0.1],1000)
assert P.loc["A","B"] > 0.5

def test_monotonicity_rates_v2():
_,P1 = computeBayesTest([0.9,0.1],1000)
_,P2 = computeBayesTest([0.2,0.1],1000)
assert P1.loc["A","B"] >= P2.loc["A","B"]

def test_monotonicity_volumes():
_,P1 = computeBayesTest([0.2,0.1],1000)
_,P2 = computeBayesTest([0.2,0.1],100)
assert P1.loc["A","B"] >= P2.loc["A","B"]

def test_monotonicity_confidence():
C1,_ = computeBayesTest([0.2,0.1],1000,credible_interval_prob=80.)
C2,_ = computeBayesTest([0.2,0.1],1000,credible_interval_prob=90.)
assert C2.loc["A","B"][1]-C2.loc["A","B"][0] >= C1.loc["A","B"][1]-C1.loc["A","B"][0]
40 changes: 40 additions & 0 deletions tests/test_computeBayesTestSampleSize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pytest
import numpy as np
import re
from bayes import computeBayesTest,computeBayesTestSampleSize

def empiricalRateSuccess(n,p1,p2,n_mc):
rate_list = np.random.binomial(n/2.,p2,n_mc)/(n/2.)
pval_list = [np.nan for i in rate_list]
for count,rate in enumerate(rate_list):
pval_list[count] = computeBayesTest([p1,rate],n)[1].loc["B","A"]
return(np.mean(np.array(pval_list)>0.8))

def test_size_1():
p1,p2 = 0.1,0.12
n_mc = 100
n = computeBayesTestSampleSize([p1,p2],accuracy=1)
rate_success = empiricalRateSuccess(n,p1,p2,n_mc)
assert ((rate_success<=0.83)and(rate_success>=0.77)),f"Rate success is {rate_success} instead of\
0.8, with sample size of {n}"

def test_size_2():
p1,p2 = 0.5,0.8
n_mc = 100
n = computeBayesTestSampleSize([p1,p2],accuracy=1)
rate_success = empiricalRateSuccess(n,p1,p2,n_mc)
assert ((rate_success<=0.83)and(rate_success>=0.77)),f"""Rate success is {rate_success} instead of """ + \
f"""0.8, with sample size of {n}"""

def test_error_large_diff(capfd):
p1,p2 = 0.1,0.9
computeBayesTestSampleSize([p1,p2],accuracy=1)
out, _ = capfd.readouterr()
assert bool(re.match("""The values in the explored size range are all large enough to """ + \
"""confirm that the positive rates""",out))

def test_error_small_diff(capfd):
p1,p2 = 0.1,0.10001
computeBayesTestSampleSize([p1,p2],accuracy=1)
out, _ = capfd.readouterr()
assert bool(re.match("""The values in the explored size range are too small to confirm """,out))

0 comments on commit 2318a93

Please sign in to comment.