Autocorrelation is a fundamental concept in time series analysis. It measures the correlation between a time series and a lagged version of itself, which can provide insights into patterns and trends over time. In other words, it measures how similar a data point is to the previous data point(s) in the series. It is useful in analyzing time-series data because it can reveal patterns in the data that might not be apparent from a simple visual inspection. In this thread, different methods will be discussed on how to compute autocorrelations of a numeric time series. If you want to learn how to compute other metrics, you can check out the threads of:
1. Using "autocorr()" method:
autocorr()method of Pandas is used to compute the autocorrelation of a numeric time series. It calculates the correlation between a time series and a lagged version of itself.
- It takes an integer argument
lag, which specifies the number of lags to use in the autocorrelation calculation. The default value is 1, which computes the autocorrelation at lag 1.
2. Using "rolling()" and "corr()":
rolling()method creates a rolling or sliding window of the time series with a specified size (
window=2) and applies a function to the data within that window. The argument
window=2means that each window consists of two consecutive values from the time series.
- The sliding window approach divides the time series into overlapping subsets, or windows, of a fixed length and computes the autocorrelation between these windows.
corr()method is used to calculate the correlation between two series and here, it computes the correlation between the rolling window and a lagged version of the time series.
shift()method is responsible for shifting the index of a time series forward or backward by a specified number of periods called lag. (Default value is 1)
3. Using "shift()" method:
shift()method is used for shifting the index of a time series forward or backward by a specified number of periods called lag.
- In this example, the
corr()method is used to find the autocorrelation between the original series and the shifted series created with the help of the
4. Using "np.corrcoef()" method:
corrcoef()function computes the Pearson correlation coefficient between two arrays or as in this example, between two series.
- We computed the correlation between the original time series and a lagged version of itself where the lag is determined by slicing the series using