Hypothesis tests

Shapiro-Wilk normality test calculator

Test whether a sample is compatible with a normal distribution.

Calculator

Enter the sample data and simulate the null distribution of \(W\) via Monte Carlo to obtain an empirical p-value.

Result pending…
Interpretation notes pending…

How to read the chart (step by step): 1) The Monte Carlo simulation generates many normal samples under \(H_0\) of the same size as your sample and computes a \(W\) for each one. 2) The blue bars count how many simulations fell in each range of \(W\): that is why their Y axis is frequency and their X axis is the value of \(W\). 3) The red marker shows your observed \(W\) and the green marker the critical \(W\) for \(\alpha\). Only their horizontal position (X) matters; their vertical height is set artificially so they are visible above the bars.

Explanation

The Shapiro-Wilk test is a statistical test designed to assess whether a data sample can be considered to come from a normal distribution. It is one of the most powerful normality tests, recommended for small and moderate sample sizes.

The test compares:

  • The sorted values of your sample
  • The expected values of a sorted normal sample
If your data "line up" well with what would be expected from a normal sample, the statistic will be large (close to 1). If they deviate from that alignment, the statistic will be small.

The null hypothesis states that the data follow a normal distribution; the alternative states that they do not. The test statistic is denoted by \(W\), and it takes values between 0 and 1: the closer to 1, the greater the compatibility with normality.

The Shapiro-Wilk statistic is built by combining the sorted data \(x_{(i)}\) with coefficients \(a_i\) that depend on \(n\):

\(W = \dfrac{\left(\sum_{i=1}^{m} a_i(x_{(n+1-i)}-x_{(i)})\right)^2}{\sum_{i=1}^{n}(x_i-\bar{x})^2}\) where:

  • \(x_{(i)}\) are the sorted data,
  • \(a_i\) are coefficients computed from the theoretical normal quantiles,
  • the denominator is the total variability of the sample.

Monte Carlo simulation for the p-value

