Get Started

Sortino ratio

For this exercise, the portfolio returns data are stored in a DataFrame called df, which you'll use to calculate the Sortino ratio. The Sortino ratio is just like the Sharpe ratio, except for that it uses the standard deviation of the negative returns only, and thereby focuses more on the downside of investing.

Let's see how big the Sortino ratio is compared to the earlier calculated Sharpe ratio. The risk-free rate rfrand the target return target are already defined and are both zero.

This is a part of the course

“Introduction to Portfolio Analysis in Python”

View Course

Exercise instructions

  • Select the returns using .loc that are strictly less than the target, and store them in a new DataFrame called downside_returns.
  • Calculate the mean of the expected returns, and the standard deviation of the downside returns.
  • Calculate the Sortino ratio using rfr for the risk-free rate.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create a downside return column with the negative returns only
downside_returns = df.loc[df['pf_returns'] ____ target]

# Calculate expected return and std dev of downside
expected_return = df['____'].____()
down_stdev = downside_returns['pf_returns'].____()

# Calculate the sortino ratio
sortino_ratio = (____ - ____)/____

# Print the results
print("Expected return  : ", expected_return*100)
print("Downside risk   : ", down_stdev*100)
print("Sortino ratio : ", sortino_ratio)
Edit and Run Code

This exercise is part of the course

Introduction to Portfolio Analysis in Python

AdvancedSkill Level
4.7+
9 reviews

Learn how to calculate meaningful measures of risk and performance, and how to compile an optimal portfolio for the desired risk and return trade-off.

Chapter 2 goes deeper into how to measure returns and risk accurately. The two most important measures of return, annualized returns, and risk-adjusted returns, are covered in the first part of the chapter. In the second part, you’ll learn how to look at risk from different perspectives. This part focuses on skewness and kurtosis of a distribution, as well as downside risk.

Exercise 1: Annualized returnsExercise 2: Annualizing portfolio returnsExercise 3: Comparing annualized rates of returnExercise 4: Risk adjusted returnsExercise 5: Interpreting the Sharpe ratioExercise 6: S&P500 Sharpe ratioExercise 7: Portfolio Sharpe ratioExercise 8: Non-normal distribution of returnsExercise 9: Skewness of the S&P500Exercise 10: Calculating skewness and kurtosisExercise 11: Comparing distributions of stock returnsExercise 12: Alternative measures of riskExercise 13: Sortino ratio
Exercise 14: Maximum draw-down portfolio

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free