Autocorrelation, or lagged correlation, is the correlation between a time series and its lagged series. It indicates the trend in the dataset. The autocorrelation formula can be defined as follows:
We can calculate the autocorrelation using the NumPy correlate() function to calculate the actual autocorrelation of sunspot cycles. We can also directly visualize the autocorrelation plot using the autocorrelation_plot() function. Let's compute the autocorrelation and visualize it:
# import needful libraries
import pandas as pd
import numpy as np
import statsmodels.api as sm
import matplotlib.pyplot as plt
# Read the dataset
data = sm.datasets.sunspots.load_pandas().data
# Calculate autocorrelation using numpy
dy = data.SUNACTIVITY - np.mean(data.SUNACTIVITY)
dy_square = np.sum(dy ** 2)
# Cross-correlation
sun_correlated = np.correlate(dy, dy, mode='full')/dy_square
result = sun_correlated[int(len(sun_correlated)/2)...