R Tutorial : Risk-factor returns

Three ways to calculate risk-factor returns: simple, relative, and log. Log returns are standard in quantitative modeling because they keep prices non-negative, work with Black-Scholes, and compound easily over time. An R example shows computing S&P 500 log returns, including the 1987 Black Monday crash.
Chapters
In qrm, the aim is to model the fluctuations and key risk factors which affect the value of a portfolio. These fluctuations are called the risk factor changes or risk factor returns or simply returns. However, there are a few different ways of defining returns. Let ZT be a time series containing the values of a risk factor at a set of regularly spaced times, which could represent days, weeks, months, etc. For illustration, let's suppose it is a daily series. Here are three different ways of defining risk factor returns. XT in the first definition, the returns are the differences of the risk factor values known as simple returns. This is the simplest definition, but not the most common. It tends to be used when the risk factors are very small values close to zero, like certain interest rate series. The second definition is the easiest to interpret. The relative returns are the differences divided by the initial values. If you multiply the relative returns by 100, you get percentage changes. So if a stock has a relative return of 0.02, it gains 2% in value. If it has a relative return of minus 0.03, it falls 3% in value. In the third definition, the returns are the differences of the log values of the risk factors. These are log returns. This definition is in fact the most widely used. Here are a few of the reasons why log returns are popular. If you build a model for the log returns of a risk factor, you know that the risk factor can never become negative. This is generally a desirable feature for risk factors that are prices or rates. However, some risk factors can become negative under unusual market conditions, an example being short term interest rates. Log returns are in fact very close to relative returns for typical values. It can be shown with some algebra that log of zt minus log of zt minus 1 can be approximated by zt minus zt minus 1 divided by zt minus 1. Finally, there are a couple of theoretical reasons for using log returns. It is particularly easy to aggregate them to calculate longer interval returns such as weekly or monthly returns, as we will see later in this chapter. Also, there's a very famous model for prices known as the Black Scholes Merton model or Geometric Brownian motion. The model is widely used for pricing derivatives. If a price series follows gbm, then the log returns will be normally distributed. That's all you really need to know about this model at this stage. It's worth remarking, however, that you're going to find that log returns generally aren't normally distributed in practice, at least for short time horizons. To calculate the log returns of the S&P 500 index, you apply the functions Log and Diff in succession. Diff calculates differences. Here is some code to illustrate. Note that you get an NA in the first position. When you apply these operations, it's best to remove the first value prior to working with the return series. Now let's plot the log returns. You see a fairly noisy series, mostly oscillating between minus 0.05 and 0.05, which can be approximately interpreted as 5% movements up and down. However, there are some very extreme values. The largest negative return corresponds to the famous stock market crash of Black Monday in 1987. Now it's your turn to calculate and plot some returns. The next exercises.
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







