R Tutorial : Challenges of portfolio optimization

Portfolio optimization is hard because solvers demand specific problem formats and handling constraints is tedious. The tutorial shows translating a quadratic utility function into R's quadprog package, which requires sign flips and careful setup. The portfolioAnalytics package wraps this complexity, letting you change constraints and objectives without rebuilding the solver each time.
Chapters
Portfolio optimization is a hard problem. One challenge is knowing what solver to use and the capabilities and limits of the chosen solver. A solver is an algorithm designed to find the optimal solution to a given problem. You can choose a solver based on the objective or formulate the objective to fit the chosen solver. Ideally, you want the flexibility of effortlessly switching between solvers depending on the problem. In many cases there is more than one solver that can solve the problem and you should evaluate both. An example of a closed form solver is a quadratic programming solver. The main advantage of closed form solvers is that they solve a given problem very fast and efficiently and return an exact solution. The main drawback is that the problem must be formulated in a very specific way that is typically unique to the solver. An example of a global solver is differential evolution optimization. Global solvers have the advantage of being able to solve more general types of problems and find the approximate solution of the global minimum or maximum of the objective function. However, the algorithms used in global solvers are relatively more complex and more compute intensive. In this course you will use both closed form and global solvers and and learn how portfolio analytics overcomes these challenges of portfolio optimization. For this next example, you will consider a portfolio optimization where the objective is to maximize quadratic utility subject to the constraints such that the weight of each asset must be greater than or equal to 0 and the weights must sum to 1. The intuition behind this formulation is that you maximize portfolio return with a penalty term for portfolio variance. The first term is the portfolio expected return, the transpose of the weights vector multiplied by the expected return vector. The second term is lambda multiplied by the portfolio variance. The transpose of the weights vector multiplied by the variance covariance matrix of the asset returns multiplied by the weight vector. The lambda term is often referred to as the risk aversion parameter. A higher value for lambda means a higher penalty for portfolio variance. You can use the solve QP function from the quadprog package to solve the quadratic utility maximization problem. Solve QP solves quadratic programming problems of the form as shown in the slide. To minimize the negative of the transpose of the D vector times the B vector plus 1/2 times the transpose of the B vector times the D matrix times the B vector. Note the similarities between the form shown here and the quadratic utility on the previous slide. You can substitute the weights with the B vector, the expected returns with the D vector and the variance covariance matrix with the D matrix. Also notice that the signs are different. This is okay because minimizing the negative of an objective is equivalent to maximizing the objective. The code on this slide solves the quadratic utility optimization problem with Solve QP from the quadprog package. Do not worry about understanding everything on this slide. The main point is to demonstrate what it takes to set up and solve a problem directly with a quadratic programming solver in R. This is a lot of work for a simple problem and prone to mistakes. What if we want to use a different solver? Add, remove or change constraints? We have to do this all over to fit the other solver. This can be challenging even for trivial problems. There is little support for visualization when using solvers directly and it is often the case that visualizations are specific to the solver throughout the exercises and videos. In this course you will learn how to easily edit constraints and objectives and view intuitive visualizations relevant to the portfolio optimization problem with the portfolio analytics package. Let's move on to some more exercises.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







