Normality Test

Calculate a normality test of every asset’s returns: whether they are normally distributed, by the Shapiro-Wilk, Anderson-Darling, Lilliefors or D’Agostino-Pearson test.

Each test has normality as its null hypothesis, so a low p-value means the returns are not normal, which matters for a Value at Risk or option price that assumes they are. The tests look at different things: the Shapiro-Wilk test at how well the ordered returns line up with normal quantiles (the most powerful in most comparisons), the Anderson-Darling test at the distance between the distributions with the most weight on the tails, the Lilliefors test at their largest distance and the D’Agostino-Pearson test at the skewness and kurtosis, like the Jarque-Bera test but also valid in small samples.

The formula for the Shapiro-Wilk test is as follows:

\[W = \frac{\sum_{i=1}^{n} a_{i} \cdot y_{i}^{2}}{\sum_{i=1}^{n} \left(r_{i} - \operatorname{mean}\left(r\right)\right)^{2}}\] \[z = \frac{\ln\left(1 - W\right) - \mu_{n}}{\sigma_{n}}\] \[\text{P Value} = 1 - \Phi(z)\]

with \(y_{i}\) the i-th smallest return and \(a_{i}\) the weights of Royston (1992) from the normal quantiles \(\text{Phi}_{\text{inv}} ((i - \tfrac{3}{8}) / (n + \tfrac{1}{4}))\). z is Royston’s normalising transformation from 12 observations, with Phi the standard normal distribution function.

The formula for the Anderson-Darling test is as follows:

\[z_{i} = \frac{y_{i} - \operatorname{mean}\left(r\right)}{s}\] \[A^{2} = -n - \frac{\sum_{i=1}^{n} (2 \cdot i - 1) \cdot \left(\ln\left(\Phi(z_{i})\right) + \ln\left(1 - \Phi(z_{n+1-i})\right)\right)}{n}\] \[A_{\text{adj}} = A^{2} \cdot \left(1 + \frac{0.75}{n} + \frac{2.25}{n^{2}}\right)\] \[\text{P Value} = \exp\left(1.2937 - 5.709 \cdot A_{\text{adj}} + 0.0186 \cdot A_{\text{adj}}^{2}\right) \quad \text{if } A_{\text{adj}} \geq 0.6\]

with \(y_{i}\) the i-th smallest return and s the standard deviation (ddof = 1). The P Value follows from \(A_{\text{adj}}\) by the formulas of D’Agostino and Stephens (1986), of which the last line is the one for \(A_{\text{adj}} \geq 0.6\).

The formula for the Lilliefors test is as follows:

\[D = \max\left(\max\left(\frac{i}{n} - \Phi(z_{i})\right), \max\left(\Phi(z_{i}) - \frac{i - 1}{n}\right)\right)\]

with \(z_{i}\) as above. The P-Value comes from Dallal and Wilkinson (1986) when that is at most 0.10, otherwise it is the share of 10,000 simulated normal samples of the same size with a larger D.

The formula for the D’Agostino-Pearson test is as follows:

\[K^{2} = Z_{1}^{2} + Z_{2}^{2}\] \[\sqrt{b_{1}} = \frac{m_{3}}{m_{2}^{3/2}}\] \[b_{2} = \frac{m_{4}}{m_{2}^{2}}\]

with \(K^{2}\) chi-squared with 2 degrees of freedom, \(Z_{1}\) the skewness \(\sqrt{b_{1}}\) transformed to a standard normal variable (D’Agostino, 1970), \(Z_{2}\) the kurtosis \(b_{2}\) transformed likewise (Anscombe and Glynn, 1983) and \(m_{k}\) the central moments.

Also known as: Shapiro-Wilk test, Anderson-Darling test, Lilliefors test, Kolmogorov-Smirnov normality test, D’Agostino K-squared test, omnibus normality test.

Example · ^GSPC, BTCUSD, TLT · 2021 to 2025

Install the Finance Toolkit with pip install financetoolkit -U, then call get_normality_test:

from financetoolkit import Toolkit

toolkit = Toolkit(
    ["^GSPC", "BTCUSD", "TLT"],
    api_key="FINANCIAL_MODELING_PREP_KEY",
    start_date="2021-01-01",
    end_date="2025-12-31",
)

toolkit.econometrics.get_normality_test(period="monthly", method="anderson_darling")

Or, with the Finance Toolkit MCP set up, ask an AI assistant such as Claude or ChatGPT:

Were the monthly returns of the S&P 500, Bitcoin and the iShares 20+ Year Treasury Bond ETF normally distributed from 2021 to 2025, by the Anderson-Darling test?

Which returns:

Using Your Own Data

get_normality_test runs on any data, not only prices: ratios, economic indicators, yield curves or your own dataset. Pass a DataFrame, a Series or a dictionary of them to Econometrics as data, with the dates in the rows or the columns, and set changes to how its changes are calculated: "percentage", "log", "difference" for rates and ratios, or None when the data are changes already. For example, for the monthly unemployment rates of the United States and Germany:

from financetoolkit import Economics, Econometrics

economics = Economics(start_date="2000-01-01")

unemployment = economics.get_unemployment_rate(
    countries=["United States", "Germany"], period="monthly"
)

econometrics = Econometrics(data=unemployment, changes="difference")

econometrics.get_normality_test()

Parameters

get_normality_test accepts the following parameters.

ParameterTypeDescriptionExample
periodstrThe data frequency for returns (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”."weekly"
within_periodboolWhether to calculate the test within the specified period or for the entire period. Thus whether to look at the test within a specific year (if period = ‘yearly’) or look at the entirety of all years. Defaults to False. Note that this requires intraday data when period = ‘daily’, since a single day only nests observations when intraday data was fetched.True
methodstrThe test. Options are: “shapiro_wilk” (Shapiro-Wilk), “anderson_darling” (Anderson-Darling), “lilliefors” (Lilliefors), “dagostino_pearson” (D’Agostino-Pearson). Defaults to “shapiro_wilk”."shapiro_wilk"
include_benchmarkboolWhether to include “Benchmark” among the assets tested. Defaults to False.True
roundingint | NoneThe number of decimals to round the results to. Defaults to None.2
show_columnsstr | list[str] | NoneThe columns to keep, a name or a list of names, matched on every column level, or the rows when no column has those names (such as the line items of a financial statement). Names that do not exist are logged, and when none exist the result is returned in full. Defaults to None, which keeps every column.None, which keeps every column

References

  • Paper Shapiro, S.S. and Wilk, M.B. (1965). “An Analysis of Variance Test for Normality (Complete Samples).” Biometrika, 52(3/4), 591-611.
  • Paper Royston, P. (1992). “Approximating the Shapiro-Wilk W-test for non-normality.” Statistics and Computing, 2(3), 117-119.
  • Paper Anderson, T.W. and Darling, D.A. (1954). “A Test of Goodness of Fit.” Journal of the American Statistical Association, 49(268), 765-769.
  • Paper D’Agostino, R.B. and Stephens, M.A. (1986). “Goodness-of-Fit Techniques.” Marcel Dekker, New York.
  • Paper Lilliefors, H.W. (1967). “On the Kolmogorov-Smirnov Test for Normality with Mean and Variance Unknown.” Journal of the American Statistical Association, 62(318), 399-402.
  • Paper Dallal, G.E. and Wilkinson, L. (1986). “An Analytic Approximation to the Distribution of Lilliefors’s Test Statistic for Normality.” The American Statistician, 40(4), 294-296.
  • Paper D’Agostino, R.B. and Pearson, E.S. (1973). “Tests for Departure from Normality. Empirical Results for the Distributions of b₂ and √b₁.” Biometrika, 60(3), 613-622.
  • Paper Anscombe, F.J. and Glynn, W.J. (1983). “Distribution of the Kurtosis Statistic b₂ for Normal Samples.” Biometrika, 70(1), 227-234.
  • Library Referenced against scipy’s shapiro
  • Library Referenced against statsmodels’ normal_ad
  • Library Referenced against scipy’s anderson
  • Library Referenced against statsmodels’ lilliefors
  • Library Referenced against scipy’s normaltest
Share