Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundant squaring #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ComStats/comstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def weighted_t_test_(v, weights):
weighted_count = np.nansum(weights, axis=1)
mean = np.nansum(v * weights, axis=1) / np.nansum(weights, axis=1)
t_nom = mean[:, np.newaxis] - mean
var = np.sqrt(np.nansum(((v.T - mean)**2) * weights.T, axis=0) / np.nansum(weights, axis=1))**2
var = np.sqrt(np.nansum(((v.T - mean)**2) * weights.T, axis=0) / (np.nansum(weights, axis=1) - 1))
t_denom = weighted_count * var + weighted_count[:, np.newaxis] * var[:, np.newaxis]
inv_base = 1/weighted_count + 1/weighted_count[:, np.newaxis]
dof = base + base[:, np.newaxis] - 2
Expand Down
48 changes: 28 additions & 20 deletions test/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ def setUp(self):
[3, 0, 1, 3, 0, 0, 2, 1, 2, 3, 3, 1, 0, 0, 2]
])
self.weights = np.array([
[1, 0.1, 0.2, 0.3, 2, 0.1, 0.1, 2, 0.1, 0.1, 0.2, 0.1, 0.3, 0.4, 1],
[1, 0.1, 0.2, 0.3, 2, 0.1, 0.1, 2, 0.1, 0.1, 0.2, 0.1, 0.3, 0.4, 1],
[1, 0.1, 0.2, 0.3, 2, 0.1, 0.1, 2, 0.1, 0.1, 0.2, 0.1, 0.3, 0.4, 1],
[1, 0.1, 0.2, 0.3, 2, 0.1, 0.1, 2, 0.1, 0.1, 0.2, 0.1, 0.3, 0.4, 1]
[1.875000, 0.187500, 0.375000, 0.562500, 3.750000, 0.187500,
0.187500, 3.750000, 0.187500, 0.187500, 0.375000, 0.187500,
0.562500, 0.750000, 1.875000],
[1.875000, 0.187500, 0.375000, 0.562500, 3.750000, 0.187500,
0.187500, 3.750000, 0.187500, 0.187500, 0.375000, 0.187500,
0.562500, 0.750000, 1.875000],
[1.875000, 0.187500, 0.375000, 0.562500, 3.750000, 0.187500,
0.187500, 3.750000, 0.187500, 0.187500, 0.375000, 0.187500,
0.562500, 0.750000, 1.875000],
[1.875000, 0.187500, 0.375000, 0.562500, 3.750000, 0.187500,
0.187500, 3.750000, 0.187500, 0.187500, 0.375000, 0.187500,
0.562500, 0.750000, 1.875000]
])
self.percentage_input_set = np.array([
[0.1, 0.05, 0.05, 0.1, 0.6, 0.0, 0.4, 0.1, 0.1, 0.05, 0.1, 0.0, 0.0, 0.0, 0.1],
Expand Down Expand Up @@ -98,27 +106,27 @@ def test_unweighted_t_test_equal_variance_one_sided(self):
self.assertTrue((scores.round(8) == expected_scores).all())

def test_weighted_t_test(self):
expected_p_values = [[ 1.00000000e+00, 1.42077000e-03, 5.10308380e-01, 7.37402900e-02],
[ 1.42077000e-03, 1.00000000e+00, 6.57160000e-04, 8.87667500e-02],
[ 5.10308380e-01, 6.57160000e-04, 1.00000000e+00, 3.35990600e-02],
[ 7.37402900e-02, 8.87667500e-02, 3.35990600e-02, 1.00000000e+00]]
expected_scores = [[ 0. , -3.53992642, 0.66687841, -1.85781692],
[ 3.53992642, 0. , 3.83253879, 1.76327075],
[-0.66687841, -3.83253879, 0. , -2.23466985],
[ 1.85781692, -1.76327075, 2.23466985, 0. ]]
expected_p_values = [[ 1.0000000e+00, 5.4909000e-04, 6.4793643e-01, 8.8074170e-02],
[ 5.4909000e-04, 1.0000000e+00, 2.3130000e-04, 5.4144420e-02],
[ 6.4793643e-01, 2.3130000e-04, 1.0000000e+00, 4.2462860e-02],
[ 8.8074170e-02, 5.4144420e-02, 4.2462860e-02, 1.0000000e+00]]
expected_scores = [[ 0. , -3.89999446, 0.46159717, -1.76731592],
[ 3.89999446, 0. , 4.22167128, 2.01009271],
[ -0.46159717, -4.22167128, 0. , -2.12593206],
[ 1.76731592, -2.01009271, 2.12593206, 0. ]]
p_values, scores = comstats.t_test(self.input_set, self.weights, {'paired': False, 'equal_variance': False})
self.assertTrue((p_values.round(8) == expected_p_values).all())
self.assertTrue((scores.round(8) == expected_scores).all())

def test_weighted_t_test_one_sided(self):
expected_p_values = [[ 5.00000000e-01, 7.10380000e-04, 2.55154190e-01, 3.68701500e-02],
[ 7.10380000e-04, 5.00000000e-01, 3.28580000e-04, 4.43833800e-02],
[ 2.55154190e-01, 3.28580000e-04, 5.00000000e-01, 1.67995300e-02],
[ 3.68701500e-02, 4.43833800e-02, 1.67995300e-02, 5.00000000e-01]]
expected_scores = [[ 0. , -3.53992642, 0.66687841, -1.85781692],
[ 3.53992642, 0. , 3.83253879, 1.76327075],
[-0.66687841, -3.83253879, 0. , -2.23466985],
[ 1.85781692, -1.76327075, 2.23466985, 0. ]]
expected_p_values = [[ 5.0000000e-01, 2.7454000e-04, 3.2396821e-01, 4.4037080e-02],
[ 2.7454000e-04, 5.0000000e-01, 1.1565000e-04, 2.7072210e-02],
[ 3.2396821e-01, 1.1565000e-04, 5.0000000e-01, 2.1231430e-02],
[ 4.4037080e-02, 2.7072210e-02, 2.1231430e-02, 5.0000000e-01]]
expected_scores = [[ 0. , -3.89999446, 0.46159717, -1.76731592],
[ 3.89999446, 0. , 4.22167128, 2.01009271],
[ -0.46159717, -4.22167128, 0. , -2.12593206],
[ 1.76731592, -2.01009271, 2.12593206, 0. ]]
p_values, scores = comstats.t_test(self.input_set, self.weights, {'paired': False, 'equal_variance': False}, True)
self.assertTrue((p_values.round(8) == expected_p_values).all())
self.assertTrue((scores.round(8) == expected_scores).all())
Expand Down