Hypothesis tests

Shapiro-Wilk normality test calculator

Test whether a sample is compatible with a normal distribution.

Parameters

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

Result pending…

p-value vs. significance level

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.

Interpretation notes pending…

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 simple closed form. Analytical approximations exist (Royston's, which is the one R uses), but this calculator estimates the p-value 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 number \(b\) of simulations at least as extreme as the observed one is counted. In Shapiro–Wilk it is small values of \(W\) that indicate non-normality, so \(b = \#\{W_{\text{sim}} \leq W_{\text{obs}}\}\), and the Monte Carlo p-value is computed as \[\hat p = \dfrac{b+1}{B+1}\] 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.

Why \((b+1)/(B+1)\) rather than \(b/B\)?

The fraction \(b/B\) is the natural estimator of the tail probability \(p = P(W \leq W_{\text{obs}} \mid H_0)\) (in fact it is unbiased for it), but when the p-value is used to make a decision with a finite number of replicates, the \((b+1)/(B+1)\) correction recommended in the Monte Carlo testing literature is preferable (Davison and Hinkley, 1997; North, Curtis and Sham, 2002; Phipson and Smyth, 2010). The rationale is as follows:

  • Exact validity of the test. Under \(H_0\), the observed \(W\) and the \(B\) simulated ones are \(B+1\) exchangeable draws from the same distribution, so the rank of the observed value among them is uniform on \(\{1, \dots, B+1\}\). Therefore \(P\bigl(\hat p \leq \alpha\bigr) \leq \alpha\) for any \(B\), with equality when \(\alpha(B+1)\) is an integer: the type I error rate never exceeds the nominal \(\alpha\). With \(b/B\), by contrast, \(P(b/B \leq \alpha) = (\lfloor \alpha B \rfloor + 1)/(B+1)\), which can slightly exceed \(\alpha\) (with \(B = 5000\) and \(\alpha = 0.05\) it is \(251/5001 \approx 0.0502\)).
  • It never returns \(p = 0\). If no replicate is as extreme as the sample, \(b/B\) would return a zero p-value, implying a certainty that \(B\) replicates cannot provide. The corrected version returns \(1/(B+1)\), the smallest probability a simulation of size \(B\) can resolve.
  • The cost is negligible. The two estimators differ by at most \(1/(B+1)\) (≈ 0.0002 with \(B = 5000\)), far below the simulation error except in the extreme tail, so it only matters in borderline cases, and there it errs on the conservative side.

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\): approximately the \(\alpha\) percentile of the simulated distribution, chosen so that \(W_{\text{obs}} < W_\alpha\) is exactly equivalent to \(\hat p < \alpha\), so the chart and the decision always agree.

Monte Carlo uncertainty

The random seed lets you reproduce the exact same result: fixing the same seed guarantees that the \(B\) simulated samples are identical across runs in this web page. R and Python use native generators and may give different Monte Carlo results, even with the same seed. But the Monte Carlo p-value is still an estimate of the p-value that infinitely many replicates would give, \(p = P(W \leq W_{\text{obs}} \mid H_0)\). Since the same computation of \(W\) is applied to the observed sample and to the simulated ones, simulation error is the only difference between the two.

The number of replicates that are as extreme follows a binomial distribution, \(b \sim \text{Bin}(B, p)\), so the standard error of the estimated p-value is approximately \(\sqrt{p(1-p)/B}\): it depends both on the p-value itself and on the number of replicates. With \(B = 5000\), it is \(\approx 0.0031\) near \(p = 0.05\) (an approximate 95% margin of \(\pm 0.006\)) and \(\approx 0.007\) near \(p = 0.5\). Besides the standard error, the calculator shows a 95% Wilson confidence interval for \(p\) based on \(b\) and \(B\), which remains informative when \(b\) is small or zero (with \(b = 0\) its upper limit is \(\approx 3.84/B\)). If \(\alpha\) falls inside that interval, the simulation is not enough to tell which side of \(\alpha\) the p-value lies on, and the calculator warns about it.

That precision is acceptable for many purposes, but it is not universally sufficient. The error shrinks as \(1/\sqrt{B}\) (quadrupling \(B\) halves it) and reaching a standard error \(\delta\) takes about \(B \approx p(1-p)/\delta^2\) replicates: for example, about 47,500 for \(\delta = 0.001\) near \(p = 0.05\). If the p-value is close to \(\alpha\), or if very small p-values must be estimated precisely, increase \(B\).

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 (1992; AS R94, 1995), which starts from the expected normal quantiles and applies polynomial corrections to the extreme coefficients to approximate the Shapiro-Wilk coefficients. They are not the exact coefficients, whose computation requires the covariance matrix of normal order statistics. For \(n = 10\), Royston's coefficients and those tabulated by Shapiro and Wilk (1965) are:

Pair \(i\)12345
\(a_i\) (Royston, calculator)0.57370.32900.21430.12280.0401
\(a_i\) (Shapiro and Wilk table, 1965)0.57390.32910.21410.12240.0399

Royston's approximation differs from the tabulated values in the fourth decimal place and is the same one R uses. The steps below use the tabulated values, which are the usual choice for hand calculation; with Royston's values, \(b\) and \(W\) agree to four decimal places.

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 counts the simulations with \(W_{\text{sim}} \leq W_{\text{obs}}\). With seed 12345 there are \(b = 3683\), so:

\(\hat p = \dfrac{b+1}{B+1} = \dfrac{3684}{5001} \approx 0.7367\)

(The uncorrected fraction, \(3683/5000 = 0.7366\), is practically identical: with a p-value far from \(\alpha\) the correction changes nothing.) The Monte Carlo standard error is \(\sqrt{0.7367 \cdot 0.2633/5000} \approx 0.006\) and the 95% Wilson interval for the p-value is approximately \([0.724, 0.749]\). R obtains \(p = 0.7355\) with Royston's analytical approximation (a normalizing transformation of \(W\)). Both are approximations of the same p-value and agree within simulation error (0.7355 lies inside the interval), but they should not be expected to match in every decimal place. You can reproduce it by entering the data in the calculator with \(B = 5000\) and seed 12345.

Conclusion

Since \(p \approx 0.74 \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 does not prove that they do: with \(n = 10\) the power of the test is limited and it would only detect marked departures. Before relying on parametric tools such as \(\bar{x}\text{-}R\) control charts, also check a Q-Q plot and prior knowledge of the process.

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 little power against moderate departures (somewhat heavier tails, mild skewness) and will often fail to reject \(H_0\) even when the data are not normal; only marked departures are detected consistently. 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.

Development and additional details

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 Monte Carlo p-value with its standard error and Monte Carlo confidence interval and, in the chart, the simulated distribution of W under normality together with the observed W and the critical value. To see how the data depart from normality, complement the result with a Q-Q plot.

Power: which departures it can detect

The power of the Shapiro-Wilk test depends on the sample size and on the type of departure from normality. With small samples it detects only very marked departures (strong skewness, clear outliers), so not rejecting normality with 10 or 15 observations says little. With very large samples the opposite happens: it detects tiny departures that have no practical importance for methods that assume normality. That is why the test should always be accompanied by a histogram or a Q-Q plot.

The link in the result opens the sample size calculator for Shapiro-Wilk with your \(\alpha\), where you can enter the skewness and kurtosis you want to be able to detect.

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.
  • Acklam, P. J. An algorithm for computing the inverse normal cumulative distribution function (algorithm published on the author's website). — Source of the coefficients of the rational approximation of \(\Phi^{-1}(p)\) used in the code, with a relative error below \(1.2 \times 10^{-9}\).