4
$\begingroup$

Does Scipy have a z test to compare the mean of two samples? I searched this page but couldn't find one.

$\endgroup$
  • $\begingroup$ I haven't used Python for Stats in a little while but, as I recall, it's best to use Statsmodels instead of Scipy.Stats. $\endgroup$ – Steve S Nov 15 '14 at 11:13
  • 4
    $\begingroup$ This question appears to be off-topic because it is about available functions in Python. $\endgroup$ – gung - Reinstate Monica Nov 15 '14 at 19:20
6
$\begingroup$

Statsmodels has a ztest function that allows you to compare two means, assuming they are independent and have the same standard deviation. See the documentation here

If you need to compare means from distributions with different standard deviation, you should use CompareMeans.ztest_ind. See documentation here.

There might be other functions I'm missing so search through the documentation!

$\endgroup$
6
$\begingroup$

No, but this wouldn't be that hard to write a function for:

def twoSampZ(X1, X2, mudiff, sd1, sd2, n1, n2):
    from numpy import sqrt, abs, round
    from scipy.stats import norm
    pooledSE = sqrt(sd1**2/n1 + sd2**2/n2)
    z = ((X1 - X2) - mudiff)/pooledSE
    pval = 2*(1 - norm.cdf(abs(z)))
    return round(z, 3), round(pval, 4)

where X1 = $\bar{X1}$, X2 = $\bar{X2}$, mudiff = null = $\mu_1 - \mu_2$, sd1 = $\sigma_1$, sd2 = $\sigma_2$, n1 = $n_1$ and n2 = $n_2$. So, going off of this example:

z, p = twoSampZ(28, 33, 0, 14.1, 9.5, 75, 50)
print z, p
$\endgroup$
  • $\begingroup$ Just a little tip: 1 - norm.cdf can be replaced in numpy using norm.sf, which gives better precision. $\endgroup$ – AkiRoss Oct 15 '15 at 9:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.