The exact distribution of the \(W\) statistic under \(H_0\) has no closed form, so the p-value is obtained by Monte Carlo simulation. The procedure is as follows:

  1. A random sample of size \(n\) is generated from \(N(0,1)\). The standard normal can be used because \(W\) is invariant to changes in mean and scale: it depends only on the shape of the sorted data, not on its level or spread.
  2. The statistic \(W\) is computed for that simulated sample.
  3. This is repeated \(B\) times (for example, \(B = 5000\)). This produces an empirical distribution of \(W\) under \(H_0\): that is, how \(W\) would behave if the data really were normal.
  4. The empirical p-value is computed as the fraction of simulations with \(W_{\text{sim}} \leq W_{\text{obs}}\): \[p\text{-value} = \dfrac{\#\{W_{\text{sim}} \leq W_{\text{obs}}\}}{B}\] A very small observed \(W\) (far from 1) will fall in the left tail of the simulated distribution, giving a small p-value and leading to a rejection of normality.

The histogram in the chart shows precisely that empirical distribution: the bars accumulate the \(B\) simulated values of \(W\). The red marker shows the \(W\) observed in your sample; if it falls in the left tail (the low-value region), there is evidence against normality. The green marker shows the critical value \(W_\alpha\): the \(\alpha\) percentile of the simulated distribution, the threshold below which \(H_0\) would be rejected.

The random seed lets you reproduce the exact same result: fixing the same seed guarantees that the \(B\) simulated samples are identical across different runs, which makes it easy to share and verify results. With large values of \(B\) (\(\geq 5000\)) the variability of the empirical p-value is small and the estimate is reliable.

The formal decision is made using the p-value: if it is smaller than the significance level \(\alpha\), normality is rejected.

Hypotheses and statistic

\(H_0\): the sample comes from a normal distribution

\(H_1\): the sample does not come from a normal distribution

\(W = \dfrac{\left(\sum_{i=1}^{m} a_i(x_{(n+1-i)}-x_{(i)})\right)^2}{\sum_{i=1}^{n}(x_i-\bar{x})^2}\)

Quick check

In Shapiro-Wilk, the p-value is the basis for the statistical decision. The statistic \(W\) summarizes how much the sorted data deviate from the pattern expected under normality.

\(\text{If } p < \alpha\Rightarrow\) reject \(H_0\)

\(\text{If } p \ge \alpha\Rightarrow\) fail to reject \(H_0\)

A non-significant result does not prove perfect normality: it indicates that, with the evidence available in that sample, no clear deviations are detected. It is advisable to complement it with a Q-Q plot and a histogram.

Worked example

In a quality-control process, the dimensions (in mm) of 10 manufactured parts are measured: 23.1, 24.5, 22.8, 25.2, 24.1, 23.7, 24.9, 23.5, 25.0, 24.3. We want to check, with \(\alpha = 0.05\), whether these data are compatible with a normal distribution.

Step 1 - Sort the data, compute the mean and the sum of squares

Sorted data: 22.8, 23.1, 23.5, 23.7, 24.1, 24.3, 24.5, 24.9, 25.0, 25.2.

\(\bar{x} = \dfrac{22.8 + 23.1 + \cdots + 25.2}{10} = \dfrac{241.1}{10} = 24.11 \text{ mm}\)

\(\displaystyle SS = \sum_{i=1}^{10}(x_i - \bar{x})^2 = (22.8-24.11)^2 + \cdots + (25.2-24.11)^2 = 6.069\)

Step 2 - Compute the coefficients \(a_i\)

The calculator uses Royston's algorithm (AS R94, 1992), which applies two polynomial corrections to the expected normal quantiles to obtain the exact Shapiro-Wilk coefficients. For \(n = 10\) the resulting coefficients are:

Pair \(i\)12345
\(a_i\) (Royston)0.57390.32910.21410.12240.0399

These coefficients match the values tabulated by Shapiro & Wilk (1965) and those computed by R.

Step 3 - Compute the numerator

Each \(a_i\) weights the difference between the \(i\)-th data point from the top and from the bottom of the sorted sample:

\(b = \sum_{i=1}^{5} a_i\bigl(x_{(11-i)} - x_{(i)}\bigr)\)

\(b = 0.5739\cdot(25.2-22.8) + 0.3291\cdot(25.0-23.1) + 0.2141\cdot(24.9-23.5) + 0.1224\cdot(24.5-23.7) + 0.0399\cdot(24.3-24.1)\)

\(b = 1.3774 + 0.6253 + 0.2997 + 0.0979 + 0.0080 = 2.4083\)

Step 4 - Statistic \(W\)

\(W = \dfrac{b^2}{SS} = \dfrac{2.4083^2}{6.069} = \dfrac{5.800}{6.069} \approx 0.9557\)

This value matches the one returned by R: shapiro.test(...)$statistic = 0.9557.

Step 5 - p-value via Monte Carlo simulation

The calculator generates \(B = 5000\) normal \(N(0,1)\) samples of size \(n = 10\), computes \(W\) for each one and estimates the p-value as the proportion of simulations with \(W \leq W_{\text{obs}}\):

\(p\text{-value} = \dfrac{\#\{W_{\text{sim}} \leq 0.9557\}}{5000} \approx 0.73\)

R uses Royston's analytical approximation and reports \(p = 0.7355\); the Monte Carlo simulation converges to the same result for large \(B\). You can reproduce it by entering the data in the calculator with \(B = 5000\) and seed 12345.

Conclusion

Since \(p \approx 0.73 \gg 0.05 = \alpha\), \(H_0\) is not rejected. At a 5% significance level, there is no statistical evidence that the dimensions of the parts fail to follow a normal distribution. This result allows parametric tools such as \(\bar{x}\text{-}R\) control charts to be applied with confidence in later stages of the quality analysis.

How to interpret the result

Rejecting \(H_0\) (p-value < \(\alpha\)) indicates that there is statistical evidence that the data do not come from a normal distribution. However, the cause can vary: heavier tails than normal (leptokurtosis), skewness, or simply the presence of outliers. To identify the type of deviation, complement the test with a Q-Q (quantile-quantile) plot: if the points curve upward in the right tail there is positive skewness; if the tails move away from the reference line, there is excess kurtosis.

Failing to reject \(H_0\) (p-value ≥ \(\alpha\)) does not prove normality; it only indicates that the data are compatible with a normal distribution at the chosen level. With small samples (\(n < 20\)), the test has very little power and will almost never reject \(H_0\), even when the data clearly depart from normal. With large samples (\(n > 200\)), on the other hand, trivial deviations can turn out to be highly significant without affecting the validity of the parametric methods that assume normality. In practice, what matters is whether the deviation is severe enough to invalidate the test you intend to apply.

The Shapiro-Wilk W statistic ranges between 0 and 1: values close to 1 indicate that the sample is consistent with normality; values close to 0 signal a severe deviation. Unlike chi-square- or KS-based tests, Shapiro-Wilk has greater power to detect deviations in small samples and is the recommended normality test for \(n \leq 50\). The tool shows the W value, the p-value and, in the chart, the Q-Q plot with the confidence band; if the points fall within the band, the deviation from normality is compatible with chance at the chosen level.

Frequently asked questions

  • What sample sizes are typical for Shapiro-Wilk? It is widely used for small and moderate sample sizes, although it can also be applied to larger sizes depending on the software.
  • Does a high p-value prove normality? No. It only indicates that there is not enough evidence to reject normality with that sample and that significance level.
  • Should I rely on the test alone? No. Best practice is to combine the formal test with a Q-Q plot and a histogram.

References:

  • Shapiro, S. S. & Wilk, M. B. (1965). An analysis of variance test for normality (complete samples). Biometrika, 52(3-4), 591-611. — Original paper of the test and of the tabulated \(a_i\) coefficients.
  • Royston, J. P. (1992). Approximating the Shapiro-Wilk W-test for non-normality. Statistics and Computing, 2, 117-119. — Polynomial approximation algorithm for the \(a_i\) coefficients implemented in this calculator.
  • Royston, J. P. (1995). Remark AS R94: A remark on Algorithm AS 181: The W-test for normality. Applied Statistics, 44(4), 547-551. — Revised version of the algorithm (AS R94), the basis for the computation of \(W\) in R and in this tool.
  • Wichura, M. J. (1988). Algorithm AS 241: The percentage points of the normal distribution. Applied Statistics, 37(3), 477-484. — Source of the coefficients of the rational approximation of \(\Phi^{-1}(p)\) used in the code.