Skip to content

Instantly share code, notes, and snippets.

@edgarrmondragon
Created April 29, 2020 21:48
Show Gist options
  • Save edgarrmondragon/a67c94241cdfb2fd4688406a31dd90e5 to your computer and use it in GitHub Desktop.
Save edgarrmondragon/a67c94241cdfb2fd4688406a31dd90e5 to your computer and use it in GitHub Desktop.
[Bitcon & Ethereum Cointegration] #timeseries #python #dickey-fuller
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Are Bitcoin and Ethereum Cointegrated?\n",
"\n",
"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 $(1,−1)$. In other words, we took the difference between the two series (after doing a units conversion). Here, you will do both steps.\n",
"\n",
"You will regress the value of one cryptocurrency, bitcoin (BTC), on another cryptocurrency, ethereum (ETH). If we call the regression coefficient $b$, then the cointegration vector is simply $(1,−b)$. Then perform the ADF test on $\\text{BTC} −b\\text{ETH}$. Bitcoin and Ethereum prices are pre-loaded in DataFrames `BTC` and `ETH`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"\n",
"BTC = pd.read_csv(\"https://s3.amazonaws.com/assets.datacamp.com/production/course_4267/datasets/BTC_GDAX.csv\", index_col=\"Date\", thousands=\",\")\n",
"ETH = pd.read_csv(\"https://s3.amazonaws.com/assets.datacamp.com/production/course_4267/datasets/ETH_GDAX.csv\", index_col=\"Date\", thousands=\",\")\n",
"\n",
"BTC.index = pd.to_datetime(BTC.index)\n",
"ETH.index = pd.to_datetime(ETH.index)\n",
"\n",
"BTC=BTC[::-1]\n",
"ETH=ETH[::-1]\n",
"\n",
"# Import the statsmodels module for regression and the adfuller function\n",
"import statsmodels.api as sm\n",
"from statsmodels.tsa.stattools import adfuller\n",
"\n",
"# Regress BTC on ETH\n",
"ETH = sm.add_constant(ETH)\n",
"result = sm.OLS(BTC, ETH).fit()\n",
"\n",
"# Compute ADF\n",
"b = result.params[1]\n",
"adf_stats = adfuller(BTC[\"Price\"] - b * ETH[\"Price\"])\n",
"print('The p-value for the ADF test is ', adf_stats[1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"fig, ax = plt.subplots(1, 1, figsize=(10, 6))\n",
"BTC['Price'].plot(ax=ax)\n",
"ETH['Price'].plot(ax=ax)\n",
"ax.set_xlabel('Date')\n",
"ax.set_ylabel('Price')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment