Univariate Investment Risk and Returns
Datasets
# Importing pandas, matplotlib
import pandas as pd
import matplotlib.pyplot as plt
# Importing datasets
fpath_csv = 'https://s3.amazonaws.com/assets.datacamp.com/production/course_6836/datasets/MSFTPrices.csv'Financial returns
Financial timeseries data
In finance, it is common to be working with a CSV (comma-separated-values) "flat" file of a timeseries of many different assets with their prices, returns, or other data over time. Sometimes the data is stored in databases, but more often than not, even large banks still use spreadsheets.
In this exercise, you have been given a timeseries of trading data for Microsoft stock as a .csv file stored at the url fpath_csv. When you finish the exercise, take note of the various types of data stored in each column.
You will be using pandas to read in the CSV data as a DataFrame.
# Read in the csv file and parse dates
StockPrices = pd.read_csv(fpath_csv, parse_dates=['Date'])
# Ensure the prices are sorted by Date
StockPrices = StockPrices.sort_values(by='Date')
# Print only the first five rows of StockPrices
print(StockPrices.head())Calculating financial returns
The file you loaded in the previous exercise included daily Open, High, Low, Close, Adjusted Close, and Volume data, often referred to as OHLCV data.
The Adjusted Close column is the most important. It is normalized for stock splits, dividends, and other corporate actions, and is a true reflection of the return of the stock over time. You will be using the adjusted close price to calculate the returns of the stock in this exercise.
StockPrices from the previous exercise is available in your workspace, and matplotlib.pyplot is imported as plt.
# Calculate the daily returns of the adjusted close price
StockPrices['Returns'] = StockPrices['Adjusted'].pct_change()
# Check the first five rows of StockPrices
print(StockPrices.head())
# Plot the returns column over time
StockPrices['Returns'].plot()
plt.show()Return distributions
In order to analyze the probability of outliers in returns, it is helpful to visualize the historical returns of a stock using a histogram.
You can use the histogram to show the historical density or frequency of a given range of returns. Note the outliers on the left tail of the return distribution are what you often want to avoid, as they represent large negative daily returns. Outliers on the right hand side of the distribution are normally particularly good events for the stock such as a positive earnings surprise.
StockPrices from the previous exercise is available in your workspace, and matplotlib.pyplot is imported as plt.
# Convert the decimal returns into percentage returns
percent_return = StockPrices['Returns']*100
# Drop the missing values
returns_plot = percent_return.dropna()
# Plot the returns histogram
plt.hist(returns_plot, bins=75)
plt.show()Mean, variance, and normal distribution
First moment: Mu You can calculate the average historical return of a stock by using numpy's mean() function.
When you are calculating the average daily return of a stock, you are essentially estimating the first moment ( ) of the historical returns distribution.
But what use are daily return estimates to a long-term investor? You can use the formula below to estimate the average annual return of a stock given the average daily return and the number of trading days in a year (typically there are roughly 252 trading days in a year):
The StockPrices object from the previous exercise is stored as a variable.
# Import numpy as np
import numpy as np
# Calculate the average daily return of the stock
mean_return_daily = np.mean(StockPrices['Returns'])
print(mean_return_daily)
# Calculate the implied annualized average return
mean_return_annualized = ((1+mean_return_daily)**252)-1
print(mean_return_annualized)Second moment: Variance
Just like you estimated the first moment of the returns distribution in the last exercise, you can can also estimate the second moment, or variance of a return distribution using numpy.
In this case, you will first need to calculate the daily standard deviation ( ), or volatility of the returns using np.std(). The variance is simply .
StockPrices from the previous exercise is available in your workspace, and numpy is imported as np
# Calculate the standard deviation of daily return of the stock
sigma_daily = np.std(StockPrices['Returns'])
print(sigma_daily)
# Calculate the daily variance
variance_daily = sigma_daily**2
print(variance_daily)