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

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/JeJe620/NYU-SUMMER-2021/blob/main/Regression_Discontinuity_%26_DiD_sections.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="baoVO4VazP9Q"
# # Data Preparation

# + [markdown] id="HMG1iPdFzTHr"
# Story Background: https://www.gemini.com/cryptopedia/the-dao-hack-makerdao
#
# Event: July 20, 2016, at block 192,000

# + [markdown] id="M1AUq193zmQg"
# [Data Metrics](https://github.com/coinmetrics-io/data/blob/master/csv/metrics.csv)

# + id="7tHY6FVuxaSN"
import pandas as pd
import numpy as np

# + [markdown] id="qt5hVJ3Mzz6g"
# ###import the data for Ethereum

# + colab={"base_uri": "https://localhost:8080/", "height": 338} id="eznPu_z5zvBc" outputId="854cba68-e44b-4578-e49d-9b115a26900c"
df_eth=pd.read_csv("https://raw.githubusercontent.com/coinmetrics-io/data/master/csv/eth.csv")
df_eth.head()

# + colab={"base_uri": "https://localhost:8080/"} id="JwqB5hAk7row" outputId="d84daf48-92db-44b0-ee91-411dd3753bc6"
df_eth['time']=pd.to_datetime(df_eth['time'])
df_eth.dtypes

# + colab={"base_uri": "https://localhost:8080/", "height": 338} id="s5c7PnODz46B" outputId="9fc63065-2ca5-4b8b-9332-125d43f912dc"
df_eth['Type'] = 'eth'
df_eth.head()

# + [markdown] id="P7GMGNoSz8FM"
# ### Import the Data for Ethereum Classic

# + colab={"base_uri": "https://localhost:8080/", "height": 338} id="Bp0Y-sPez91E" outputId="f6a73b83-bda6-4657-825d-962af779c1ea"
df_etc=pd.read_csv("https://raw.githubusercontent.com/coinmetrics-io/data/master/csv/etc.csv")
df_etc.head()

# + colab={"base_uri": "https://localhost:8080/"} id="qYVvsug1GWHU" outputId="ba869fa7-1f4a-41c5-f267-c71704f2256b"
### change to dateime
df_etc['time']=pd.to_datetime(df_etc['time'])
df_etc.dtypes

# + colab={"base_uri": "https://localhost:8080/", "height": 338} id="8aaYGs2sGhk6" outputId="7a7f5f17-c072-44da-fba6-9b887a3ebe80"
df_etc['Type'] = 'etc'
df_etc.head()

# + [markdown] id="qgHxty-pxejg"
# # Regression Discontinguity

# + [markdown] id="3_Q5kfEJ7YWl"
# https://youtu.be/TfKwgGT2fSM

# + [markdown] id="w6zkeOJZ7ZY2"
# ### create the identifier varaible for Dao Hack

# + id="1-x1Zdvs8DRm"
from datetime import date

# + colab={"base_uri": "https://localhost:8080/", "height": 338} id="f4B5JEcG7PWC" outputId="f33b4724-1114-4e42-cf11-7bd1387757a1"
df_eth ['After_DaoHack'] =df_eth['time'].apply(lambda x: 1 if x>= date(2016,7,20) else 0)
df_eth[df_eth.time.dt.date>=date(2016,7,18)].head()

# + colab={"base_uri": "https://localhost:8080/", "height": 338} id="vh_XxOpoD7Zz" outputId="74bc3a17-6d33-4f55-e20b-57c5961f2eb7"
df_eth['days']=df_eth.index
df_eth.head()

# + [markdown] id="J58BVTE1EBZM"
# ### normalize the data

# + colab={"base_uri": "https://localhost:8080/"} id="IIKWCP4mEgd8" outputId="ac07fd67-9dc0-4f8c-d011-420b7188554d"
df_eth['days'][df_eth.time.dt.date==date(2016,7,20)]

# + colab={"base_uri": "https://localhost:8080/", "height": 338} id="W4EBX56wEEZI" outputId="297bb1a4-9eb8-4421-e80b-7e260d0894f2"
df_eth['days']=df_eth['days']-356
df_eth[df_eth.time.dt.date>=date(2016,7,18)].head()

