-
-
Save EM5813/73d9c71e34e7018dc91233377c7fa783 to your computer and use it in GitHub Desktop.
all the code
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 numpy as np | |
import warnings | |
from pandas_datareader import data as pdr | |
import yfinance as yf | |
import datetime as dt | |
from yahoo_fin import stock_info as si | |
import pandas as pd | |
pd.set_option('display.max_rows', None) | |
warnings.filterwarnings("ignore") | |
yf.pdr_override() | |
num_of_years = 1 | |
start = dt.date.today() - dt.timedelta(days = int(365.25*num_of_years)) | |
end = dt.date.today() | |
tickers = si.tickers_dow() | |
dataset = pdr.get_data_yahoo(tickers, start, end)['Adj Close'] | |
stocks_returns = np.log(dataset/dataset.shift(1)) | |
print('\nCorrelation Matrix') | |
corr_matrix = stocks_returns.corr() | |
print (corr_matrix) | |
def get_redundant_pairs(df): | |
pairs_to_drop = set() | |
cols = df.columns | |
for i in range(0, df.shape[1]): | |
for j in range(0, i+1): | |
pairs_to_drop.add((cols[i], cols[j])) | |
return pairs_to_drop | |
def get_top_abs_correlations(df): | |
au_corr = df.corr().abs().unstack() | |
labels_to_drop = get_redundant_pairs(df) | |
au_corr = au_corr.drop(labels=labels_to_drop).sort_values(ascending=False) | |
return au_corr | |
print("\nTop Absolute Correlations") | |
print(get_top_abs_correlations(stocks_returns)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment