Skip to content

Commit

Permalink
tests for bhattacharrya e kullback-leibler funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
milcent committed Apr 17, 2021
1 parent 90e0b2f commit 895e708
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
16 changes: 8 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import numpy as np
import pandas as pd
from ..benford import utils as ut
from ..benford.constants import confs, len_test, rev_digs
from ..benford.constants import confs, rev_digs
from ..benford.expected import _get_expected_digits_


@pytest.fixture
Expand Down Expand Up @@ -32,10 +33,6 @@ def choose_digs_rand():
return choice([1, 2, 3, 22, -2])


@pytest.fixture
def get_random_len_by_digs(choose_digs_rand):
return len_test[choose_digs_rand]

@pytest.fixture
def choose_test():
return choice(["F1D","F2D","F3D","SD","L2D"])
Expand Down Expand Up @@ -167,11 +164,14 @@ def gen_join_expect_found_diff_SD(gen_proportions_SD):
def gen_join_expect_found_diff_L2D(gen_proportions_L2D):
return ut.join_expect_found_diff(gen_proportions_L2D, -2)


@pytest.fixture
def gen_linspaced_zero_one(cuts:int=1000):
return np.linspace(0, 1, cuts)


@pytest.fixture
def gen_random_proportions(gen_linspaced_zero_one, get_random_len_by_digs):
simul = np.random.choice(gen_linspaced_zero_one, get_random_len_by_digs)
return simul / simul.sum()
def gen_random_digs_and_proportions(gen_linspaced_zero_one, choose_digs_rand):
exp = _get_expected_digits_(choose_digs_rand).Expected.values
rand_prop = np.random.choice(gen_linspaced_zero_one, len(exp))
return exp, rand_prop / rand_prop.sum()
23 changes: 21 additions & 2 deletions tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,24 @@ def test_rand_test_conf_9999999(self, gen_join_expect_found_diff_random_test):

class TestBhattacharyya():

def test_coeff(self):
pass
def test_coeff(self, gen_random_digs_and_proportions):
exp, rand_prop = gen_random_digs_and_proportions
bhat_coeff = st._bhattacharyya_coefficient(exp, rand_prop)
assert isinstance(bhat_coeff, float)
assert bhat_coeff >= 0
assert bhat_coeff <= 1

def test_distance(self, gen_random_digs_and_proportions):
exp, rand_prop = gen_random_digs_and_proportions
bhat_dist = st.bhattacharyya_distance(exp, rand_prop)
assert isinstance(bhat_dist, float)
assert bhat_dist >= 0


class TestKLDivergence():

def test_kld(self, gen_random_digs_and_proportions):
exp, rand_prop = gen_random_digs_and_proportions
kl_diverg = st.kullback_leibler_divergence(exp, rand_prop)
assert isinstance(kl_diverg, float)
assert kl_diverg >= 0

0 comments on commit 895e708

Please sign in to comment.