7

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

ADJ
  • 415
  • 1
  • 4
  • 10

2 Answers2

9

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*(norm.sf(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
VRR
  • 3
  • 4
Nate
  • 768
  • 5
  • 10
  • 1
    Just a little tip: `1 - norm.cdf` can be replaced in numpy using `norm.sf`, which gives better precision. – AkiRoss Oct 15 '15 at 09:07
9

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!

cd98
  • 650
  • 4
  • 16