# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] Collapsed="false"
# # How to use
#
# [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/simaki/cointanalysis/blob/master/examples/howto/howto.ipynb)

# + Collapsed="false"
# # !pip install pandas pandas_datareader matplotlib numpy
# # !pip install cointanalysis

# + Collapsed="false"
import pandas as pd
from pandas_datareader.data import DataReader
import matplotlib.pyplot as plt
import numpy as np

from cointanalysis import CointAnalysis


# + Collapsed="false"
def fetch_etf(ticker):
    return DataReader(ticker, 'yahoo', '2012-01-01', '2018-12-31')['Adj Close']


# + [markdown] Collapsed="false"
# Let us see how the main class `CointAnalysis` works using two ETFs, [HYG](https://www.bloomberg.com/quote/HYG:US) and [BKLN](https://www.bloomberg.com/quote/BKLN:US), as examples.

# + Collapsed="false"
hyg = fetch_etf('HYG')
bkln = fetch_etf('BKLN')

# + [markdown] Collapsed="false"
# Since they are both connected with liabilities of low-rated companies, these prices behave quite similarly.

# + Collapsed="false"
plt.figure(figsize=(16, 4))

plt.title('HYG and BKLN')
hyg_norm = 100 * hyg / hyg[0]
bkln_norm = 100 * bkln / bkln[0]
plt.plot(hyg_norm, label='HYG (2012-01-01 = 100)', linewidth=1)
plt.plot(bkln_norm, label='BKLN (2012-01-01 = 100)', linewidth=1)

plt.legend()
plt.show()

# + [markdown] Collapsed="false"
# ## Cointegration test

# + [markdown] Collapsed="false"
# The method `test` carries out a cointegration test.
#
# The following code gives p-value for null-hypothesis that there is no cointegration.

# + Collapsed="false"
coint = CointAnalysis()

X = np.stack([hyg, bkln], axis=1)

coint.test(X).pvalue_

# + [markdown] Collapsed="false"
# The test have rejected the null-hypothesis by p-value of 0.55%, which implies cointegration.

# + [markdown] Collapsed="false"
# ## Get spread

# + [markdown] Collapsed="false"
# The method `fit` finds the cointegration equation.

# + Collapsed="false"
coint.fit(X)
print(f'coef: {coint.coef_}')
print(f'mean: {coint.mean_}')
print(f'std: {coint.std_}')

# + [markdown] Collapsed="false"
# This means that spread "-0.18 HYG + BKLN" has the mean 6.97 and standard deviation 0.15.

# + [markdown] Collapsed="false"
# In fact, the prices adjusted with these parameters clarifies the similarities of these ETFs:

# + Collapsed="false"
plt.figure(figsize=(16, 4))

hyg_adj = (-coint.coef_[0]) * hyg + coint.mean_

plt.title('HYG and BKLN')
plt.plot(hyg_adj, label=f'{-coint.coef_[0]:.2f} * HYG + {coint.mean_:.2f}', linewidth=1)
plt.plot(bkln, label='BKLN', linewidth=1)

plt.legend()
plt.show()

# + [markdown] Collapsed="false"
# 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).
#
# The method `fit_transform` carries out `fit` and `transform` at once.

# + Collapsed="false"
spread = coint.transform(X)

# + Collapsed="false"
series_spread = pd.Series(spread, index=hyg.index)

plt.figure(figsize=(16, 4))
plt.title('Spread between HYG and BKLN')
plt.plot(series_spread, linewidth=1)
plt.show()

StarcoderdataJupyterScriptsDedupFiltered