Normality Tests

Test whether a sample of data came from a normal (or a certain) population.

 

Anderson-Darling


def anderson(x:Iterable)->ADTestRes:
"""
Performs Anderson-Darling test
"""


@dataclass
class ADTestRes:
	pvalue:float
	A2:float

 

Examples

Let's inspect whether some given data comes from a normal distribution.


data = [2.39798, -0.16255, 0.54605, 0.68578, -0.78007, 
1.34234, 1.53208, -0.86899, -0.50855, -0.58256, 
-0.54597, 0.08503, 0.38337, 0.26072, 0.34729]

result = st.anderson(data)

print(result)
"""
TestNormRes(
	pval=0.3304, 
	A2=0.3935)
"""

 

 

Kolmogorov-Smirnov


def ks_1samp(
	x:Iterable, 
	cdf:FunctionType=pnorm, 
	args:tuple=())->Ks1SampletestResult:
"""
Performs two.sided Kolmogorov-Smirnov test
"""

@dataclass
class Ks1SampletestResult:
	D:float #test statistic
	pvalue:float
	D_loc:float #location of max distance (D)
	D_sign:int

 

Examples

Let's inspect whether some given data comes from a normal distribution.


data = [2.39798, -0.16255, 0.54605, 0.68578, -0.78007, 
1.34234, 1.53208, -0.86899, -0.50855, -0.58256, 
-0.54597, 0.08503, 0.38337, 0.26072, 0.34729]

#Note that by default the test uses pnorm (normal dist)
result = st.ks_1samp(data)

print(result)
"""
Ks1SampletestResult(
	D=0.1924, 
	pvalue=0.57028, 
	D_loc=-0.86899, 
	D_sign=-1)
"""

 

 

Shapiro-Wilkinson test


def shapiro(x:Iterable)->ShapiroTestResult:
"""
Performs Shapiro-Wilkinson test

- x must be iterable containing only Real numbers
- x must have at least length 3.
- if len(x)>5000, W is accurate, but the p-value may not be.
"""

@dataclass
class ShapiroTestResult:
	pvalue:float
	W:float #test statistics
	msg:str #warning message, if existsocation of max distance (D)
	D_sign:int

 

Examples

Is the given data coming from a normal distribution?


data = [2.39798, -0.16255, 0.54605, 0.68578, -0.78007, 
1.34234, 1.53208, -0.86899, -0.50855, -0.58256, 
-0.54597, 0.08503, 0.38337, 0.26072, 0.34729]

result = st.shapiro(data)

print(result)
"""
ShapiroTestResult(
	pvalue=0.2623, 
	W=0.92885, 
	msg='')
"""