Main Content

kstest

One-sample Kolmogorov-Smirnov test

Description

example

h= kstest(x)returns a test decision for the null hypothesis that the data in vectorxcomes from a standard normal distribution, against the alternative that it does not come from such a distribution, using theone-sample Kolmogorov-Smirnov test. The resulthis1if the test rejects the null hypothesis at the 5% significance level, or0otherwise.

example

h= kstest(x,Name,Value)returns a test decision for the one-sample Kolmogorov-Smirnov test with additional options specified by one or more name-value pair arguments. For example, you can test for a distribution other than standard normal, change the significance level, or conduct a one-sided test.

example

[h,p] = kstest(___)also returns thep-valuepof the hypothesis test, using any of the input arguments from the previous syntaxes.

example

[h,p,ksstat,cv] = kstest(___)also returns the value of the test statisticksstatand the approximate critical valuecvof the test.

Examples

collapse all

Perform the one-sample Kolmogorov-Smirnov test by usingkstest. Confirm the test decision by visually comparing the empirical cumulative distribution function (cdf) to the standard normal cdf.

Load theexamgradesdata set. Create a vector containing the first column of the exam grade data.

loadexamgradestest1 = grades(:,1);

Test the null hypothesis that the data comes from a normal distribution with a mean of 75 and a standard deviation of 10. Use these parameters to center and scale each element of the data vector, becausekstesttests for a standard normal distribution by default.

x = (test1-75)/10; h = kstest(x)
h =logical0

The returned value ofh = 0indicates thatkstestfails to reject the null hypothesis at the default 5% significance level.

Plot the empirical cdf and the standard normal cdf for a visual comparison.

cdfplot(x) holdonx_values = linspace(min(x),max(x)); plot(x_values,normcdf(x_values,0,1),'r-') legend('Empirical CDF','Standard Normal CDF','Location','best')

Figure contains an axes object. The axes object with title Empirical CDF contains 2 objects of type line. These objects represent Empirical CDF, Standard Normal CDF.

图中显示empiri之间的相似性cal cdf of the centered and scaled data vector and the cdf of the standard normal distribution.

Load the sample data. Create a vector containing the first column of the students’ exam grades data.

loadexamgrades; x = grades(:,1);

Specify the hypothesized distribution as a two-column matrix. Column 1 contains the data vectorx. Column 2 contains cdf values evaluated at each value inxfor a hypothesized Student’s t distribution with a location parameter of 75, a scale parameter of 10, and one degree of freedom.

test_cdf = [x,cdf('tlocationscale',x,75,10,1)];

Test if the data are from the hypothesized distribution.

h = kstest(x,'CDF',test_cdf)
h =logical1

The returned value ofh = 1indicates thatkstestrejects the null hypothesis at the default 5% significance level.

Load the sample data. Create a vector containing the first column of the students’ exam grades data.

loadexamgrades; x = grades(:,1);

Create a probability distribution object to test if the data comes from a Student’s t distribution with a location parameter of 75, a scale parameter of 10, and one degree of freedom.

test_cdf = makedist('tlocationscale','mu',75,'sigma',10,'nu',1);

Test the null hypothesis that the data comes from the hypothesized distribution.

h = kstest(x,'CDF',test_cdf)
h =logical1

The returned value ofh = 1indicates thatkstestrejects the null hypothesis at the default 5% significance level.

Load the sample data. Create a vector containing the first column of the students’ exam grades.

loadexamgrades; x = grades(:,1);

Create a probability distribution object to test if the data comes from a Student’s t distribution with a location parameter of 75, a scale parameter of 10, and one degree of freedom.

test_cdf = makedist('tlocationscale','mu',75,'sigma',10,'nu',1);

Test the null hypothesis that data comes from the hypothesized distribution at the 1% significance level.

[h,p] = kstest(x,'CDF',test_cdf,'Alpha',0.01)
h =logical1
p = 0.0021

The returned value ofh = 1indicates thatkstest拒绝零假设1%的意义level.

Load the sample data. Create a vector containing the third column of the stock return data matrix.

loadstockreturns; x = stocks(:,3);

Test the null hypothesis that the data comes from a standard normal distribution, against the alternative hypothesis that the population cdf of the data is larger than the standard normal cdf.

[h,p,k,c] = kstest(x,'Tail','larger')
h =logical1
p = 5.0854e-05
k = 0.2197
c = 0.1207

The returned value ofh = 1indicates thatkstestrejects the null hypothesis in favor of the alternative hypothesis at the default 5% significance level.

Plot the empirical cdf and the standard normal cdf for a visual comparison.

[f,x_values] = ecdf(x); J = plot(x_values,f); holdon; K = plot(x_values,normcdf(x_values),'r--'); set(J,'LineWidth',2); set(K,'LineWidth',2); legend([J K],'Empirical CDF','Standard Normal CDF','Location','SE');

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Empirical CDF, Standard Normal CDF.

The plot shows the difference between the empirical cdf of the data vectorxand the cdf of the standard normal distribution.

Input Arguments

collapse all

Sample data, specified as a vector.

Data Types:single|double

