-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathExample7_3.py
46 lines (45 loc) · 1.31 KB
/
Example7_3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
# How to Form a Good Cointegrating (and Mean-Reverting) Pair of Stocks
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.stattools import coint
from statsmodels.api import OLS
from scipy.stats import pearsonr
df1 = pd.read_excel("miscFiles/KO.xls")
df2 = pd.read_excel("miscFiles/PEP.xls")
df = pd.merge(df1, df2, on="Date", suffixes=("_KO", "_PEP"))
df.set_index("Date", inplace=True)
df.sort_index(inplace=True)
"""
## Run cointegration (Engle-Granger) test
"""
coint_t, pvalue, crit_value = coint(df["Adj Close_KO"], df["Adj Close_PEP"])
(
coint_t,
pvalue,
crit_value,
) # abs(t-stat) < critical value at 90%. pvalue says probability of null hypothesis (of no cointegration) is 73%
"""
## Determine hedge ratio
"""
model = OLS(df["Adj Close_KO"], df["Adj Close_PEP"])
results = model.fit()
hedgeRatio = results.params
print(hedgeRatio)
"""
## spread = KO - hedgeRatio*PEP
"""
spread = df["Adj Close_KO"] - hedgeRatio[0] * df["Adj Close_PEP"]
plt.plot(spread) # Figure 7.2
plt.show()
"""
## Correlation test
"""
dailyret = df.loc[:, ("Adj Close_KO", "Adj Close_PEP")].pct_change()
dailyret.corr()
dailyret_clean = dailyret.dropna()
pearsonr(
dailyret_clean.iloc[:, 0], dailyret_clean.iloc[:, 1]
) # first output is correlation coefficient, second output is pvalue.