Skip to main content

Python package for cointegration analysis.

Project description

CointAnalysis

version Build Status LICENSE

Python library for cointegration analysis.

hyg-bkln-adjust

Features

  • Carry out cointegration test
  • Evaluate spread between cointegrated time-series
  • Generate cointegrated time-series artificially
  • Based on scikit-learn API

Installation

$ pip install cointanalysis

What is cointegration?

See Hamilton's book.

How to use

Let us see how the main class CointAnalysis works using two ETFs, HYG and BKLN, as examples. Since they are both connected with liabilities of low-rated companies, these prices behave quite similarly.

hyg-bkln

Cointegration test

The method score carries out a cointegration test. The following code gives p-value for null-hypothesis that there is no cointegration.

from cointanalysis import CointAnalysis

hyg = ...   # Fetch historical price of high-yield bond ETF
bkln = ...  # Fetch historical price of bank loan ETF
X = np.array([hyg, bkln]).T

coint = CointAnalysis()
coint.score(X)
# 0.0055

The test have rejected the null-hypothesis by p-value of 0.55%, which implies cointegration.

Get spread

The method fit finds the cointegration equation.

coint = CointAnalysis().fit(X)

coint.coef_
# np.array([-0.18  1.])
coint.const_
# 7.00
coint.std_
# 0.15

This means that spread "-0.18 HYG + BKLN" has the mean 7.00 and standard deviation 0.15.

In fact, the prices adjusted with these parameters clarifies the similarities of these ETFs:

hyg-bkln-adjust

The time-series of spread is obtained by applying the method transform subsequently. The mean and the standard deviation are automatically adjusted (unless you pass parameters asking not to).

spread = coint.transform(X)
# returns (-0.18 * hyg + 1. * bkln - 7.00) / 0.15

spread = coint.transform(X, adjust_mean=False, adjust_std=False)
# returns -0.18 * hyg + 1. * bkln

The method fit_transform carries out fit and transform at once.

spread = coint.fit_transform(X)

The result looks like this:

hyg-bkln-spread

Acknowledgements

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for cointanalysis, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size cointanalysis-0.1.1-py3-none-any.whl (8.9 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size cointanalysis-0.1.1.tar.gz (8.7 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page