Introduction to the Sharpe Ratio using Pandas, Numpy and MatPlotLib
The Sharpe Ratio is a widely used measure in finance to compare different investment opportunities based on their risk-adjusted returns. It was introduced by Professor William Sharpe in 1966 and has since become one of the most popular risk/return measures.
The Sharpe Ratio calculates the additional return per unit of risk that an investor could obtain by choosing one investment over another. It compares the expected returns of two investments and considers the difference in returns relative to the standard deviation (a measure of risk) of this difference.
By using the Sharpe Ratio, investors can assess the trade-off between risk and return and make informed decisions about their investments. A higher Sharpe Ratio indicates that an investment offers a higher reward for a given level of risk.
Using the Sharpe Ratio to Choose Between Facebook and Amazon Stocks
In our example, we will calculate the Sharpe Ratio for the stocks of two tech giants, Facebook and Amazon. We will use the S&P 500 as a benchmark, which represents the performance of the 500 largest stocks in the US.
By comparing the Sharpe Ratios of Facebook and Amazon stocks against the benchmark, we can determine which stock offers a better risk-adjusted return. The stock with a higher Sharpe Ratio would be considered the more favorable option, as it provides a higher return for a given level of risk.
It's important to note that the Sharpe Ratio is typically calculated for a portfolio and uses the risk-free interest rate as a benchmark. However, in our simplified example, we are using individual stocks and a stock index as the benchmark. This approach is commonly used when comparing the performance of active portfolio management against a low-cost index fund.
To calculate the Sharpe Ratio, we would need historical returns data for Facebook and Amazon stocks, as well as the S&P 500 index. We would also need to determine the risk-free rate, which could be the 3-month Treasury Bill Rate or another suitable benchmark.
By applying the Sharpe Ratio methodology, investors can make more informed decisions about their investments and assess the risk-adjusted returns of different options.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
%matplotlib inline
stock_data = pd.read_csv("datasets/stock_data.csv", parse_dates = ["Date"], index_col="Date").dropna()
benchmark_data = pd.read_csv("datasets/benchmark_data.csv", parse_dates = ["Date"], index_col="Date").dropna()Stock DataFrame
stock_dataBenchmark Dataframe
benchmark_data1 - A first glance at the data for basic information about the datasets and the first couple of entries.
# 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())2 - Plot & summarize daily prices for Amazon and Facebook
We plot the graph for Amazon and Facebook Separately to see the daily stock prices.
# visualize the stock_data
stock_data.plot(subplots=True, title="Stock Data")
# summarize the stock_data
stock_data.describe()3 - Visualize & summarize daily values for the S&P 500
We also look at the daily average stock price for S&P 500.
# plot the benchmark_data
benchmark_data.plot(title="S&P 500")
# summarize the benchmark_data
benchmark_data.describe()4 - 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.
# 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()5 - Daily S&P 500 returns