# + [markdown] id="9EnjgUYS8hyE"
# ## create the regression function

# + [markdown] id="I75Dz7Lk8mNU"
# https://www.statsmodels.org/stable/index.html

# + id="stLbYqt18hg7" colab={"base_uri": "https://localhost:8080/"} outputId="1f114eac-9699-4afa-95b1-25bc210e1d79"
 import statsmodels.api as sm
 import statsmodels.formula.api as smf

# + id="rmA9-dnY8qgU"
results = smf.ols('VtyDayRet30d ~ 1+ After_DaoHack +days+TxTfrValAdjUSD ', data=df_eth).fit()

# + colab={"base_uri": "https://localhost:8080/"} id="L_j8WQIm9F2w" outputId="11a02e07-0cd2-429a-abc9-24905442ad44"
print(results.summary())

# + id="AOCpbBdb9UmW"
results = smf.ols('VtyDayRet30d ~ 1+ After_DaoHack + days +After_DaoHack*days +TxTfrValAdjUSD', data=df_eth).fit()

# + colab={"base_uri": "https://localhost:8080/"} id="pn4MDJj19ZHB" outputId="6749416d-0de2-47b6-dc67-e6c9d5be2e7d"
print(results.summary())

# + [markdown] id="ETLFygpxxjt5"
# # Difference in Difference

# + [markdown] id="4qdGg7YKxmuk"
# https://towardsdatascience.com/causal-inference-101-difference-in-differences-1fbbb0f55e85

# + [markdown] id="WL9RDDGyJhVq"
# ### prepare the datasets

# + id="5uOwr1ndITdh"
Columns =["time","VtyDayRet30d","TxTfrValAdjUSD","FeeMedUSD","RevUSD","GasLmtTxMean","Type"]
df_eth =df_eth[Columns]
df_etc =df_etc[Columns]

# + colab={"base_uri": "https://localhost:8080/"} id="OWbaXLXKIX0b" outputId="d3f257d3-1554-459f-985d-a0084740197d"
df_etc =df_etc.dropna()
df_etc.head()

# + id="vkHvoa1wIAhh"
df_eth =df_eth[df_eth.time.dt.date>=date(2016,7,25)]
df_etc =df_etc[df_etc.time.dt.date>=date(2016,7,25)]

# + id="fd60jQVmI20J"
df_eth=df_eth.reset_index()
df_etc=df_etc.reset_index()

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="t8401h32IsgK" outputId="06b038b8-00d2-4821-fc3a-782c6e9e0bb3"
df_eth["days"]=df_eth.index
df_eth.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="FZ-uORuVI8pS" outputId="c1e2e31c-ab9a-4b3a-c5ff-970ae004f49f"
df_etc["days"]=df_etc.index
df_etc.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="7n8h9KRFG1CF" outputId="be6046eb-4e9d-4056-a653-13c8d283f8a7"
df = pd.concat([df_eth, df_etc], axis=0)
df.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="UfrlCbnhHXRb" outputId="7bf6d6f7-546e-4f46-e406-d797a6d896a7"
df["etc"]=df["Type"].apply(lambda x: 1 if x=="etc" else 0)
df.head()

# + [markdown] id="mUFwPq7KJkQw"
# ### run the regression
#
# Treatment: ETC hard-fork

# + id="oQ2RfZhUJQ0I"
 import statsmodels.api as sm
 import statsmodels.formula.api as smf

# + colab={"base_uri": "https://localhost:8080/"} id="Ns-3OEd-J1Zy" outputId="6934fdce-a928-4bbd-e9a7-cfc398ed192f"
results = smf.ols('VtyDayRet30d ~ 1+ etc +days+TxTfrValAdjUSD+FeeMedUSD+RevUSD+GasLmtTxMean', data=df).fit()
print(results.summary())

# + colab={"base_uri": "https://localhost:8080/"} id="gN-_gCC7K8p4" outputId="01b1b856-b96f-4ba7-c003-94dfe435c3cc"
results = smf.ols('VtyDayRet30d ~ 1+ etc +days +etc*days+TxTfrValAdjUSD+FeeMedUSD+RevUSD+GasLmtTxMean', data=df).fit()
print(results.summary())

StarcoderdataJupyterScriptsDedupFiltered