Get Started

Calculation of portfolio returns

For your first exercise on calculating portfolio returns, you will verify that a portfolio return can be computed as the weighted average of the portfolio component returns. In other words, this means that a portfolio return is calculated by taking the sum of simple returns multiplied by the portfolio weights. Remember that simple returns are calculated as the final value minus the initial value, divided by the initial value.

Assume that you invested in three assets. Their initial values are 1000 USD, 5000 USD, 2000 USD, respectively. Over time, the values change to 1100 USD, 4500 USD, and 3000 USD.

This is a part of the course

“Introduction to Portfolio Analysis in R”

View Course

Exercise instructions

  • Create a vector of the initial asset values in_values.
  • Create a vector of the final values, fin_values.
  • Create a vector of the initial weights, weights.
  • Use the simple return definition to compute the vector of returns on the three component assets. Assign return values to returns.
  • Assign the portfolio returns to preturns.

Hands-on interactive exercise

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

# Vector of initial value of the assets
in_values <- 
  
# Vector of final values of the assets
fin_values <-

# Weights as the proportion of total value invested in each asset
weights <-

# Vector of simple returns of the assets 
returns <- (___ - ___)/___
  
# Compute portfolio return using the portfolio return formula
preturns <- sum(___*___)
  

This exercise is part of the course

Introduction to Portfolio Analysis in R

BeginnerSkill Level
4.5+
4 reviews

Apply your finance and R skills to backtest, analyze, and optimize financial portfolios.

Asset returns and portfolio weights; those are the building blocks of a portfolio return. This chapter is about computing those portfolio weights and returns in R.

Exercise 1: Welcome to the courseExercise 2: Getting a grasp of the basicsExercise 3: Get a feel for the dataExercise 4: The portfolio weightsExercise 5: Calculating portfolio weights when component values are givenExercise 6: The weights of an equally weighted portfolioExercise 7: The weights of a market capitalization-weighted portfolioExercise 8: The portfolio returnExercise 9: Calculation of portfolio returns
Exercise 10: From simple to gross and multi-period returnsExercise 11: The asymmetric impact of gains and lossesExercise 12: PerformanceAnalyticsExercise 13: Buy-and-hold versus (daily) rebalancingExercise 14: The time series of asset returnsExercise 15: The time series of portfolio returnsExercise 16: The time series of weights

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