Skip to content
# 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('datasets/stock_data.csv', index_col = 'Date', parse_dates = ['Date']).dropna()
benchmark_data = pd.read_csv('datasets/benchmark_data.csv', index_col = 'Date', parse_dates = ['Date']).dropna()

A first glance at the data

# 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())

Plot & summarize daily prices for Amazon and Facebook

Before we compare an investment in either Facebook or Amazon with the index of the 500 largest companies in the US, let's visualize the data, so we better understand what we're dealing with.

# visualize the stock_data
stock_data.plot(subplots= True, title = 'Stock Data')


# summarize the stock_data
stock_data.describe()

Visualize & summarize daily values for the S&P 500

Let's also take a closer look at the value of the S&P 500, our benchmark.

# plot the benchmark_data
benchmark_data.plot(kind = 'line', title= 'S&P 500')


# summarize the benchmark_data
benchmark_data.describe()

The inputs for the Sharpe Ratio: Starting with Daily Stock Returns

The Sharpe Ratio uses the difference in returns between the two investment opportunities under consideration.

However, our data show the historical value of each investment, not the return. To calculate the return, we need to calculate the percentage change in value from one day to the next. We'll also take a look at the summary statistics because these will become our inputs as we calculate the Sharpe Ratio. Can you already guess the result?

# calculate daily stock_data returns
stock_returns = stock_data.pct_change()

# plot the daily returns
stock_returns.plot(kind = 'line')

# summarize the daily returns
stock_returns.describe()

Daily S&P 500 returns

For the S&P 500, calculating daily returns works just the same way, we just need to make sure we select it as a Series using single brackets [] and not as a DataFrame to facilitate the calculations in the next step.

# calculate daily benchmark_data returns
# ... YOUR CODE FOR TASK 6 HERE ...
sp_returns = benchmark_data['S&P 500'].pct_change()

# plot the daily returns
sp_returns.plot(kind = 'line')

# summarize the daily returns
sp_returns.describe()

Calculating Excess Returns for Amazon and Facebook vs. S&P 500

Next, we need to calculate the relative performance of stocks vs. the S&P 500 benchmark. This is calculated as the difference in returns between stock_returns and sp_returns for each day.

# calculate the difference in daily returns
excess_returns = stock_returns.sub(sp_returns, axis = 0)

# plot the excess_returns
excess_returns.plot(kind = 'line')

# summarize the excess_returns
excess_returns.describe()

The Sharpe Ratio, Step 1: The Average Difference in Daily Returns Stocks vs S&P 500

Now we can finally start computing the Sharpe Ratio. First we need to calculate the average of the excess_returns. This tells us how much more or less the investment yields per day compared to the benchmark.

# calculate the mean of excess_returns 
avg_excess_return = np.mean(excess_returns)

# plot avg_excess_returns
avg_excess_return.plot.bar(title = 'Mean of the Return Difference')