Book Image

Learning NumPy Array

By : Ivan Idris
Book Image

Learning NumPy Array

By: Ivan Idris

Overview of this book

Table of Contents (14 chapters)
Learning NumPy Array
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Demonstrating cointegration


Cointegration is similar to correlation, but it is considered by many to be a better metric to define the relatedness of two time-series. The usual way to explain the difference between cointegration and correlation is to take the example of a drunken man and his dog. Correlation tells you something about the direction in which they are going. Cointegration relates to their distance over time, which in this case is constrained by the leash of the dog. We will demonstrate cointegration using computer-generated time-series and real data. The data can be downloaded from Quandl in CSV format.

The Augmented Dickey Fuller (ADF) test can be used to measure the cointegration of time-series; proceed with the following steps to demonstrate cointegration:

  1. Define the following function to calculate the ADF statistic.

    def calc_adf(x, y):
        result = stat.OLS(x, y).fit()    
        return ts.adfuller(result.resid)
  2. Generate a sine value and calculate the cointegration of the value...