Skip to content

Commit

Permalink
started stats tests
Browse files Browse the repository at this point in the history
  • Loading branch information
milcent committed Nov 20, 2020
1 parent 1a4082a commit f500de2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
3 changes: 1 addition & 2 deletions benford/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ def Z_score(frame, N):
N: sample size
Returns:
Series of computed Z scores, or zero if N==0, so there is no
DivisionByZero
Series of computed Z scores
"""
return (frame.AbsDif - (1 / (2 * N))) / sqrt(
(frame.Expected * (1. - frame.Expected)) / N)
Expand Down
26 changes: 19 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from random import choice
import pytest
import numpy as np
import pandas as pd
from ..benford import utils as ut
from ..benford.constants import confs


@pytest.fixture
Expand All @@ -10,13 +12,13 @@ def gen_N():


@pytest.fixture
def gen_N_lower(gen_N):
return np.random.randint(0, gen_N)
def gen_decimals():
return np.random.randint(0, 8)


# @pytest.fixture
# def gen_N_higher(gen_N):
# return np.random.randint(gen_N, 25000)
@pytest.fixture
def gen_N_lower(gen_N):
return np.random.randint(0, gen_N)


@pytest.fixture
Expand All @@ -26,6 +28,16 @@ def gen_array(gen_N):
np.random.randint(1, num))


@pytest.fixture
def choose_digs():
return choice([1, 2, 3, 22, -2])


@pytest.fixture
def choose_confidence():
return choice(list(confs.keys())[1:])


@pytest.fixture
def gen_series(gen_array):
return pd.Series(gen_array)
Expand Down Expand Up @@ -82,8 +94,8 @@ def small_str_foo_series():


@pytest.fixture
def gen_get_digs_df(gen_series):
return ut.get_digs(gen_series, decimals=8)
def gen_get_digs_df(gen_series, gen_decimals):
return ut.get_digs(gen_series, decimals=gen_decimals)


@pytest.fixture
Expand Down
23 changes: 21 additions & 2 deletions tests/test_stats.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
# import pytest
# from ..benford import stats as st
import pytest
from ..benford import stats as st
from ..benford.constants import crit_chi2


def test_Z_score_F1D():
pass

def test_chi_sq_conf_None(gen_join_expect_found_diff_F1D, capsys):
jefd_F1D = gen_join_expect_found_diff_F1D
chi = st.chi_sq(jefd_F1D, len(jefd_F1D) - 1, None)
out, _ = capsys.readouterr()
assert out == "\nChi-square test needs confidence other than None.\n"
assert chi is None


def test_chi_sq_conf_(gen_join_expect_found_diff_F1D,
choose_confidence, capsys):
jefd_F1D = gen_join_expect_found_diff_F1D
ddf = len(jefd_F1D) - 1
confidence = choose_confidence
chis = st.chi_sq(jefd_F1D, ddf, choose_confidence)
assert chis[1] == crit_chi2[ddf][confidence]
13 changes: 7 additions & 6 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
import numpy as np
import pandas as pd
from ..benford import utils as ut

Expand All @@ -22,10 +21,12 @@ def test_set_N_float():
with pytest.raises(ValueError) as context:
ut._set_N_(127.8, -100)


def test_set_N_zero(gen_N):
assert ut._set_N_(0, None) == 1
assert ut._set_N_(0, gen_N) == 1


def test_get_mantissas_less_than_1(gen_array):
assert sum(ut.get_mantissas(gen_array) > 1) == 0

Expand Down Expand Up @@ -135,39 +136,39 @@ def test_get_digs_dec_infer(gen_array):

def test_get_proportions_F1D(gen_proportions_F1D):
prop_f1d = gen_proportions_F1D
assert ((prop_f1d.index >= 1) & (prop_f1d.index <= 9)).all()
# assert ((prop_f1d.index >= 1) & (prop_f1d.index <= 9)).all()
assert prop_f1d.Found.sum() > .99999
assert (prop_f1d.Found >= 0).all()
assert prop_f1d.Counts.dtype == int


def test_get_proportions_F2D(gen_proportions_F2D):
prop_f2d = gen_proportions_F2D
assert ((prop_f2d.index >= 10) & (prop_f2d.index <= 99)).all()
# assert ((prop_f2d.index >= 10) & (prop_f2d.index <= 99)).all()
assert prop_f2d.Found.sum() > .99999
assert (prop_f2d.Found >= 0).all()
assert prop_f2d.Counts.dtype == int


def test_get_proportions_F3D(gen_proportions_F3D):
prop_f3d = gen_proportions_F3D
assert ((prop_f3d.index >= 100) & (prop_f3d.index <= 999)).all()
# assert ((prop_f3d.index >= 100) & (prop_f3d.index <= 999)).all()
assert prop_f3d.Found.sum() > .99999
assert (prop_f3d.Found >= 0).all()
assert prop_f3d.Counts.dtype == int


def test_get_proportions_SD(gen_proportions_SD):
prop_sd = gen_proportions_SD
assert ((prop_sd.index >= 0) & (prop_sd.index <= 9)).all()
# assert ((prop_sd.index >= 0) & (prop_sd.index <= 9)).all()
assert prop_sd.Found.sum() > .99999
assert (prop_sd.Found >= 0).all()
assert prop_sd.Counts.dtype == int


def test_get_proportions_L2D(gen_proportions_L2D):
prop_l2d = gen_proportions_L2D
assert ((prop_l2d.index >= 00) & (prop_l2d.index <= 99)).all()
# assert ((prop_l2d.index >= 00) & (prop_l2d.index <= 99)).all()
assert prop_l2d.Found.sum() > .99999
assert (prop_l2d.Found >= 0).all()
assert prop_l2d.Counts.dtype == int
Expand Down

0 comments on commit f500de2

Please sign in to comment.