acf_np

mdtools.statistics.acf_np(x, center=True, unbiased=False)[source]

Calculate the autocorrelation function of a 1-dimensional array.

Parameters:
  • x (array_like) – 1-dimensional input array.

  • center (bool, optional) – If True, center the input array around its mean, i.e. subtract the sample mean \(\bar{X}\) when calculating the variance and covariance.

  • unbiased (bool, optional) – If True, the covariance \(\text{Cov}[X_{t_0}, X_{t_0 + \tau}]\) is normed by the actual number of sample points \(t_0\) (which depends on the lag time \(\tau\)). If False, the covariance is for all lag times normed by the number of sampling points at \(t_0 = 0\) (which is equal to the length of the input array x). Note that setting unbiased to True might result in values of the ACF that lie outside the interval [-1, 1], especially for lag times larger than len(x) // 2 if center is True.

Returns:

ac (numpy.ndarray) – Array of the same shape as x containing the autocorrelation function of x.

Raises:

ValueError : – If the first element of ac is not unity or one or more elements of ac fall outside the interval [-1, 1].

See also

mdtools.statistics.acf()

Different implementation of the ACF using using a for loop

mdtools.statistics.acf_se()

Calculate the standard errors of an autocorrelation function

mdtools.statistics.acf_confint()

Calculate the confidence intervals of an autocorrelation function

Notes

See mdtools.statistics.acf() for the mathematical details of computing the autocorrelation function (ACF).

This function uses numpy.correlate() to calculate the autocorrelation function whereas mdtools.statistics.acf() uses an explicit for loop. Therefore, mdtools.statistics.acf() can handle arbitrarily shaped input arrays and offers more options.

Examples

>>> a = np.random.normal(loc=3, scale=7, size=11)
>>> np.allclose(mdt.stats.acf_np(a), mdt.stats.acf(a), rtol=0)
True
>>> np.allclose(
...     mdt.stats.acf_np(a, center=False),
...     mdt.stats.acf(a, center=False),
...     rtol=0
... )
True