# Importing required modules
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Settings to produce nice plots in a Jupyter notebook
plt.style.use('fivethirtyeight')
%matplotlib inline
# Reading in the data
stock_data = pd.read_csv('/work/stock_data.csv', parse_dates = True, index_col = ['Date']).dropna()
benchmark_data = pd.read_csv('/work/benchmark_data.csv', parse_dates = True, index_col = ['Date']).dropna()
# Display summary for stock_data
print('Stocks\n')
stock_data.info()
print(stock_data.head())
# Display summary for benchmark_data
print('\nBenchmarks\n')
benchmark_data.info()
print(benchmark_data.head())
Stocks
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 252 entries, 2016-01-04 to 2016-12-30
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Amazon 252 non-null float64
1 Facebook 252 non-null float64
dtypes: float64(2)
memory usage: 5.9 KB
Amazon Facebook
Date
2016-01-04 636.989990 102.220001
2016-01-05 633.789978 102.730003
2016-01-06 632.650024 102.970001
2016-01-07 607.940002 97.919998
2016-01-08 607.049988 97.330002
Benchmarks
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 252 entries, 2016-01-04 to 2016-12-30
Data columns (total 1 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 S&P 500 252 non-null float64
dtypes: float64(1)
memory usage: 3.9 KB
S&P 500
Date
2016-01-04 2012.66
2016-01-05 2016.71
2016-01-06 1990.26
2016-01-07 1943.09
2016-01-08 1922.03
# visualize the stock_data
stock_data.plot(subplots = True, title = 'Stock Data')
# summarize the stock_data
stock_data.describe()
# plot the benchmark_data
benchmark_data.plot(title = 'S&P 500')
# summarize the benchmark_data
benchmark_data.describe()
# calculate daily stock_data returns
stock_returns = stock_data.pct_change()
# plot the daily returns
stock_returns.plot()
# summarize the daily returns
stock_returns.describe()
# calculate daily benchmark_data returns
sp_returns = benchmark_data['S&P 500'].pct_change()
# plot the daily returns
sp_returns.plot()
# summarize the daily returns
sp_returns.describe()
# calculate the difference in daily returns
excess_returns = stock_returns.sub(sp_returns, axis = 0)
# plot the excess_returns
excess_returns.plot();
# summarize the excess_returns
excess_returns.describe()
# calculate the mean of excess_returns
avg_excess_return = excess_returns.mean()
# plot avg_excess_returns
avg_excess_return.plot.bar(title = 'Mean of the Return Difference');
# calculate the standard deviations
sd_excess_return = excess_returns.std()
# plot the standard deviations
sd_excess_return.plot.bar(title = 'Standard Deviation of the Return Difference')
# calculate the daily sharpe ratio
daily_sharpe_ratio = avg_excess_return.div(sd_excess_return)
# annualize the sharpe ratio
annual_factor = np.sqrt(252)
annual_sharpe_ratio = daily_sharpe_ratio.mul(annual_factor)
# plot the annualized sharpe ratio
annual_sharpe_ratio.plot.bar(title = 'Annualized Sharpe Ratio: Stocks vs S&P 500')
# Uncomment your choice.
buy_amazon = True
# buy_facebook = True