stoffer_toloi(series, lags=15, nparam=0, freq='D', snap_to_equidistant_timestamps=False)[source]#

Adapted Ljung-Box test to deal with missing data [stoffer_1992].

  • series (pandas.Series) – Time series to compute the adapted Ljung-Box statistic for.

  • lags (int, optional) – the number of lags to compute the statistic for. Only lags for which a correlation is computed are used.

  • nparam (int, optional) – Number of parameters of the noisemodel.

  • freq (str, optional) – String with the frequency to resample the time series to.

  • snap_to_equidistant_timestamps (bool, optional) – if False (default), a sample is taken from series with equidistant timesteps using pandas’ reindex. Only values are kept that lie on those equidistant timestamps. If True, an equidistant time series is created taking as many values as possible from the original series which are then snapped to the nearest equidistant timestamp.


  • qm (float) – Adapted Ljung-Box test statistic.

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

Return type:

Tuple[float, float]


Stoffer and Toloi [stoffer_1992] extended the Ljung-Box test to also work with missing data. The test statistic is computed as follows:

\[Q_k = n^2 \sum_{k=1}^{h} \frac{\hat{\rho}_k^2}{n-k}\]

where \(\hat{\rho}_k\) is the autocorrelation for lag $k$. When the residual time series have non-equidistant time steps it is recommended to use this test over the original Ljung-Box test.

The Stoffer-Toloi test is strictly an adapted version of the Ljung-Box test to deal with missing data in a time series and not a time series with non-equidistant time steps. This means that the time series is updated to an equidistant time series by filling nan-values.

Considerations for this test:

  • Test is also applicable to irregular time series.

  • The time step has to be chosen (e.g., Days). This should not be smaller than the smallest time step or the test will most likely fail to reject $H_0$ anyway.


[stoffer_1992] (1,2)

Stoffer, D. S., & Toloi, C. M. (1992). A note on the Ljung—Box—Pierce stoffer_toloi statistic with missing data. Statistics & probability letters, 13(5), 391-396.


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