Get startedGet started for free

Maximize quadratic utility function

In the video on challenges of portfolio optimization, you saw how to solve a quadratic utility optimization problem with the package quadprog. This exercise will show you how to solve a quadratic utility problem using the PortfolioAnalytics package. Recall the quadratic utility formulation has two terms, one for portfolio mean return and another for portfolio variance with a risk aversion parameter, lambda.

This exercise is part of the course

Intermediate Portfolio Analysis in R

View Course

Exercise instructions

  • Create a portfolio specification object using asset names from the index_returns dataset and name the portfolio specification object port_spec.
  • Add a full investment constraint such that the weights sum to 1 to the port_spec object.
  • Add a long only constraint such that the weight of an asset is between 0 and 1 to the port_spec object.
  • Add an objective to maximize portfolio mean return to the port_spec object.
  • Add an objective to minimize portfolio variance to the port_spec object. Risk aversion should be set to 10.
  • Run the optimization. This problem can be solved by a quadratic programming solver so we specify optimize_method = "ROI"

Hands-on interactive exercise

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

# Create the portfolio specification
port_spec <- portfolio.spec(assets = ___)

# Add a full investment constraint such that the weights sum to 1
port_spec <- add.constraint(portfolio = ___, type = ___)

# Add a long only constraint such that the weight of an asset is between 0 and 1
port_spec <- add.constraint(portfolio = ___, type = ___)

# Add an objective to maximize portfolio mean return
port_spec <- add.objective(portfolio = ___, type = ___, name = ___)

# Add an objective to minimize portfolio variance
port_spec <- add.objective(portfolio = port_spec, type = ___, name = ___, risk_aversion = ___)

# Solve the optimization problem
opt <- optimize.portfolio(R = ___, portfolio = ___, optimize_method = "ROI")
Edit and Run Code