# A fast R function for calculating rolling downside deviation

Fabian Scheler
July 20, 2022

Risk is traditionally often defined as a deviation from the expected value. However, in time series analysis, the latter is commonly equated with the mean, which yields the widely used standard deviation formula.

However, as every financial market participant has probably painfully experienced at some point, negative deviations from the mean feel very different to positive divergences. In other words, investors strongly prefer gains and experience loss aversion, a particular distaste for returns smaller than 0, regardless of the mean or expected return.

Among other things, behavioural studies have shown that this strong dislike for losses can result in objectively irrational behaviour. Examples include increased risk appetite among investors once they have incurred a loss or the tendency to hold on to investments whose value has fallen below the purchase price.

Beyond that, it is well known that the returns of many assets are not normally distributed. Especially the returns from alternative investments like short-volatility strategies or merger arbitrage tend to be asymmetric or skewed. This makes comparisons based on a symmetric measure such as standard deviation problematic.

Lastly, the standard deviation is not a very intuitive risk measure. Private investors, in particular, tend to cope better with more visual descriptions of risk, such as maximum drawdowns.

The above points are why academics and finance professionals have developed alternative risk measures. One of these risk measures is the downside deviation. Downside deviation looks at the frequency and scale of returns below a specific threshold value, usually set to 0.

It thus provides investors with a more targeted assessment of an asset's or strategy's likelihood to produce losses and the magnitude thereof, which tends to be a bit more tangible than the two-sided deviation from the mean (even though results tend to be relatively similar in practice).
Mathematically, the measure is defined as outlined below.

Most importantly, unlike the similarly well-known semi-variance, downside deviation divides the sum of the squared negative deviations from the threshold value by the total number of observations, not just by the number of such negative data points.

There is an efficient implementation for R, including additional documentation in the excellent PerformanceAnalytics package. However, the library does not provide a solution for rolling returns, which is, therefore, addressed by the function below.

Using the example Investopedia gave in another article covering the topic makes it easy to verify the results obtained by both the PerformanceAnalytics function and the function provided here that builds on runSum from the fantastic TTR package. Generally, due to their speed and ease of use, TTR, roll and caTools are must-have R packages for time series analysis.

Let's finally apply this to a practical example.