Name-Value Arguments

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, whereNameis the argument name andValueis the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and encloseNamein quotes.

Example:'Tail','larger','Alpha',0.01specifies a test using the alternative hypothesis that the cdf of the population from which the sample data is drawn is greater than the cdf of the hypothesized distribution, conducted at the 1% significance level.

Significance level of the hypothesis test, specified as the comma-separated pair consisting of'Alpha'and a scalar value in the range (0,1).

Example:'Alpha',0.01

Data Types:single|double

cdf of hypothesized continuous distribution, specified the comma-separated pair consisting of'CDF'and either a two-column matrix or a continuous probability distribution object. WhenCDFis a matrix, column 1 contains a set of possiblexvalues, and column 2 contains the corresponding hypothesized cumulative distribution function valuesG(x). The calculation is most efficient ifCDF这样第一列包含指定值s in the data vectorx. If there are values inxnot found in column 1 ofCDF,kstestapproximatesG(x) by interpolation. All values inxmust lie in the interval between the smallest and largest values in the first column ofCDF. By default,kstesttests for a standard normal distribution.

Theone-sample Kolmogorov-Smirnov testis only valid for continuous cumulative distribution functions, and requiresCDFto be predetermined. The result is not accurate ifCDFis estimated from the data. To testxagainst the normal, lognormal, extreme value, Weibull, or exponential distribution without specifying distribution parameters, uselillietestinstead.

Data Types:single|double

Type of alternative hypothesis to evaluate, specified as the comma-separated pair consisting of'Tail'and one of the following.

'unequal' Test the alternative hypothesis that the cdf of the population from whichxis drawn is not equal to the cdf of the hypothesized distribution.
'larger' Test the alternative hypothesis that the cdf of the population from whichxis drawn is greater than the cdf of the hypothesized distribution.
'smaller' Test the alternative hypothesis that the cdf of the population from whichxis drawn is less than the cdf of the hypothesized distribution.

If the values in the data vectorxtend to be larger than expected from the hypothesized distribution, the empirical distribution function ofxtends to be smaller, and vice versa.

Example:'Tail','larger'

Output Arguments

collapse all

Hypothesis test result, returned as a logical value.

  • Ifh= 1, this indicates the rejection of the null hypothesis at theAlphasignificance level.

  • Ifh= 0, this indicates a failure to reject the null hypothesis at theAlphasignificance level.

p-value of the test, returned as a scalar value in the range [0,1].pis the probability of observing a test statistic as extreme as, or more extreme than, the observed value under the null hypothesis. Small values ofpcast doubt on the validity of the null hypothesis.

Test statistic of the hypothesis test, returned as a nonnegative scalar value.

Critical value, returned as a nonnegative scalar value.

More About

collapse all

One-Sample Kolmogorov-Smirnov Test

The one-sample Kolmogorov-Smirnov test is a nonparametric test of the null hypothesis that the population cdf of the data is equal to the hypothesized cdf.

The two-sided test for “unequal” cdf functions tests the null hypothesis against the alternative that the population cdf of the data is not equal to the hypothesized cdf. The test statistic is the maximum absolute difference between the empirical cdf calculated fromxand the hypothesized cdf:

D * = max x ( | F ^ ( x ) G ( x ) | ) ,

where F ^ ( x ) is the empirical cdf and G ( x ) is the cdf of the hypothesized distribution.

The one-sided test for a “larger” cdf function tests the null hypothesis against the alternative that the population cdf of the data is greater than the hypothesized cdf. The test statistic is the maximum amount by which the empirical cdf calculated fromxexceeds the hypothesized cdf:

D * = max x ( F ^ ( x ) G ( x ) ) .

The one-sided test for a “smaller” cdf function tests the null hypothesis against the alternative that the population cdf of the data is less than the hypothesized cdf. The test statistic is the maximum amount by which the hypothesized cdf exceeds the empirical cdf calculated fromx:

D * = max x ( G ( x ) F ^ ( x ) ) .

kstestcomputes the critical valuecvusing an approximate formula or by interpolation in a table. The formula and table cover the range0.01alpha0.2for two-sided tests and0.005alpha0.1for one-sided tests.cvis returned asNaNifalphais outside this range.

Algorithms

kstestdecides to reject the null hypothesis by comparing thep-valuepwith the significance levelAlpha, not by comparing the test statisticksstatwith the critical valuecv. Sincecvis approximate, comparingksstatwithcvoccasionally leads to a different conclusion than comparingpwithAlpha.

References

[1] Massey, F. J. “The Kolmogorov-Smirnov Test for Goodness of Fit.”Journal of the American Statistical Association. Vol. 46, No. 253, 1951, pp. 68–78.

[2] Miller, L. H. “Table of Percentage Points of Kolmogorov Statistics.”Journal of the American Statistical Association. Vol. 51, No. 273, 1956, pp. 111–121.

[3] Marsaglia, G., W. Tsang, and J. Wang. “Evaluating Kolmogorov’s Distribution.”Journal of Statistical Software. Vol. 8, Issue 18, 2003.

Version History

Introduced before R2006a