Skip to content

Commit d2b476d

Browse files
Merge pull request #14 from Ivan-Pokhabov/dev/graph-tests
feat & test: make first version of graphs criterion and tests for them
2 parents d1314d9 + dac65b0 commit d2b476d

File tree

7 files changed

+165
-2
lines changed

7 files changed

+165
-2
lines changed

stattest/test/exponent.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
from stattest.core.distribution import expon
99
from stattest.test.goodness_of_fit import AbstractGoodnessOfFitTestStatistic
10+
from stattest.test.graph_goodness_of_fit import (
11+
GraphEdgesNumberTestStatistic,
12+
GraphMaxDegreeTestStatistic,
13+
)
1014

1115

1216
class AbstractExponentialityTestStatistic(AbstractGoodnessOfFitTestStatistic, ABC):
@@ -813,4 +817,22 @@ def execute_statistic(self, rvs, **kwargs):
813817
return hg
814818

815819

820+
class GraphEdgesNumberExpTest(AbstractExponentialityTestStatistic, GraphEdgesNumberTestStatistic):
821+
@staticmethod
822+
@override
823+
def code():
824+
super_class = AbstractExponentialityTestStatistic
825+
parent_code = super(super_class, super_class).code()
826+
return f"EdgesNumber_{parent_code}"
827+
828+
829+
class GraphMaxDegreeExpTest(AbstractExponentialityTestStatistic, GraphMaxDegreeTestStatistic):
830+
@staticmethod
831+
@override
832+
def code():
833+
super_class = AbstractExponentialityTestStatistic
834+
parent_code = super(super_class, super_class).code()
835+
return f"MaxDegree_{parent_code}"
836+
837+
816838
# TODO: check all mistype warnings
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from abc import ABC
2+
from typing import Union
3+
4+
from numpy import float64
5+
from typing_extensions import override
6+
7+
from stattest.test.goodness_of_fit import AbstractGoodnessOfFitTestStatistic
8+
9+
10+
class AbstractGraphTestStatistic(AbstractGoodnessOfFitTestStatistic, ABC):
11+
@override
12+
def execute_statistic(self, rvs, **kwargs) -> Union[float, float64]:
13+
dist = self._compute_dist(rvs)
14+
15+
adjacency_list = self._make_adjacency_list(rvs, dist)
16+
statistic = self.get_graph_stat(adjacency_list)
17+
return statistic
18+
19+
@staticmethod
20+
def _make_adjacency_list(rvs, dist: float) -> list[list[int]]:
21+
adjacency_list: list[list[int]] = []
22+
23+
for i in range(len(rvs)):
24+
adjacency_list.append([])
25+
for j in range(i):
26+
if abs(rvs[i] - rvs[j]) < dist:
27+
adjacency_list[i].append(j)
28+
adjacency_list[j].append(i)
29+
30+
return adjacency_list
31+
32+
@staticmethod
33+
def _compute_dist(rvs): # TODO (normalize for different distributions)
34+
return (max(rvs) - min(rvs)) / 10
35+
36+
@staticmethod
37+
def get_graph_stat(graph: list[list[int]]):
38+
raise NotImplementedError("Method is not implemented")
39+
40+
41+
class GraphEdgesNumberTestStatistic(AbstractGraphTestStatistic, ABC):
42+
@staticmethod
43+
@override
44+
def get_graph_stat(graph):
45+
return sum(map(len, graph)) // 2
46+
47+
48+
class GraphMaxDegreeTestStatistic(AbstractGraphTestStatistic, ABC):
49+
@staticmethod
50+
@override
51+
def get_graph_stat(graph):
52+
return max(map(len, graph))

stattest/test/normal.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
from stattest.test.common import ADTestStatistic, KSTestStatistic, LillieforsTest
1010
from stattest.test.goodness_of_fit import AbstractGoodnessOfFitTestStatistic
11+
from stattest.test.graph_goodness_of_fit import (
12+
GraphEdgesNumberTestStatistic,
13+
GraphMaxDegreeTestStatistic,
14+
)
1115

1216

1317
class AbstractNormalityTestStatistic(AbstractGoodnessOfFitTestStatistic, ABC):
@@ -275,8 +279,7 @@ def skew_test(a):
275279
n = len(a)
276280
if n < 8:
277281
raise ValueError(
278-
"skew test is not valid with less than 8 samples; %i samples"
279-
" were given." % int(n)
282+
"skew test is not valid with less than 8 samples; %i samples were given." % int(n)
280283
)
281284
b2 = scipy_stats.skew(a, axis=0)
282285
y = b2 * math.sqrt(((n + 1) * (n + 3)) / (6.0 * (n - 2)))
@@ -2175,5 +2178,23 @@ def stat35(x):
21752178
return rn # Here is the test statistic value
21762179

