1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| import matplotlib.pyplot as plt import numpy as np import pandas as pd import pandas_datareader import scipy.stats as stats from mpl_toolkits import mplot3d from matplotlib import cm
tickers = ['GOOGL','FB','AAPL','NFLX','AMZN'] ticker_num = len(tickers) price_data = [] for ticker in range(ticker_num): prices = pandas_datareader.DataReader(tickers[ticker], start='2015-11-30', end = '2020-11-30', data_source='yahoo') price_data.append(prices[['Adj Close']]) df_stocks = pd.concat(price_data, axis=1)
logreturns = np.log(df_stocks/df_stocks.shift(1))[1:] logreturns.columns = tickers logreturns.head()
plt.style.use('ggplot') mu, std = stats.norm.fit(logreturns['GOOGL']) x = np.linspace(mu-5*std, mu+5*std, 500) logreturns['GOOGL'].hist(bins=60, density=True, histtype="stepfilled", alpha=0.5) x = np.linspace(mu - 3*std, mu+3*std, 500) plt.plot(x, stats.norm.pdf(x, mu, std)) plt.title("Log return distribution for GOOGL") plt.xlabel("Return") plt.ylabel("Density")
rows = 2 cols = 2 fig, axs = plt.subplots(rows, cols, figsize=(12,6)) ticker_n = 1 for i in range(rows): for j in range(cols): mu, std = stats.norm.fit(logreturns[tickers[ticker_n]]) x = np.linspace(mu-5*std, mu+5*std, 500) axs[i,j].hist(logreturns[tickers[ticker_n]], bins=60, density=True, histtype="stepfilled", alpha=0.5) axs[i,j].plot(x, stats.norm.pdf(x, mu, std)) axs[i,j].set_title("Log return distribution for "+tickers[ticker_n]) axs[i,j].set_xlabel("Return") axs[i,j].set_ylabel("Density") ticker_n = ticker_n + 1 plt.tight_layout()
|