How to quickly analyze risk adjusted returns

September 21, 2024
Facebook logo.
Twitter logo.
LinkedIn logo.
Get this code in Google Colab

How to quickly analyze risk adjusted returns

Investors constantly face the challenge of assessing risk-adjusted returns. Not only is it important for proper portfolio management, but to compare strategies on a like-for-like basis.

Metrics like the Sharpe Ratio is ok but treats positive and negative volatility the same. Most of only care about losing money.

I've been through this myself.

When I first started trading at 18 and later as a professional trader, I realized how misleading it could be to treat all volatility the same. I remember the pain of seeing profits evaporate because I wasn't considering risk properly.

By reading today's newsletter, you'll get Python code to compute the Sortino ratio and enhance your portfolio management strategies.

Let's go!

How to quickly analyze risk-adjusted returns

The Sortino ratio is a key metric for evaluating risk-adjusted returns in stock portfolios. Unlike the Sharpe ratio, it focuses only on downside risk. This can give you a clearer picture of an investment’s performance relative to potential losses.

https://x.com/pyquantnews/status/1833841627216957517

In practice, the Sortino ratio is calculated by dividing the portfolio’s excess return by the downside deviation. This involves isolating negative deviations from the expected return. A portfolio with a higher Sortino ratio indicates better risk-adjusted returns.

Professionals use the Sortino ratio to compare different investments. Ultimately, they can prioritize those with higher Sortino ratios.

This metric is particularly useful for tailoring investment strategies to specific goals and risk tolerance.

Let's see how it works with Python.

Load stock data and calculate daily returns

We start by loading stock data for three companies: Apple (AAPL), Microsoft (MSFT), and Google (GOOGL). We will use the adjusted closing prices from January 1, 2020, to January 1, 2022. Then, we calculate the daily returns for these stocks.

1import pandas as pd
2import numpy as np
3import yfinance as yf
4import matplotlib.pyplot as plt
5
6tickers = ['AAPL', 'MSFT', 'GOOGL']
7data = yf.download(tickers, start='2020-01-01', end='2022-01-01')['Adj Close']
8
9returns = data.pct_change().dropna()

We use the yf.download method from the yfinance library to fetch adjusted closing prices for the specified tickers and date range.

The pct_change method calculates the daily returns, which represent the percentage change in stock prices from one day to the next. The dropna method ensures that we remove any missing values from our data.

Calculate portfolio returns

Next, we will calculate the returns of a portfolio consisting of the three stocks. We assign specific weights to each stock and compute the portfolio returns based on these weights.

1weights = np.array([0.4, 0.4, 0.2])
2portfolio_returns = returns.dot(weights)

We create an array of weights that represent the proportion of the portfolio allocated to each stock: 40% to Apple, 40% to Microsoft, and 20% to Google.

We then calculate the portfolio returns by taking the dot product of the daily returns and the weights. This gives us a time series of portfolio returns, which is the weighted sum of the individual stock returns for each day.

Calculate Sortino ratio

We will now calculate the Sortino ratio for the portfolio. The Sortino ratio measures the risk-adjusted return, focusing only on downside risk. We first determine the excess returns over a risk-free rate and then compute the downside deviation.

1risk_free_rate = 0.01
2target_return = 0
3excess_return = portfolio_returns - risk_free_rate / 252
4downside_returns = excess_return[excess_return < 0]
5downside_deviation = np.std(downside_returns)
6sortino_ratio = np.mean(excess_return) / downside_deviation

We set the risk-free rate at 1% and assume a target return of 0. The excess return is calculated by subtracting the daily risk-free rate from the portfolio returns. We identify the downside returns, which are the negative excess returns, and compute the standard deviation of these downside returns to get the downside deviation.

Finally, we calculate the Sortino ratio by dividing the mean excess return by the downside deviation.

Easy!

Plot portfolio returns and visualize the downside risk

Lastly, we will plot the portfolio returns and highlight the periods where the returns are below the target return. This will help us visualize the downside risk.

1plt.figure(figsize=(12, 6))
2plt.plot(portfolio_returns.index, portfolio_returns, label='Portfolio Returns')
3downside_returns = portfolio_returns[portfolio_returns < target_return]
4plt.fill_between(downside_returns.index, downside_returns, alpha=0.5, color='red', label='Downside Returns')
5plt.title('Portfolio Returns with Downside Risk Highlighted')
6plt.xlabel('Date')
7plt.ylabel('Returns')
8plt.legend()
9plt.show()

The result is the following plot.

We create a plot with portfolio returns on the y-axis and dates on the x-axis. We use the plot method from matplotlib to visualize the portfolio returns.

This visualization helps us easily identify periods of negative performance.

Your next steps

Try changing the weights assigned to the stocks and observe how the portfolio returns and the Sortino ratio change. You can also experiment with different time periods to see how the portfolio would have performed in various market conditions. This will give you a deeper understanding of portfolio performance and risk management.

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