I have a vector of data, and I want to test if it came from a normal distribution with mean zero and unknown variance. Do you know if there is matlab function or simple script for this? If you don't know anything matlab specific, then a name and reference for the specific test is fine and I will just implement it myself.

Also, if the specific test can return the confidence level instead of just answering yes-no at a given confidence level then that would be a benefit, but is not essential.

What I already know

If I want to test if my data is from a normal distribution with mean 0 and variance 1 then I can use the Kolmogorov-Smirnov test. If I want if my data is from a normal distribution with unknown mean AND variance then I can use the Lilliefors test or the Jarque-Bera test. However, I want a fixed mean (= 0) and unknown variance.

Naive approach

The naive approach is to take my data $D$, calculate the variance around zero $\sigma^2_0$ and then renormalize my data by this to get a dataset $D'$. Then I can perform the Kolmogorov-Smirnov test on this. However, it is not clear how one would justify this, especially since the KS tests specifically warns against testing against distributions with parameters estimated from the same data (renormalizing $D$ to $D'$ will be the same as testing against a normal distibution with mean zero and variance $\sigma^2_0$). Is this naive approach justified?

  • $\begingroup$ You need to specify an alternative hypothesis that you want to test this normal distribution against. If your hypothesis is false, what will you put in its place? Put another way, what would be the next thing that you would try if this (as yet unspecified) test came back saying "reject the hypothesis - this data is not normally distributed". Unless you can answer this question, what is the point of going to all the trouble of a formal test? $\endgroup$ Commented Aug 19, 2011 at 13:18
  • 2
    $\begingroup$ There is a body of theory that deals with the case where parameters of the distribution are estimated from the data before the KS test is applied. The reference is J. Durbin, Distribution theory for tests based on the sample distribution function, SIAM 1973. Alternatively, you could use the Anderson Darling test for normality which can deal with the case where $\mu$ is known and $\sigma$ is unknown. There is a Matlab implementation of the latter called AnDartest, available in File Exchange. $\endgroup$ Commented Aug 19, 2011 at 13:39
  • $\begingroup$ @probabilityislogic I don't care what distribution the data came from if it didn't come from the normal; thus I don't need any further tests. $\endgroup$ Commented Aug 19, 2011 at 14:05
  • 1
    $\begingroup$ @Artem - if you don't care about alternatives, you probably don't need a formal test - just look at a histogram of the observations that are supposed to be normal. Your naive method should also be sufficient too. The problem with ignoring alternatives is that you can't tell what power your test has (i.e. how often the test correctly rejects the null) $\endgroup$ Commented Aug 19, 2011 at 16:24
  • 1
    $\begingroup$ You can use Spiegelhalter's test, which is fairly powerful against fat-tailed alternatives and simple to code in Matlab: stackoverflow.com/questions/1882944/… Another advantage is that you need not specify the mean nor the standard deviation. It may not maintain the nominal rate at small sample sizes (less than 50 say), but should be OK otherwise. $\endgroup$
    – shabbychef
    Commented Aug 21, 2011 at 4:09

2 Answers 2


You can use Spiegelhalter's test (1983, not the 'omnibus test' from 1977):

function pval = spiegel_test(x)
% compute pvalue under null of x normally distributed;
% x should be a vector;
% D. J. Spiegelhalter, 'Diagnostic tests of distributional shape,' 
% Biometrika, 1983
xm = mean(x);
xs = std(x);
xz = (x - xm) ./ xs;
xz2 = xz.^2;
N = sum(xz2 .* log(xz2));
n = numel(x);
ts = (N - 0.73 * n) / (0.8969 * sqrt(n)); %under the null, ts ~ N(0,1)
pval = 1 - abs(erf(ts / sqrt(2)));    %2-sided test. if only Matlab had R's pnorm function ... 

I include code to test this under the null and under a few alternatives:

% under H0:
pvals = nan(10000,1);
for tt=1:numel(pvals);
    pvals(tt) = spiegel_test(randn(300,1));
mean(pvals < 0.05)

I get something like:

ans =


Under some alternatives:

%under Ha (using a Tukey g-distribution)
g = 0.4;
pvals = nan(10000,1);
for tt=1:numel(pvals);
    pvals(tt) = spiegel_test((exp(g * randn(300,1)) - 1)/g);
mean(pvals < 0.05)

%under Ha (using a Tukey h-distribution)
h = 0.1;
pvals = nan(10000,1);
for tt=1:numel(pvals);
    x = randn(300,1);
    pvals(tt) = spiegel_test(x .* exp(0.5 * h * x.^2));
mean(pvals < 0.05)

I get:

ans =


ans =


This test discards the knowledge that the mean must equal zero, so is perhaps less powerful than other tests. Spiegelhalter notes this test performs reasonably well for sample sizes greater than about 25, and is designed to test against symmetric alternatives (e.g. the Tukey h-distribution). It is less powerful against asymmetric alternatives.


See https://www.mathworks.com/matlabcentral/fileexchange/60147-normality-test-package.

This package automatically runs 10 goodness of fit tests:


enter image description here

Make sure X is a row vector.

This function provides ten Normality tests that are not altogether available under one compact routine as a compiled Matlab function. All tests are coded to provide p-values for those normality tests, and the this function gives the results as an output table. Included tests are: Kolmogorov-Smirnov test (Limiting form (KS-Lim), Stephens Method (KS-S), Marsaglia Method (KS-M), Lilliefors test (KS-L)), Anderson-Darling (AD) test, Cramer-Von Mises (CvM) test, Shapiro-Wilk (SW) test, Shapiro-Francia (SF) test, Jarque-Bera (JB) test, D’Agostino and Pearson (DAP) test. Tests are not meant for big data. Most tests does not work for data bigger than 900.

The related paper (DOI: 10.22237/jmasm/1509496200) can be found at https://digitalcommons.wayne.edu/jmasm/vol16/iss2/30/

  • $\begingroup$ Please elaborate on the links. The answer should explain what about the links answer the questions. Also links can become extinct over time & we want to preserve the questions & answers. $\endgroup$ Commented Jan 12, 2020 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.