Created
August 10, 2020 13:40
-
-
Save Vido/cca498b80f925baaaf96f8d4620d960d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import yfinance | |
import numpy as np | |
import statsmodels.api as sm | |
import matplotlib.pyplot as mplt | |
# Download dos dados | |
data = yfinance.download( | |
tickers = 'PSSA3.SA ITUB4.SA', | |
start="2020-05-01", | |
end="2020-08-05", | |
interval = '1d', | |
treads = False | |
) | |
series_x = data['Close']['PSSA3.SA'] | |
series_y = data['Close']['ITUB4.SA'] | |
# Regressão e Resíduo | |
X = sm.add_constant(series_x.values) | |
mod = sm.OLS(series_y, X) | |
results = mod.fit() | |
def half_life(ts): | |
lagged = ts.shift(1).fillna(method="bfill") | |
delta = ts-lagged | |
X = sm.add_constant(lagged.values) | |
ar_res = sm.OLS(delta, X).fit() | |
half_life = -1*np.log(2)/ar_res.params['x1'] | |
return half_life, ar_res | |
half_life, _ = half_life(results.resid) | |
print('halflife: ', half_life) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment