-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathinfo_theory_test.py
More file actions
180 lines (169 loc) · 5.42 KB
/
info_theory_test.py
File metadata and controls
180 lines (169 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Copyright 2019 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for tensorflow_transform.info_theory."""
import unittest
from tensorflow_transform import info_theory, test_case
EPSILON = 1e-4
def _make_hypergeometric_pmf_sum_up_to_one_parameters():
start = 1000
end = 10000
range_length = end - start
num_chunks = 15
assert range_length % num_chunks == 0
chunk_size = int(range_length / num_chunks)
sub_ranges = [(x, x + chunk_size) for x in range(start, end, chunk_size)]
return [ # pylint: disable=g-complex-comprehension
dict(
testcase_name="{}_to_{}".format(a, b),
test_range=range(a, b),
n=end,
y_j=start,
)
for a, b in sub_ranges
]
class InfoTheoryTest(test_case.TransformTestCase):
def testHypergeometricPmf(self):
expected_results = [(0, 0.75), (1, 0.25)]
results = list(info_theory._hypergeometric_pmf(4, 1, 1))
for expected_result, result in zip(expected_results, results):
self.assertEqual(expected_result[0], result[0])
self.assertNear(expected_result[1], result[1], EPSILON)
def testHypergeometricPmf_LargeN(self):
expected_results = [
(0, 0.9508937),
(1, 0.0482198),
(2, 0.0008794),
(3, 7.1e-06),
(4, 2.5e-08),
(5, 0.0),
]
results = list(info_theory._hypergeometric_pmf(1000, 5, 10))
for expected_result, result in zip(expected_results, results):
self.assertEqual(expected_result[0], result[0])
self.assertNear(expected_result[1], result[1], EPSILON)
@test_case.named_parameters(*_make_hypergeometric_pmf_sum_up_to_one_parameters())
def test_hypergeometric_pmf_sum_up_to_one(self, test_range, n, y_j):
for x in test_range:
probs = [prob for _, prob in info_theory._hypergeometric_pmf(n, x, y_j)]
sum_prob = sum(probs)
self.assertNear(sum_prob, 1.0, EPSILON)
@test_case.named_parameters(
dict(
testcase_name="all_co_occur",
n=10,
x_i=10,
y_j=10,
expected=0,
),
dict(
testcase_name="2_co_occur_no_observations",
n=10,
x_i=0,
y_j=0,
expected=0,
),
dict(
testcase_name="2_values_appear_half_the_time",
n=10,
x_i=5,
y_j=5,
expected=0.215411,
),
dict(
testcase_name="2_values_differing_frequencies",
n=10,
x_i=2,
y_j=4,
expected=0.524209,
),
)
def test_calculate_partial_expected_mutual_information(self, n, x_i, y_j, expected):
self.assertNear(
info_theory.calculate_partial_expected_mutual_information(n, x_i, y_j),
expected,
EPSILON,
)
@test_case.named_parameters(
dict(
testcase_name="strongly_positive_mi",
cell_count=2,
row_count=10,
col_count=2,
total_count=14,
expected_mi=0.970854,
),
dict(
testcase_name="weakly_positive_mi",
cell_count=4,
row_count=15,
col_count=6,
total_count=25,
expected_mi=0.608012,
),
dict(
testcase_name="strongly_negative_mi",
cell_count=2,
row_count=10,
col_count=6,
total_count=25,
expected_mi=-0.526069,
),
dict(
testcase_name="weakly_negative_mi",
cell_count=3,
row_count=31,
col_count=4,
total_count=41,
expected_mi=-0.0350454,
),
dict(
testcase_name="zero_mi",
cell_count=4,
row_count=8,
col_count=8,
total_count=16,
expected_mi=0,
),
dict(
testcase_name="invalid_input_zero_cell_count",
cell_count=4,
row_count=0,
col_count=8,
total_count=8,
expected_mi=0,
),
dict(
testcase_name="invalid_input_zero_row_count",
cell_count=4,
row_count=0,
col_count=8,
total_count=8,
expected_mi=0,
),
dict(
testcase_name="invalid_input_zero_col_count",
cell_count=4,
row_count=8,
col_count=0,
total_count=8,
expected_mi=0,
),
)
def test_mutual_information(
self, cell_count, row_count, col_count, total_count, expected_mi
):
per_cell_mi = info_theory.calculate_partial_mutual_information(
cell_count, row_count, col_count, total_count
)
self.assertNear(per_cell_mi, expected_mi, EPSILON)