[SciPy-user] Welch's ttest
Angus McMorland
amcmorl@gmail....
Tue Aug 14 18:06:34 CDT 2007
On 15/08/07, Alan G Isaac <aisaac@american.edu> wrote:
> On Tue, 14 Aug 2007, Angus McMorland apparently wrote:
> > def welchs_approximate_ttest
> Just a reminder that nowadays if you post code it is
> helpful to state explicitly what the license is,
> even if you think it is simple enough to obviously
> belong in the public domain. On this list,
> public domain, BSD, or MIT licensing are particularly
> welcome, I believe.
An excellent reminder, thanks Alan. After a quick check to remind
myself what these all mean, the BSD licence will do fine for that
code. For completeness then, the code becomes:
def welchs_approximate_ttest(n1, mean1, sem1, \
n2, mean2, sem2, alpha):
'''Welch''s approximate t-test for the difference of two means of
heteroscedasctic populations.
Implemented from Biometry, Sokal and Rohlf, 3rd ed., 1995, Box 13.4
:Parameters:
n1 : int
number of variates in sample 1
n2 : int
number of variates in sample 2
mean1 : float
mean of sample 1
mean2 : float
mean of sample 2
sem1 : float
standard error of mean1
sem2 : float
standard error of mean2
alpha : float
desired level of significance of test
:Returns:
significant : bool
True if means are significantly different, else False
t_s_prime : float
t_prime value for difference of means
t_alpha_prime : float
critical value of t_prime at given level of significance
Copyright (c) 2007, Angus McMorland
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the University of Auckland, New Zealand nor
the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL,EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.'''
svm1 = sem1**2 * n1
svm2 = sem2**2 * n2
t_s_prime = (mean1 - mean2)/n.sqrt(svm1/n1+svm2/n2)
t_alpha_df1 = scipy.stats.t.ppf(1-alpha/2, n1 - 1)
t_alpha_df2 = scipy.stats.t.ppf(1-alpha/2, n2 - 1)
t_alpha_prime = (t_alpha_df1 * sem1**2 + t_alpha_df2 * sem2**2) / \
(sem1**2 + sem2**2)
return abs(t_s_prime) > t_alpha_prime, t_s_prime, t_alpha_prime
and a test class as well...
class TestBiometry(NumpyTestCase):
def test_welchs_approximate_ttest(self):
chimpanzees = (37, 0.115, 0.017) # n, mean, sem
gorillas = (6, 0.511, 0.144)
case1 = welchs_approximate_ttest(chimpanzees[0], \
chimpanzees[1], \
chimpanzees[2], \
gorillas[0], \
gorillas[1], \
gorillas[2], \
0.05)
self.assertTrue( case1[0] )
self.assertAlmostEqual( case1[1], -2.73, 2 )
self.assertAlmostEqual( case1[2], 2.564, 2 )
female = (10, 8.5, n.sqrt(3.6)/n.sqrt(10))
male = (10, 4.8, n.sqrt(0.9)/n.sqrt(10))
case2 = welchs_approximate_ttest(female[0], \
female[1], \
female[2], \
male[0], \
male[1], \
male[2], 0.001)
self.assertTrue( case2[0] )
self.assertAlmostEqual( case2[1], 5.52, 2 )
self.assertAlmostEqual( case2[2], 4.781, 2 )
In case it's useful to anyone the standard form of the BSD licence can
be found here: http://www.opensource.org/licenses/bsd-license.php
> IMO, there should be an explicit policy that code
> posted to the list without a licensing statement
> will be in the public domain. I believe that is
> the intent of such posts, in general. But this policy
> should be presented as part of the list registration.
Sounds like a good plan to me.
> Cheers,
> Alan Isaac
--
AJC McMorland, PhD Student
Physiology, University of Auckland
More information about the SciPy-user
mailing list