Giter Club home page Giter Club logo

cointanalysis's Introduction

CointAnalysis

python pypi CI downloads code style

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

Open In Colab

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 test 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.test(X)

coint.pvalue_
# 0.0055

The test has rejected the null-hypothesis by the 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.mean_
# 6.97
coint.std_
# 0.15

This means that spread "-0.18 HYG + BKLN" has a mean 6.97 and a standard deviation of 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

cointanalysis's People

Contributors

dependabot[bot] avatar imgbot[bot] avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.