R Tutorial: Analyzing volatility

Chris Baut shows how to compute daily returns in R and estimate volatility using rolling windows. S&P 500 returns have near-zero mean but varying volatility. GARCH models provide more accurate forecasts than simple rolling estimates.
Chapters
Hi and welcome to the course on Garchmalles in R. On Wall street there are old traders and bold traders, but there are very few old bold traders. That's because the bold traders suffer large losses when ignoring the risk of their investments. My name is Chris Baut. My goal is to give you a hands on experience in using GargMalz in R. I have a PhD on forecasting financial risk and use Garchmalz to win by not losing much. My favorite R package is the R Regards package of Alexios Galanos which will be the workhorse for the entire course. To get started you need returns. Take for example the price of a stock and denote this by capital P. Its daily return equals today's price minus yesterday's price divided by yesterday's price. The calculatereturns function in the package performs analytics, makes it easy to compute those returns. It takes the price series as an input and provides you the corresponding returns. In case of the S&P 500, you can see that the average daily return is close to zero and that its variability changes over time. There are periods with a low variability and periods with a high variability. You can quantify this variability by computing the return standard deviation. Investors call this the return volatility and denote it by the Greek character sigma with the index t. To emphasize that its value changes every day, you need to take those changes into account since they heavily influence the probability of losing money. The plot shows this by comparing the return distribution on a turbulent day in blue with the distribution on a calm day in red. On a calm day there is almost no chance of losing more than 5%, but on a turbulent day it becomes likely to lose more than 5%. Getting the volatility right is thus of utmost importance. You can estimate the return standard deviation in R by by applying the function SD to the return series for the daily S&P 500 returns. This gives you a daily volatility of around 1%. The underlying formula is that the standard deviation equals the square root of the average squared deviation of the return from its mean. Financial traders annualize the daily volatility by multiplying it with with the square root of the number of trading days in a year, namely 252. This gives you the standard deviation that you can expect on yearly returns for the S&P 500, you get a 17% annualized volatility. When you compute this annualized standard deviation on subsamples of one year, you get different numbers through time. They vary between a low of 7% and a high of 41%. In a similar fashion, you can compute volatility on rolling estimation samples. We then roll the sample through time by adding the most recent observation and removing the most distant one. Practical question is then to choose the length of the estimation window. Typically, the window is a multiple of 22, which is the number of trading days in a month in R. You can do this rolling calculation with the function chartrollingperformance as arguments. It requires the return series, the size of the window, the estimation function, and the number of trading days in a year. You see here the corresponding rolling volatility estimates. Note the switches between periods of low and high volatility. This rolling approach is too simple in practice. You will later discover that a higher accuracy is achieved when using a Gartric model. But first we refresh the basics of estimating standard deviations.
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







