Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

DZone's Guide to

Normality Versus Goodness-of-Fit Tests

· Big Data Zone ·
Free Resource

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

The open source HPCC Systems platform is a proven, easy to use solution for managing data at scale. Visit our Easy Guide to learn more about this completely free platform, test drive some code in the online Playground, and get started today.

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...

Managing data at scale doesn’t have to be hard. Find out how the completely free, open source HPCC Systems platform makes it easier to update, easier to program, easier to integrate data, and easier to manage clusters. Download and get started today.

Topics:

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.