Skip to content

Commit

Permalink
BUG: fix issues in mstats.ttest_1samp. Add axis keyword.
Browse files Browse the repository at this point in the history
  • Loading branch information
rgommers committed Nov 10, 2013
1 parent 32051ab commit 9511568
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
27 changes: 15 additions & 12 deletions scipy/stats/mstats_basic.py
Expand Up @@ -762,18 +762,21 @@ def sen_seasonal_slopes(x):
#---- --- Inferential statistics ---
#####--------------------------------------------------------------------------

def ttest_onesamp(a, popmean):
a = ma.asarray(a)
x = a.mean(axis=None)
v = a.var(axis=None,ddof=1)
n = a.count(axis=None)
df = n-1
svar = ((n-1)*v) / float(df)
t = (x-popmean)/ma.sqrt(svar*(1.0/n))
prob = betai(0.5*df,0.5,df/(df+t*t))
return t,prob
ttest_onesamp.__doc__ = stats.ttest_1samp.__doc__
ttest_1samp = ttest_onesamp
def ttest_1samp(a, popmean, axis=0):
a, axis = _chk_asarray(a, axis)
if a.size == 0:
return (np.nan, np.nan)

x = a.mean(axis=axis)
v = a.var(axis=axis, ddof=1)
n = a.count(axis=axis)
df = n - 1.
svar = ((n - 1) * v) / df
t = (x - popmean) / ma.sqrt(svar / n)
prob = betai(0.5 * df, 0.5, df / (df + t*t))
return t, prob
ttest_1samp.__doc__ = stats.ttest_1samp.__doc__
ttest_onesamp = ttest_1samp


def ttest_ind(a, b, axis=0):
Expand Down
2 changes: 1 addition & 1 deletion scipy/stats/stats.py
Expand Up @@ -3150,7 +3150,7 @@ def ttest_1samp(a, popmean, axis=0):
t = np.divide(d, denom)
t, prob = _ttest_finish(df, t)

return t,prob
return t, prob


def _ttest_finish(df,t):
Expand Down
29 changes: 29 additions & 0 deletions scipy/stats/tests/test_mstats_basic.py
Expand Up @@ -549,6 +549,7 @@ def test_nd_input(self):
assert_allclose(res_2d[1], [res_1d[1]] * 2)


#TODO: for all ttest functions, add tests with masked array inputs
class TestTtest_rel():

def test_vs_nonmasked(self):
Expand Down Expand Up @@ -614,5 +615,33 @@ def test_empty(self):
assert_(np.all(np.isnan(res1)))


class TestTtest_1samp():

def test_vs_nonmasked(self):
np.random.seed(1234567)
outcome = np.random.randn(20, 4) + [0, 0, 1, 2]

# 1-D inputs
res1 = stats.ttest_1samp(outcome[:, 0], 1)
res2 = mstats.ttest_1samp(outcome[:, 0], 1)
assert_allclose(res1, res2)

# 2-D inputs
res1 = stats.ttest_1samp(outcome[:, 0], outcome[:, 1], axis=None)
res2 = mstats.ttest_1samp(outcome[:, 0], outcome[:, 1], axis=None)
assert_allclose(res1, res2)
res1 = stats.ttest_1samp(outcome[:, :2], outcome[:, 2:], axis=0)
res2 = mstats.ttest_1samp(outcome[:, :2], outcome[:, 2:], axis=0)
assert_allclose(res1, res2)

# Check default is axis=0
res3 = mstats.ttest_1samp(outcome[:, :2], outcome[:, 2:])
assert_allclose(res2, res3)

def test_empty(self):
res1 = mstats.ttest_1samp([], 1)
assert_(np.all(np.isnan(res1)))


if __name__ == "__main__":
run_module_suite()

0 comments on commit 9511568

Please sign in to comment.