Are Bitcoin and Ethereum Cointegrated?
Cointegration involves two steps: regressing one time series on the other to get the cointegration vector, and then perform an ADF test on the residuals of the regression. In the last example, there was no need to perform the first step since we implicitly assumed the cointegration vector was \(\small (1,-1)\). In other words, we took the difference between the two series (after doing a units conversion). Here, you will do both steps.
You will regress the value of one cryptocurrency, bitcoin (BTC), on another cryptocurrency, ethereum (ETH). If we call the regression coefficient \(\small b\), then the cointegration vector is simply \(\small (1,-b)\). Then perform the ADF test on BTC \(\small - b \) ETH. Bitcoin and Ethereum prices are pre-loaded in DataFrames BTC
and ETH
.
This exercise is part of the course
Time Series Analysis in Python
Exercise instructions
- Import the statsmodels module for regression and the
adfuller
function - Add a constant to the
ETH
DataFrame usingsm.add_constant()
- Regress
BTC
onETH
usingsm.OLS(y,x).fit()
, where y is the dependent variable and x is the independent variable, and save the results inresult
.- The intercept is in
result.params[0]
and the slope inresult.params[1]
- The intercept is in
- Run ADF test on BTC \(\small - b \) ETH
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import the statsmodels module for regression and the adfuller function
import statsmodels.api as sm
from statsmodels.tsa.stattools import adfuller
# Regress BTC on ETH
ETH = sm.___(ETH)
result = sm.OLS(___,___).fit()
# Compute ADF
b = result.params[1]
adf_stats = adfuller(___['Price'] - b*___['Price'])
print("The p-value for the ADF test is ", adf_stats[1])