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.

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