In statistical physics, an autocorrelation function is widely used to understand a system's nonequilibrium behaivor.
However, the definition of autocorrelation function used in physics (or statistics) is different from the definition of correlation function used in signal processing.
The physicist's definition is,
Unfortunately, the physicist's version is not implemented in the popular python packages such as numpy and scipy.
Naively using scipy.signal.correlate due to its cosmetic similarity, therefore, often leaves physicists puzzled, forcing them to implement their own algorithms.
Due to a large number of iterations, however, such algorithms are likely to be very slow when directly implemented in python, especially compared to the scipy.signal.correlate function.
Here, we showcase two correct autocorrelation function implementations that use fast algorithms of the scipy library.
The first method is using the fast Fourier transform (FFT) algorithm (scipy.fftpack). Note that the FFT method by default performs a cyclical convolution, but an autocorrelation is defined as an acyclical convolution.
So, blindly implementing the FFT method results in an incorrect autucorrelation function. To remedy this, we insert some zeros in the data, namely zero padding, so that the algorithm runs as if it's acyclical. You can see the difference in the figure.
The second method is to modify scipy.signal.correlate so that it complies with the physicist's definition of autocorrelation function. It involves dividing each entry by the number of data points that are correlated, i.e., we are calculating the average of correlation.