Python Tutorial : Financial Returns

Dakota Wixom teaches how to load stock prices in Pandas, calculate daily returns with .pct_change(), and plot the distribution in Matplotlib. You'll learn the difference between discrete and log returns, why discrete returns work best for portfolios, and how to handle the NaN values that appear when computing returns.
Chapters
Hello and welcome to this course on portfolio risk management in Python. I'm Dakota Wixom and I'll be your instructor for this course. In this course, you'll learn how to analyze return distributions, build portfolios, identify factors driving portfolio returns, and even estimate potential losses. But before we get into portfolio investing, factor analysis, or risk management, it is essential to understand the fundamental properties of financial returns, which is what this chapter is all about. To kick things off, let's first define the term risk itself. Financial risk can simply be thought of as a measure of the uncertainty of future returns. When analyzing returns, we can say that risk is essentially the dispersion or variance of those returns. What I'm getting at is that financial risk has many different definitions, but they all hinge on two fundamental returns and probability. Before we get ahead of ourselves, let's do a quick refresher on financial returns. Financial returns are generally derived from stock prices and are expressed as percentages in decimal form. There are two common types of financial discrete returns, also called simple returns, and log returns, also called continuous returns. For this course, you will be using mostly discrete returns at the daily frequency. Log returns are used for some advanced formulas and financial models. In finance, there is a very simple formula to calculate simple returns, which is simply today's price minus yesterday's price divided by yesterday's price. This gives you a percentage gain or loss for the day. Log returns are actually pretty easy to calculate too, but we can ignore them for the sake of this course. What is important to understand though, is that log returns and aggregate across time, while discrete returns aggregate across assets. Since you are building portfolios with multiple assets, discrete returns make the most sense to work with. For this course. In order to calculate returns, you're first going to need stock price data to work with. We will provide you with CSV files containing stock prices, and it's your job to read in that data and process it into a data frame. To accomplish all of this, you'll need to work with the Python module Pandas, which will come in handy for this course. Make sure to parse the dates, sort the values by date, and then set the index to the date column. Now that you've wrangled the data, calculating daily returns is as easy as calling the.pctchange method on the proper column. If you call the head method on your returns, you'll notice that the very first period has an invalid return entry. This is because in the first period there was no previous price to derive the return from. In order to visualize the distribution of your daily returns, you're going to need to first import the matplotlib pyplot module. Remember how the first period had no return? You will need to call the dropna method on your returns before passing them into the hist function. Setting bins equal to a large number will spread out your plot, but a low number will cause a lack of resolution. If you set density equal to false, you will plot the frequency of returns rather than the probability density on the y axis. Finally, call PLT show to display the plot. Got everything? Great. Now let's try some examples.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







