pastas.stats.tests.runs_test#

runs_test(series, cutoff='median')[source]#

Runs test for autocorrelation.

Parameters
  • series (pandas.Series) – Time series to test for autocorrelation.

  • cutoff (str or float, optional) – String set to “mean”, “median”, or a float value to use as the cutoff.

Returns

  • z_stat (float) – Runs test statistic.

  • pval (float) – p-value for the test statistic, based on a normal distribution.

Return type

Tuple[float, float]

Notes

Wald and Wolfowitz developed [wald_1943] developed a distribution free test ( i.e., no normal distribution is assumed) to test for autocorrelation. This test is also appropriate for non-equidistant time steps in the residuals time series. The Null-hypothesis is that the residual time series is a random sequence of positive and negative values. The alternative hypothesis is that they are non-random. The test statistic is computed as follows:

\[Z = \frac{R-\bar{R}}{\sigma_R}\]

where $R$ is the number of runs, \(\bar{R}\) the expected number of runs and \(\sigma_R\) the standard deviation of the number of runs. A run is defined as the number of sequences of exclusively postitive and negative values in the time series.

Considerations for this test:

  • Test is also applicable to time series with non-equidistant time steps.

References

wald_1943

Wald, A., & Wolfowitz, J. (1943). An exact test for randomness in the non-parametric case based on serial correlation. The Annals of Mathematical Statistics, 14(4), 378-388.

Examples

>>> res = pd.Series(index=pd.date_range(start=0, periods=1000, freq="D"),
>>>                 data=np.random.rand(1000))
>>> stat, pval = ps.stats.runs_test(res)
>>> if p > alpha:
>>>     print("Failed to reject the Null-hypothesis, no significant"
>>>           "autocorrelation. p =", p.round(2))
>>> else:
>>>     print("Reject the Null-hypothesis")