shapiroWilkNormalityTest

Performs the Shapiro-Wilk test for normality.

Synopsis

shapiroWilkNormalityTest (x)

Required Arguments

float x[] (Input)
Array of size nObservations containing the observations.

Return Value

The p-value for the Shapiro-Wilk test for normality is returned.

Optional Arguments

shapiroWilkW (Output)
A scalar for the Shapiro-Wilk W test statistic.

Description

The Shapiro-Wilk test for normality is thought by D’Agostino and Stevens (1986, p. 406) to be one of the best omnibus tests of normality. The function is based on the approximations and code given by Royston (1982a, b, c; and 1991). The minimum sample size is 3 and sample sizes as large as 5000 have been validated. In the Shapiro and Wilk test, W is given by

\[W = \left(\sum a_i x_{(i)}\right)^2 / \left(\sum \left(x_i - \overline{x}\right)^2\right)\]

where \(x_{(i)}\) is the i-th largest order statistic and \(\bar{x}\) is the sample mean. Royston (1982 and 1991) gives approximations and tabled values that can be used to compute the coefficients \(a_i\), \(i=1,\ldots,n\), and obtains the significance level of the W statistic.

Example

This example is taken from Conover (1980, pp. 195, 364). The data consists of 50 two-digit numbers taken from a telephone book. The W test fails to reject the null hypothesis of normality at the 0.05 level of significance.

from __future__ import print_function
from numpy import *
from pyimsl.stat.shapiroWilkNormalityTest import shapiroWilkNormalityTest

x = [23.0, 36.0, 54.0, 61.0, 73.0, 23.0,
     37.0, 54.0, 61.0, 73.0, 24.0, 40.0,
     56.0, 62.0, 74.0, 27.0, 42.0, 57.0,
     63.0, 75.0, 29.0, 43.0, 57.0, 64.0,
     77.0, 31.0, 43.0, 58.0, 65.0, 81.0,
     32.0, 44.0, 58.0, 66.0, 87.0, 33.0,
     45.0, 58.0, 68.0, 89.0, 33.0, 48.0,
     58.0, 68.0, 93.0, 35.0, 48.0, 59.0,
     70.0, 97.0]
shapiroWilkW = []

# Shapiro-Wilk test
p_value = shapiroWilkNormalityTest(x, shapiroWilkW=shapiroWilkW)

print("p-value = %11.4f" % (p_value))
print("Shapiro Wilk W statistic = %11.4f" % (shapiroWilkW[0]))

Output

p-value =      0.3473
Shapiro Wilk W statistic =      0.9744