Skip to content

Instantly share code, notes, and snippets.

@CnrLwlss
Created March 13, 2015 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CnrLwlss/904a14ebed788073543d to your computer and use it in GitHub Desktop.
Save CnrLwlss/904a14ebed788073543d to your computer and use it in GitHub Desktop.
Z-test in python and alternative...
# Demonstrating significant differences between a
# vector of measurements and a single value
# Using the statsmodels package for doing test
# Using numpy to generate some fake data
from statsmodels.stats import weightstats as stests
import numpy as np
data=np.random.normal(loc=3.4,scale=0.1,size=100)
singleValue=3.3
# Assuming data are normally distributed, we can do z-test
testResult=stests.ztest(data,value=singleValue)
pValue=testResult[1]
print("p-value is: "+str(pValue))
print("")
# For me, it is more convincing NOT to assume normal distribution
# Can make a statement like this:
N=len(data)
ave=np.mean(data)
if(singleValue<ave):
print("Value is less than mean of data and "+str(len(data[data<singleValue]))+" out of "+str(N)+" individual observations are less than value.")
else:
print("Value is greater than mean of data and "+str(len(data[data>singleValue]))+" out of "+str(N)+" individual observations are greater than value.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment