Big Data/Analytics Zone is brought to you in partnership with:

Arthur Charpentier, ENSAE, PhD in Mathematics (KU Leuven), Fellow of the French Institute of Actuaries, professor at UQàM in Actuarial Science. Former professor-assistant at ENSAE Paritech, associate professor at Ecole Polytechnique and professor assistant in economics at Université de Rennes 1. Arthur is a DZone MVB and is not an employee of DZone and has posted 159 posts at DZone. You can read more from them at their website. View Full User Profile

# Normality Versus Goodness-of-Fit Tests

11.07.2012
| 3012 views |

In many cases, in statistical modeling, we would like to test whether the underlying distribution from an i.i.d. sample lies in a given (parametric) family, e.g. the Gaussian family

$H_0:F(\cdot)\in\mathcal F$where$\mathcal F=\{\Phi(\cdot;\mu,\sigma^2);\mu\in\mathbb{R},\sigma^2\in\mathbb{R}_+\}$

Consider a sample

> library(nortest)
> X=rnorm(n)

Then a natural idea is to use goodness of fit tests (natural is not necessarily correct, we'll get back on that later on), i.e.

$H_0:F(\cdot)=\Phi(\cdot;\mu,\sigma^2)$

for some $\mu$ and $\sigma^2$. But since those two parameters are unknown, it is not uncommon to see people substituting estimators to those two unknown parameters, i.e.

$H_0:F(\cdot)=\Phi(\cdot;\widehat\mu_n,\widehat\sigma^2_n)$

Using Kolmogorov-Smirnov test, we get

> pn=function(x){pnorm(x,mean(X),sd(X))};
> P.KS.Norm.estimated.param=
+ ks.test(X,pn)$p.value But since we choose parameters based on the sample we use to run a goodness of fit test, we should expect to have troubles, somewhere. So another natural idea is to split the sample: the first half will be used to estimate the parameters, and then, we use the second half to run a goodness of fit test (e.g. using Kolmogorov-Smirnov test) > pn=function(x){pnorm(x,mean(X[1:(n/2)]), + sd(X[1:(n/2)]))} > P.KS.Norm.out.of.sample= + ks.test(X[(n/2+1):n],pn)$p.value>.05)

As a benchmark, we can use Lilliefors test, where the distribution of Kolmogorov-Smirnov statistics is corrected to take into account the fact that we use estimators of parameters

> P.Lilliefors.Norm=
+ lillie.test(X)\$p.value 

Here, let us consider i.i.d. samples of size 200 (100,000 samples were generated here). The distribution of the $p$-value of the test is shown below,

In red, the Lilliefors test, where we see that the correction works well: the $p$-value is uniformly distributed on the unit inteval. There is 95% chance to accept the normality assumption if we accept it when the $p$-value exceeds 5%. On the other hand,

• with Kolmogorv-Smirnov test, on the overall sample, we always accept the normal assumption (almost), with a lot of extremely large $p$-values
• with Kolmogorv-Smirnov test, with out of sample estimation, we actually observe the opposite: in a lot of simulation, the $p$-value is lower then 5% (with the sample was from a $\mathcal N(0,1)$ sample).

The cumulative distribution function of the $p$-value is

I.e., the proportion of samples with $p$-value exceeding 5% is 95% for Lilliefors test (as expected), while it is 85% for the out-of-sample estimator, and 99.99% for Kolmogorov-Smirnov with estimated parameters,

> mean(P.KS.Norm.out.of.sample>.05)
[1] 0.85563
> mean(P.KS.Norm.estimated.param>.05)
[1] 0.99984
> mean(P.Lilliefors.Norm>.05)
[1] 0.9489

So using Kolmogorov-Smirnov with estimated parameters is not good, since we might accept $H_0$ too often. On the other hand, if we use this technique with two samples (one to estimate parameter, one to run goodness of fit tests), it looks much better ! even if we reject $H_0$ too often. For one test, the rate of first type error is rather large, but for the other, it is the rate of second type error...

Published at DZone with permission of Arthur Charpentier, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

"Starting from scratch" is seductive but disease ridden