nscore 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nscore/__init__.py +0 -0
- nscore/batch.py +149 -0
- nscore/nonparametric_nsm.py +577 -0
- nscore/nsm.py +703 -0
- nscore/savi.py +626 -0
- nscore/tools/__init__.py +0 -0
- nscore/tools/plotting.py +239 -0
- nscore/utils/__init__.py +2 -0
- nscore/utils/utils_general.py +323 -0
- nscore/utils/utils_wsr.py +151 -0
- nscore/wsr.py +159 -0
- nscore-0.1.0.dist-info/METADATA +16 -0
- nscore-0.1.0.dist-info/RECORD +35 -0
- nscore-0.1.0.dist-info/WHEEL +5 -0
- nscore-0.1.0.dist-info/top_level.txt +2 -0
- scripts/__init__.py +0 -0
- scripts/general/__init__.py +0 -0
- scripts/general/generate_cld_plot.py +133 -0
- scripts/general/large_scale_bernoulli_test_gather_data.py +445 -0
- scripts/general/large_scale_bernoulli_test_process_data.py +106 -0
- scripts/general/large_scale_bernoulli_test_visualize_data.py +542 -0
- scripts/general/multivariate_savi_gather_data.py +113 -0
- scripts/general/multivariate_savi_process_data.py +30 -0
- scripts/general/nonparametric_density_evaluation_summary_statistics.py +48 -0
- scripts/general/nonparametric_density_evaluations.py +181 -0
- scripts/paper_results/__init__.py +0 -0
- scripts/paper_results/evaluate_full_RL_results.py +219 -0
- scripts/paper_results/lbm_data_eval_combine_part1.py +49 -0
- scripts/paper_results/lbm_data_eval_combine_part2.py +48 -0
- scripts/paper_results/lbm_data_eval_combine_part3.py +49 -0
- scripts/paper_results/lbm_data_eval_part1.py +281 -0
- scripts/paper_results/lbm_data_eval_part2.py +268 -0
- scripts/paper_results/lbm_data_eval_part3.py +267 -0
- scripts/paper_results/process_full_RL_results.py +58 -0
- scripts/paper_results/rl_mujoco_cartpole_data.py +27 -0
nscore/__init__.py
ADDED
|
File without changes
|
nscore/batch.py
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"""Batch tests.
|
|
2
|
+
|
|
3
|
+
This module defines batch methods for hypothesis testing.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
from numpy.typing import ArrayLike
|
|
8
|
+
from scipy.stats import barnard_exact
|
|
9
|
+
|
|
10
|
+
from statistical_comparison_core import (
|
|
11
|
+
Decision,
|
|
12
|
+
Hypothesis,
|
|
13
|
+
MirroredTestMixin,
|
|
14
|
+
TestBase,
|
|
15
|
+
TestResult,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class BarnardExactTest(TestBase):
|
|
20
|
+
"""Barnard's exact test.
|
|
21
|
+
|
|
22
|
+
This class is a wrapper around scipy's implementation of Barnard's exact test.
|
|
23
|
+
For more details, refer to scipy's documentation:
|
|
24
|
+
https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.barnard_exact.html
|
|
25
|
+
|
|
26
|
+
Attributes:
|
|
27
|
+
alternative: Specification of the alternative hypothesis.
|
|
28
|
+
alpha: Significance level of the test.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
def __init__(self, alternative: Hypothesis, alpha: float) -> None:
|
|
32
|
+
"""Initializes the test object.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
alternative: Specification of the alternative hypothesis.
|
|
36
|
+
alpha: Significance level of the test.
|
|
37
|
+
"""
|
|
38
|
+
self.alternative = alternative
|
|
39
|
+
self.alpha = alpha
|
|
40
|
+
|
|
41
|
+
def run_on_sequence(
|
|
42
|
+
self, sequence_0: ArrayLike, sequence_1: ArrayLike
|
|
43
|
+
) -> TestResult:
|
|
44
|
+
"""Runs the test on a pair of two Bernoulli sequences.
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
sequence_0: Sequence of Bernoulli data from the first source.
|
|
48
|
+
sequence_1: Sequence of Bernoulli data from the second source.
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
TestResult: Result of the hypothesis test.
|
|
52
|
+
|
|
53
|
+
Raises:
|
|
54
|
+
ValueError: If the input sequences are not Bernoulli data.
|
|
55
|
+
"""
|
|
56
|
+
sequence_0_is_binary = np.all(
|
|
57
|
+
(np.array(sequence_0) == 0) + (np.array(sequence_0) == 1)
|
|
58
|
+
)
|
|
59
|
+
sequence_1_is_binary = np.all(
|
|
60
|
+
(np.array(sequence_1) == 0) + (np.array(sequence_1) == 1)
|
|
61
|
+
)
|
|
62
|
+
if not (sequence_0_is_binary and sequence_1_is_binary):
|
|
63
|
+
raise (ValueError("Input sequences must be all Bernoulli data."))
|
|
64
|
+
num_successes_0 = np.sum(sequence_0).item()
|
|
65
|
+
num_failures_0 = len(sequence_0) - num_successes_0
|
|
66
|
+
num_successes_1 = np.sum(sequence_1).item()
|
|
67
|
+
num_failures_1 = len(sequence_1) - num_successes_1
|
|
68
|
+
table = [[num_successes_0, num_successes_1], [num_failures_0, num_failures_1]]
|
|
69
|
+
|
|
70
|
+
barnard = barnard_exact(
|
|
71
|
+
table,
|
|
72
|
+
alternative=(
|
|
73
|
+
"less" if self.alternative == Hypothesis.P0LessThanP1 else "greater"
|
|
74
|
+
),
|
|
75
|
+
pooled=(len(sequence_0) == len(sequence_1)),
|
|
76
|
+
)
|
|
77
|
+
if barnard.pvalue <= self.alpha:
|
|
78
|
+
decision = Decision.AcceptAlternative
|
|
79
|
+
else:
|
|
80
|
+
decision = Decision.FailToDecide
|
|
81
|
+
result = TestResult(
|
|
82
|
+
decision,
|
|
83
|
+
{"p_value": barnard.pvalue.item(), "statistic": barnard.statistic.item()},
|
|
84
|
+
)
|
|
85
|
+
return result
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class MirroredBarnardExactTest(MirroredTestMixin, TestBase):
|
|
89
|
+
"""A pair of one-sided Barnard's exact tests with mirrored alternatives.
|
|
90
|
+
|
|
91
|
+
In our terminology, a mirrored test is one that runs two one-sided tests
|
|
92
|
+
simultaneously, with the null and the alternaive flipped from each other. This is so
|
|
93
|
+
that it can yield either Decision.AcceptNull or Decision.AcceptAlternative depending
|
|
94
|
+
on the input data, unlike standard one-sided tests that can never 'accept' the null.
|
|
95
|
+
(Those standard tests will at most fail to reject the null, as represented by
|
|
96
|
+
Decision.FailToDecide.)
|
|
97
|
+
|
|
98
|
+
For example, if the alternative is Hypothesis.P0MoreThanP1 and the decision is
|
|
99
|
+
Decision.AcceptNull, it should be interpreted as accepting Hypothesis.P0LessThanP1.
|
|
100
|
+
|
|
101
|
+
The significance level alpha controls the following two errors simultaneously: (1)
|
|
102
|
+
probability of wrongly accepting the alternative when the null is true, and (2)
|
|
103
|
+
probability of wrongly accepting the null when the alternative is true. Note that
|
|
104
|
+
Bonferroni correction is not needed since the null hypothesis for one test is the
|
|
105
|
+
alternative for the other.
|
|
106
|
+
|
|
107
|
+
Attributes:
|
|
108
|
+
alternative: Specification of the alternative hypothesis.
|
|
109
|
+
alpha: Significance level of the test.
|
|
110
|
+
"""
|
|
111
|
+
|
|
112
|
+
_base_class = BarnardExactTest
|
|
113
|
+
|
|
114
|
+
def run_on_sequence(
|
|
115
|
+
self, sequence_0: ArrayLike, sequence_1: ArrayLike
|
|
116
|
+
) -> TestResult:
|
|
117
|
+
"""Runs the test on a pair of two Bernoulli sequences.
|
|
118
|
+
|
|
119
|
+
Args:
|
|
120
|
+
sequence_0: Sequence of Bernoulli data from the first source.
|
|
121
|
+
sequence_1: Sequence of Bernoulli data from the second source.
|
|
122
|
+
|
|
123
|
+
Returns:
|
|
124
|
+
TestResult: Result of the hypothesis test.
|
|
125
|
+
"""
|
|
126
|
+
result_for_alternative = self._test_for_alternative.run_on_sequence(
|
|
127
|
+
sequence_0, sequence_1
|
|
128
|
+
)
|
|
129
|
+
result_for_null = self._test_for_null.run_on_sequence(sequence_0, sequence_1)
|
|
130
|
+
|
|
131
|
+
info = {
|
|
132
|
+
"result_for_alternative": result_for_alternative,
|
|
133
|
+
"result_for_null": result_for_null,
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (not result_for_alternative.decision == Decision.FailToDecide) and (
|
|
137
|
+
result_for_null.decision == Decision.FailToDecide
|
|
138
|
+
):
|
|
139
|
+
decision = Decision.AcceptAlternative
|
|
140
|
+
elif (not result_for_null.decision == Decision.FailToDecide) and (
|
|
141
|
+
result_for_alternative.decision == Decision.FailToDecide
|
|
142
|
+
):
|
|
143
|
+
decision = Decision.AcceptNull
|
|
144
|
+
else:
|
|
145
|
+
decision = Decision.FailToDecide
|
|
146
|
+
|
|
147
|
+
result = TestResult(decision, info)
|
|
148
|
+
|
|
149
|
+
return result
|