Cointegration is just like a correlation that can be viewed as a superior metric to define the relatedness of two time series. Cointegration is the stationary behavior of the linear combination of two time series. In this way, the trend of the following equation must be stationary:
y(t) - a x(t)
Consider a drunk man and his dog out on a walk. Correlation tells us whether they are going in the same direction. Cointegration tells us something about the distance over time between the man and his dog. We will show cointegration using randomly generated time-series and real data. The Augmented Dickey-Fuller (ADF) test tests for a unit root in a time series and can be used to determine the stationarity of time series.
Let's see an example to understand the cointegration of two time series.
You can check out the full code for this example at the following GitHub link:https://github.com/PacktPublishing/Python-Data-Analysis-Third-Edition/blob/master/Chapter08/Ch8.ipynb...