Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 38 · Linear and Quadratic Programs

Portfolio Optimization as a QP

Model the mean-variance portfolio problem as a QP: minimize risk for a target return with no short selling, then trace the efficient frontier with a solver. Educational, not advice.

A classic use of quadratic programming is choosing how to split money among several assets. Each asset has an expected return and a risk. Combining assets whose prices do not move together lowers the total risk. Harry Markowitz turned this idea into a convex problem in the 1950s, and it remains the standard textbook example of a QP.

This lesson is about the math. The numbers below are invented for teaching, and nothing here is investment advice.

The model

Suppose there are n assets. Let w be the vector of portfolio weights (the fraction of money in each asset). Let μ be the vector of expected returns and Σ the covariance matrix of returns (the diagonal holds variances; off-diagonals say how assets move together).

  • The expected portfolio return is μᵀw.
  • The portfolio variance is wᵀΣw, a convex quadratic in w, because Σ is positive semidefinite.

The minimum-variance problem for a target return r is:

Text
minimize    wᵀ Σ w
subject to  1ᵀ w = 1          (weights add up to 100%)
            μᵀ w ≥ r          (return at least r)
            w ≥ 0             (no short selling)

This is a QP with P = 2Σ. Its constraints are affine, so Slater's condition is automatic when a feasible point exists, strong duality holds and KKT conditions characterize the optimum.

Setting up made-up numbers

Three assets with annual volatilities of 10%, 18% and 25%, expected returns 4%, 7% and 10%, and positive but modest correlations. The covariance matrix is Σ_ij = ρ_ij σ_i σ_j.

Python Build the inputs and check convexity

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the equal-weight portfolio has risk below the plain average of the three volatilities (about 17.7%), thanks to diversification.

Global minimum variance with a closed form

Ignore the return target and the sign constraint for a moment: minimize wᵀΣw subject to 1ᵀw = 1. The KKT system gives w = Σ⁻¹1 / (1ᵀΣ⁻¹1). This may produce negative weights (short positions), which we will forbid next.

Solving the constrained QP

With w ≥ 0 and a return target, we use SLSQP. We sweep the target r from the smallest to the largest expected return to trace the efficient frontier: the set of portfolios with the lowest risk for each level of return.

Python Tracing the efficient frontier

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the table from top to bottom. At the lowest target (4%), the return constraint is not binding: the weights equal the global minimum-variance portfolio printed at the end, which already earns more than 4% and has lower risk (about 9.5%) than holding asset 1 alone (10%). As the target rises, the optimizer shifts weight toward the riskier, higher-return assets. At a target of exactly 10%, only asset 3 can reach it, so w = (0, 0, 1) and the risk equals 25%.

The efficient frontier is a convex curve: risk grows faster and faster as the return target rises.

Reading the multipliers

The multiplier of the return constraint is the price of return: how much extra variance you must accept for one more unit of target return. This is the sensitivity idea from the earlier lesson in action. Multipliers of w_i ≥ 0 that are positive mark the assets that the optimizer would sell short if allowed.

Practical remarks

  • Estimated returns and covariances are noisy. Small input changes can swing the optimal weights a lot, so practitioners add constraints (maximum weights) or regularization.
  • A common alternative adds a penalty: minimize wᵀΣw - γ μᵀw, with γ ≥ 0 controlling the appetite for return. This is still a QP.
  • Real portfolios may have transaction costs and integer lot sizes. Those can break convexity, so check the model before trusting the solver.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Why is the minimum-variance portfolio problem a convex QP?

    A covariance matrix is positive semidefinite, so the variance is convex. Linear constraints keep the feasible set convex.
  2. 2. What does the constraint 1ᵀw = 1 say?

    1ᵀw adds up all weights, so the whole budget is invested.
  3. 3. What does w ≥ 0 forbid?

    Non-negative weights mean you cannot borrow an asset and sell it.
  4. 4. What does the multiplier of the return constraint μᵀw ≥ r measure?

    It is the shadow price of the constraint: the rate at which the minimal variance rises as the target rises.

Summary

  • Portfolio selection is a QP: minimize wᵀΣw subject to a budget, a return target and (optionally) w ≥ 0.
  • Diversification works because off-diagonal covariances are smaller than the volatilities multiplied together.
  • Sweeping the target return traces the efficient frontier.
  • Inputs are estimates, so results are sensitive; use constraints or penalties and treat the output as a tool for analysis, not advice.

Next: Proximal Operators