Run test for randomness
a test decision for the null hypothesis that the values in the data
h = runstest(
x come in random order, against the alternative
that they do not. The test is based on the number of runs of consecutive
values above or below the mean of
x. The result
the test rejects the null hypothesis at the 5% significance level,
h = runstest(___, returns
a test decision using additional options specified by one or more
name-value pair arguments. For example, you can change the significance
level of the test, specify the algorithm used to calculate the p-value,
or conduct a one-sided test.
Test Data for Randomness Using Sample Median
Generate a vector of 40 random numbers from a standard normal distribution.
rng default; % for reproducibility x = randn(40,1);
Test whether the values in
x appear in random order, using the sample median as the reference value.
[h,p] = runstest(x,median(x))
h = 0
p = 0.8762
The returned value of
h = 0 indicates that
runstest does not reject the null hypothesis that the values in
x are in random order at the default 5% significance level.
x — Data vector
vector of scalar values
Data vector, specified as a vector of scalar values.
x as missing values, and ignores them.
v — Reference value
x (default) | scalar value
Reference value, specified as a scalar value. If you specify
a value for
the hypothesis test based on the number of runs of consecutive values
above or below
values exactly equal to
comma-separated pairs of
the argument name and
Value is the corresponding value.
Name must appear inside quotes. You can specify several name and value
pair arguments in any order as
'Alpha',0.01,'Method','Approximate','Tail','right'specifies a right-tailed test with 1% significance level, which returns the approximate p-value.
Method — Method used to compute p-value
Method used to compute p-value, specified
as the comma-separated pair consisting of
'exact' to use an exact algorithm, or
use a normal approximation. The default is
runs above/below, and for runs up/down when the length of
less than or equal to 50. If
runstest tests for
runs up/down and the length of
x is greater than
50, then the default is
'approximate', and the
is not available.
Tail — Type of alternative hypothesis
'both' (default) |
Type of alternative hypothesis, specified as the comma-separated
pair consisting of
'Tail' and one of the following.
|Two-tailed test (sequence is not random)|
|Right-tailed test (like values separate for runs above/below, direction alternates for runs up/down)|
|Left-tailed test (like values cluster for runs above/below, values trend for runs up/down)|
h — Hypothesis test result
Hypothesis test result, returned as
= 1, then
runstestrejects the null hypothesis at the
= 0, then
runstestfails to reject the null hypothesis at the
The result in
runstest is based on the
number of runs of consecutive values above or below the mean of
Too few runs indicate a tendency for high and low values to cluster.
Too many runs indicate a tendency for high and low values to alternate.
runstest uses a test statistic which is
the difference between the number of runs and its mean, divided by
its standard deviation. The test statistic is approximately normally
distributed when the null hypothesis is true.
p — p-value
scalar value in the range [0,1]
p-value of the test, returned as a scalar
value in the range [0,1].
p is the probability
of observing a test statistic as extreme as, or more extreme than,
the observed value under the null hypothesis. Small values of
doubt on the validity of the null hypothesis.
p is computed from either the test statistic
or the exact distribution of the number of runs, depending on the
value specified for the
'Method' name-value pair
stats — Test data
Test data, returned as a structure with the following fields.
nruns— The number of runs
n1— The number of values above
n0— The number of values below
z— The test statistic
 Gibbons, Jean Dickinson, and Subhabrata Chakraborti. Nonparametric Statistical Inference. 5th ed. Boca Raton: CRC Press, 2011.