Run test for randomness
h = runstest(x)
h = runstest(x,v)
h = runstest(x,'ud')
h = runstest(___,Name,Value)
[h,p,stats] = runstest(___)
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.
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
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
Specify optional comma-separated pairs of
Name is the argument
Value is the corresponding
Name must appear
inside single quotes (
You can specify several name and value pair
arguments in any order as
'Alpha'— Significance level
0.05(default) | scalar value in the range (0,1)
Significance level of the hypothesis test, specified as the
comma-separated pair consisting of
a scalar value in the range (0,1).
'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
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 a logical value.
runstest rejects the null hypothesis at
Alpha significance level.
runstest fails to reject the null hypothesis
Alpha significance level.
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-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
n0 — The number of values
z — The test statistic