Created
March 13, 2015 16:30
-
-
Save CnrLwlss/904a14ebed788073543d to your computer and use it in GitHub Desktop.
Z-test in python and alternative...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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