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 necesary libraries | |
from datetime import date | |
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
import yfinance as yf | |
%matplotlib inline |
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
def boxplot_group_day(returns_slice): | |
Daily_Returns = returns_slice.groupby([returns_slice.index.isocalendar().week.rename('week'), | |
returns_slice.index.dayofweek.rename('day')]).mean() | |
return Daily_Returns.boxplot(column='Returns', by='day') | |
boxplot_group_day(returns); |
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
returns_of_june = returns.loc[returns.index.month.isin([6])] | |
boxplot_group_day(returns_of_june); |
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
returns_of_december = returns.loc[returns.index.month.isin([12])] | |
boxplot_group_day(returns_of_december); |
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
md_returns = returns.squeeze().groupby([returns.index.month.rename('month'), | |
returns.index.dayofweek.rename('day')]).mean().unstack(0) | |
fig, ax = plt.subplots(figsize=(16,6)) | |
sns.heatmap(md_returns, ax=ax, cmap="Spectral"); |
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
Monthly_Returns = ratesD.groupby([ratesD.index.year.rename('year'), | |
ratesD.index.month.rename('month')] | |
).median().to_frame("Returns") | |
Monthly_Returns.boxplot(by='month', figsize=(16, 8)); |
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
Monthly_Returns = ratesD.groupby([ratesD.index.year.rename('year'), | |
ratesD.index.month.rename('month')] | |
).median().to_frame("Returns") | |
Monthly_Returns.boxplot(by='month', figsize=(16, 8)); |
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 statsmodels.api as sm | |
decomposition = sm.tsa.seasonal_decompose(data['Adj Close'], model="additive", period = 253) | |
trend = decomposition.trend | |
seasonal = decomposition.seasonal | |
residual = decomposition.resid | |
fig, axs = plt.subplots(4, figsize=(14, 10), sharex=True) | |
data['Adj Close'].plot(title='Original', color="blue", ax=axs[0]) | |
trend.plot(title='Trend', color="red", ax=axs[1]) |
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
seasonal["2019"].plot(label='Seasonality', color="blue", figsize=(20,8)); |
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
seasonal["2019-11":"2019-12"].plot(label='Seasonality', color="blue", figsize=(20,8)); |
OlderNewer