How to measure your risk-adjusted returns with the Sortino ratio

October 7, 2022
Facebook logo.
Twitter logo.
LinkedIn logo.
Get this code in Google Colab

How to measure your risk-adjusted returns with the Sortino ratio

How to measure your risk-adjusted returns with the Sortino ratio

In today’s issue, I’m going to show you how to use a popular performance metric to measure risk: the Sortino ratio.

The Sortino ratio tells you the risk-adjusted return of an investment. It is similar to the Sharpe ratio except it only considers “downside deviation”. In other words, it only uses the asset's standard deviation of negative returns. Investors consider it a better measure of an asset's risk-adjusted performance since positive volatility is a benefit.

If you’re investing or trading, you might want to consider the Sortino ratio as one of your performance metrics.

Without considering the Sortino ratio, you miss important information about the performance.

Like the Sharpe ratio, you can use the Sortino ratio to compare investments.

For example:

  • Stock A returns 30% with 20% volatility and 30% downside deviation
  • Stock B returns 20% with 13% volatility and 15% downside deviation

Which is the better investment?

Both stocks have the same Sharpe ratios. Stock B’s return is lower but so is the downside deviation. That means stock A has more volatility in its negative returns and is riskier than stock B. The Sortino ratio captures this dynamic.

By the end of this newsletter, you’ll know how to:

  1. Get stock price data
  2. Compute the Sortino ratio
  3. Compute downside deviation
  4. Compare two investment options

All using Python.

Let’s go!

Step 1: Get the data

Start by importing the libraries and getting the data. I like to use yfinance for getting stock data and NumPy for vectorizing outputs.

1import yfinance as yf
2import numpy as np

With yfinance, you can download data for more than one stock at a time. Grab data for both SPY and AAPL.

1data = yf.download(["SPY", "AAPL"], start="2020-01-01", end="2022-07-31")

This gives you a MultiIndex DataFrame. There’s one extra line of code we need to build the returns.

1closes = data['Adj Close']
2spy_returns = closes.SPY.pct_change().dropna()
3aapl_returns = closes.AAPL.pct_change().dropna()

Step 2: Compute the Sortino Ratio

A function for the Sortino ratio in Python is straightforward.

1def sortino_ratio(returns, adjustment_factor=0.0):
2    """
3    Determines the Sortino ratio of a strategy.
4    
5    Parameters
6    ----------
7    returns : pd.Series or np.ndarray
8        Daily returns of the strategy, noncumulative.
9        adjustment_factor : int, float
10        Constant daily benchmark return throughout the period.
11
12    Returns
13    -------
14    sortino_ratio : float
15
16    Note
17    -----
18    See `<https://www.sunrisecapital.com/wp-content/uploads/2014/06/Futures_
19    Mag_Sortino_0213.pdf>`__ for more details.
20    """
21    
22    # compute annualized return
23    returns_risk_adj = np.asanyarray(returns - adjustment_factor)
24    mean_annual_return = returns_risk_adj.mean() * 252
25
26    # compute the downside deviation
27    downside_diff = np.clip(returns_risk_adj, np.NINF, 0)
28    np.square(downside_diff, out=downside_diff)
29    annualized_downside_deviation = np.sqrt(downside_diff.mean()) * np.sqrt(252)
30    
31    return average_annual_return / annualized_downside_deviation

The function takes a series of daily returns and subtracts an adjustment factor. Some investors use this to measure the return over the risk-free rate. Since the risk free-rate has been near 0 for almost a decade, I don’t consider it in the analysis. Finally, take the average return and annualize it.

To compute downside deviation, I use the clip function. The clip returns the numbers in an array between two values. I take the negative returns by using negative infinity and 0. Then I find the standard deviation of the negative returns and annualize it.

The Sortino ratio is the average annualized return divided by the annualized downside deviation.

A note on downside deviation

“Risk” usually refers to the volatility of an asset. That’s because the higher the volatility the higher the uncertainty of where the price will be when you want to sell the asset. When most people think of risk, they usually think of how much money they might lose. Downside deviation attempts to capture the “money-losing” part of the asset volatility.

Step 3: Compare investment options

Compute the Sortino ratio for AAPL and SPY.

1# sortino ratio for SPY
2sortino_ratio(spy_returns)
3
4# sortino ratio for AAPL
5sortino_ratio(aapl_returns)

The Sortino ratio for AAPL is higher than for SPY. That means you got more return for every unit of downside risk.

It’s always useful to look at performance metrics through time because they are never static. It’s easy with Python.

1aapl_returns.rolling(30).apply(sortino_ratio).plot()
How to measure your risk-adjusted returns with the Sortino ratio

Use a histogram to visualize the distribution of rolling Sortino ratios. In the case of AAPL, there is a long tail of positive Sortino ratios.

1aapl_returns.rolling(30).apply(sortino_ratio).hist(bins=50)
How to measure your risk-adjusted returns with the Sortino ratio

Finally, let’s see how the Sortino ratios of AAPL and SPY compare through time. Was AAPL always a better risk adjusted return?

Subtract the SPY Sortino ratio from the AAPL Sortino ratio and plot the result as a histogram.

1(
2    aapl_returns.rolling(30).apply(sortino_ratio)
3    - spy_returns.rolling(30).apply(sortino_ratio)
4).hist(bins=50)
How to measure your risk-adjusted returns with the Sortino ratio

The histogram looks like two overlapping distributions. One distribution is centered around 0 and looks to be skewed left. The other distribution looks to center around 5 and be more even. This might reflect two different periods of time. One where SPY outperformed AAPL and another where AAPL outperformed SPY.

Without the Sortino ratio, you might miss this important information about performance.

Man with glasses and a wristwatch, wearing a white shirt, looking thoughtfully at a laptop with a data screen in the background.