h = runstest(x) returns
a test decision for the null hypothesis that the values in the data
vector 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 h is 1 if
the test rejects the null hypothesis at the 5% significance level,
or 0 otherwise.

h = runstest(x,v) returns
a test decision based on the number of runs of consecutive values
above or below the specified reference value v.
Values exactly equal to v are discarded.

h = runstest(x,'ud') returns
a test decision based on the number of runs up or down. Too few runs
indicate a trend, while too many runs indicate an oscillation. Values
exactly equal to the preceding value are discarded.

h = runstest(___,Name,Value) 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.

[h,p,stats]
= runstest(___) also returns the p-value
of the test p, and a structure stats containing
additional data about the test.

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.

Reference value, specified as a scalar value. If you specify
a value for v, then runstest performs
the hypothesis test based on the number of runs of consecutive values
above or below v. runstest discards
values exactly equal to v.

Data Types: single | double

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments.
Name is the argument
name and Value is the corresponding
value. Name must appear
inside single quotes (' ').
You can specify several name and value pair
arguments in any order as Name1,Value1,...,NameN,ValueN.

Method used to compute p-value, specified
as the comma-separated pair consisting of 'Method' and
either 'exact' to use an exact algorithm, or 'approximate' to
use a normal approximation. The default is 'exact' for
runs above/below, and for runs up/down when the length of x is
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 'exact' method
is not available.

Hypothesis test result, returned as a logical value.

If h= 1,
then runstest rejects the null hypothesis at
the Alpha significance level.

If h= 0,
then runstest fails to reject the null hypothesis
at the Alpha significance level.

The result in runstest is based on the
number of runs of consecutive values above or below the mean of x.
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-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 p cast
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
argument.