-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_stats.py
169 lines (125 loc) · 5.02 KB
/
test_stats.py
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
"""
Test functions in stats module.
"""
from __future__ import print_function, division
import numpy as np
import unittest
import stats
import scipy.stats
class TruismsTestCase(unittest.TestCase):
def test_true_is_false(self):
self.assertFalse(True)
class StatsTestCase(unittest.TestCase):
def test_pearsonr_with_confidence(self):
for noise in [0.01, 0.1, 0.2, 0.5, 1, 2, 5, 10]:
x = np.random.uniform(0, 1, 100)
y = x + np.random.normal(0, noise, 100)
rho, p, lb, ub = stats.pearsonr_with_confidence(x, y)
self.assertGreater(ub, rho)
self.assertLess(lb, rho)
self.assertLess(ub, 1)
self.assertGreater(lb, -1)
if p < 0.05:
self.assertGreater(lb, 0)
else:
self.assertLess(lb, 0)
for noise in [0.01, 0.1, 0.2, 0.5, 1, 2, 5, 10]:
x = np.random.uniform(0, 1, 100)
y = x**4 + np.random.normal(0, noise, 100)
rho, p, lb, ub = stats.pearsonr_with_confidence(x, y)
self.assertGreater(ub, rho)
self.assertLess(lb, rho)
self.assertLess(ub, 1)
self.assertGreater(lb, -1)
if p < 0.05:
self.assertGreater(lb, 0)
else:
self.assertLess(lb, 0)
def test_pearsonr_partial(self):
x = np.random.normal(0, 1, 200)
z = x + np.random.normal(0, .2, x.shape)
y = 3 * z + np.random.normal(0, .1, x.shape)
rho, p, lb, ub = stats.pearsonr_partial_with_confidence(x, y, [z])
# find best fit line
slope, icpt, _, _, _ = scipy.stats.linregress(z, y)
y_prime = y - (slope * z + icpt)
rho_correct, p_correct, lb_correct, ub_correct = stats.pearsonr_with_confidence(x, y_prime)
self.assertAlmostEqual(rho, rho_correct)
self.assertAlmostEqual(p, p_correct)
self.assertAlmostEqual(lb, lb_correct)
self.assertAlmostEqual(ub, ub_correct)
def test_cov_with_confidence(self):
for noise in [0.01, 0.1, 0.2, 0.5, 1, 2, 5, 10]:
x = np.random.normal(0, 1, 1000)
y = 3 * x + np.random.normal(0, noise, x.shape)
cov, pv, lb, ub = stats.cov_with_confidence(x, y, confidence=0.95)
self.assertGreater(cov, 1)
self.assertGreater(cov, lb)
self.assertLess(cov, ub)
x = np.random.uniform(0, 10, 1000)
y = 3 * x + np.random.normal(0, 0.01, x.shape)
cov, pv, lb, ub = stats.cov_with_confidence(x, y, confidence=0.95)
self.assertGreater(cov, 1)
def test_nansem_gives_same_as_scipy_stats_sem(self):
# 1d array with no nans
x = np.random.normal(0, 1, 1000)
self.assertAlmostEqual(scipy.stats.sem(x), stats.nansem(x))
# 1d array with nans
x = np.concatenate([x, np.nan * np.ones(100,)])
self.assertAlmostEqual(scipy.stats.sem(x[:1000]), stats.nansem(x))
# 2d array with no nans
x = np.random.normal(0, 2, (50, 50))
self.assertAlmostEqual(scipy.stats.sem(x, axis=None), stats.nansem(x, axis=None))
np.testing.assert_array_almost_equal(scipy.stats.sem(x, axis=0), stats.nansem(x, axis=0))
np.testing.assert_array_almost_equal(scipy.stats.sem(x, axis=1), stats.nansem(x, axis=1))
# 2d array with nans
y = x.copy()
y[-5:, :] = np.nan
self.assertAlmostEqual(scipy.stats.sem(y[:-5, :], axis=None), stats.nansem(y, axis=None))
np.testing.assert_array_almost_equal(scipy.stats.sem(y[:-5, :], axis=0),
stats.nansem(y, axis=0))
y = x.copy()
y[:, -5:] = np.nan
self.assertAlmostEqual(scipy.stats.sem(y[:, :-5], axis=None), stats.nansem(y, axis=None))
np.testing.assert_array_almost_equal(scipy.stats.sem(y[:, :-5], axis=1),
stats.nansem(y, axis=1))
def test_pearsonr_difference_significance(self):
# go through a few examples (calculations done using http://www.quantpsy.org/corrtest/corrtest.htm)
r_a = 0.3639
n_a = 91
r_b = 0.0205
n_b = 63
p = 2*0.01556
self.assertAlmostEqual(
p, stats.pearsonr_difference_significance(r_a, n_a, r_b, n_b),
places=3,
)
r_a = 0.3
n_a = 200
r_b = 0.1
n_b = 100
p = 2*0.04585
self.assertAlmostEqual(
p, stats.pearsonr_difference_significance(r_a, n_a, r_b, n_b),
places=3,
)
r_a = 0.7
n_a = 30
r_b = -0.3
n_b = 10
p = 2*0.00276
self.assertAlmostEqual(
p, stats.pearsonr_difference_significance(r_a, n_a, r_b, n_b),
places=3,
)
r_a = -0.1
n_a = 20
r_b = 0.4
n_b = 4
p = 2*0.30529
self.assertAlmostEqual(
p, stats.pearsonr_difference_significance(r_a, n_a, r_b, n_b),
places=3,
)
if __name__ == '__main__':
unittest.main()