21772180

2181+
class GraphEdgesNumberNormTest(AbstractNormalityTestStatistic, GraphEdgesNumberTestStatistic):
2182+
@staticmethod
2183+
@override
2184+
def code():
2185+
super_class = AbstractNormalityTestStatistic
2186+
parent_code = super(super_class, super_class).code()
2187+
return f"EdgesNumber_{parent_code}"
2188+
2189+
2190+
class GraphMaxDegreeNormTest(AbstractNormalityTestStatistic, GraphMaxDegreeTestStatistic):
2191+
@staticmethod
2192+
@override
2193+
def code():
2194+
super_class = AbstractNormalityTestStatistic
2195+
parent_code = super(super_class, super_class).code()
2196+
return f"MaxDegree_{parent_code}"
2197+
2198+
21782199
# TODO: fix all weak warnings
21792200
# TODO: check tests
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest as pytest
2+
3+
from stattest.test.exponent import GraphEdgesNumberExpTest
4+
from tests.exponentiality.abstract_exponentiality_test_case import AbstractExponentialityTestCase
5+
6+
7+
@pytest.mark.parametrize(
8+
("data", "result"),
9+
[
10+
([0.713, 0.644, 2.625, 0.740, 0.501, 0.185, 0.982, 1.028, 1.152, 0.267], 12),
11+
([0.039, 3.036, 0.626, 1.107, 0.139, 1.629, 0.050, 0.118, 0.978, 2.699], 7),
12+
],
13+
)
14+
class TestCaseGraphEdgesNumberExponentialityTest(AbstractExponentialityTestCase):
15+
@pytest.fixture
16+
def statistic_test(self):
17+
return GraphEdgesNumberExpTest()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest as pytest
2+
3+
from stattest.test.exponent import GraphMaxDegreeExpTest
4+
from tests.exponentiality.abstract_exponentiality_test_case import AbstractExponentialityTestCase
5+
6+
7+
@pytest.mark.parametrize(
8+
("data", "result"),
9+
[
10+
([0.713, 0.644, 2.625, 0.740, 0.501, 0.185, 0.982, 1.028, 1.152, 0.267], 4),
11+
([0.039, 3.036, 0.626, 1.107, 0.139, 1.629, 0.050, 0.118, 0.978, 2.699], 3),
12+
],
13+
)
14+
class TestCaseGraphMaxDegreeExponentialityTest(AbstractExponentialityTestCase):
15+
@pytest.fixture
16+
def statistic_test(self):
17+
return GraphMaxDegreeExpTest()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest as pytest
2+
3+
from stattest.test.normal import GraphEdgesNumberNormTest
4+
from tests.normality.abstract_normality_test_case import AbstractNormalityTestCase
5+
6+
7+
@pytest.mark.parametrize(
8+
("data", "result"),
9+
[
10+
([-0.264, 0.031, 0.919, 1.751, -0.038, 0.133, 0.643, -0.480, 0.094, -0.527], 9),
11+
([1.311, 3.761, 0.415, 0.764, 0.100, -0.028, -1.516, -0.108, 2.248, 0.229], 11),
12+
],
13+
)
14+
class TestCaseGraphEdgesNumbernormalityTest(AbstractNormalityTestCase):
15+
@pytest.fixture
16+
def statistic_test(self):
17+
return GraphEdgesNumberNormTest()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest as pytest
2+
3+
from stattest.test.normal import GraphMaxDegreeNormTest
4+
from tests.normality.abstract_normality_test_case import AbstractNormalityTestCase
5+
6+
7+
@pytest.mark.parametrize(
8+
("data", "result"),
9+
[
10+
([-0.264, 0.031, 0.919, 1.751, -0.038, 0.133, 0.643, -0.480, 0.094, -0.527], 4),
11+
([1.311, 3.761, 0.415, 0.764, 0.100, -0.028, -1.516, -0.108, 2.248, 0.229], 5),
12+
],
13+
)
14+
class TestCaseGraphEdgesNumbernormalityTest(AbstractNormalityTestCase):
15+
@pytest.fixture
16+
def statistic_test(self):
17+
return GraphMaxDegreeNormTest()

0 commit comments

Comments
 (0)