kstest — Perform a one-sample Kolmogorov-Smirnov test.

kstest(x) performs a one-sample Kolmogorov-Smirnov test of whether the sample in x could come from a standard normal distribution. [h,p,ksstat,cv] = kstest(...) also returns an approximate p-value, the KS statistic, and an approximate critical value.

Syntax

h = kstest(x)
h = kstest(x, Name, Value)
[h, p] = kstest(___)
[h, p, ksstat, cv] = kstest(___)

Inputs

NameTypeRequiredDefaultDescription
xAnyYesSample data.
nameValuePairsAnyVariadicAlpha, CDF, and Tail options.

Returns

NameTypeDescription
hLogicalArrayHypothesis test decision.
pNumericArrayP-value.
ksstatNumericArrayKolmogorov-Smirnov test statistic.
cvNumericArrayApproximate critical value.

Returned values from kstest depend on how many outputs the caller requests.

Errors

IdentifierWhenMessage
RunMat:kstest:InvalidArgumentInputs, CDF specification, tail, or significance level are malformed.kstest: invalid argument
RunMat:kstest:InternalRunMat cannot construct kstest outputs.kstest: internal error

How kstest works

  • kstest(x) requires numeric vector input, omits NaN sample values, and tests against the standard normal CDF.
  • kstest(x,"Alpha",alpha) changes the significance level. alpha must be a scalar in (0,1).
  • kstest(x,"Tail",tail) accepts "unequal"/"both", "larger", and "smaller".
  • kstest(x,"CDF",cdf) accepts a two-column numeric matrix whose first column contains sample points and whose second column contains CDF probabilities in [0,1]. Rows are sorted by sample point, duplicate sample points are collapsed to the largest probability at that point, and probabilities must be nondecreasing after normalization. Sample values must lie inside the table's x range. Values are linearly interpolated between table rows.
  • "CDF" also accepts "normcdf", "normal", "norm", or a @normcdf function handle for the standard normal CDF.
  • The p-values use standard large-sample approximations. For two-sided tests, cv is returned for Alpha in [0.01,0.20]; for one-sided tests, cv is returned for Alpha in [0.005,0.10]. Outside those ranges, cv is NaN, matching MATLAB's documented critical-value limitations.
  • Continuous probability distribution objects for "CDF" are not supported until RunMat's makedist/fitdist distribution object surface exists.

Examples

Test a centered sample against the standard normal CDF

h = kstest([-1 -0.25 0 0.25 1])

Request all outputs with a one-sided alternative

[h,p,ksstat,cv] = kstest(x, "Tail", "smaller")

Use an explicit CDF table

cdf = [0 0; 0.5 0.5; 1 1];
h = kstest(x, "CDF", cdf)

Using kstest with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how kstest changes the result.

Run a small kstest example, explain the result, then change one input and compare the output.

FAQ

Does kstest accept arbitrary CDF function handles?

Only @normcdf is recognized as a function handle. Use a two-column CDF table for custom reference distributions.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how kstest is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.