liveBook · Manning

Portfolio optimization

As explained in Build a Robo-Advisor with Python (From Scratch)

Portfolio optimization is the technique of selecting portfolio holdings or weights by optimizing a specified investment objective.

Portfolio optimization selects asset weights by solving an explicit trade-off among expected return, risk, and portfolio rules. It turns allocation, rebalancing, and tax-aware decisions into problems that can be solved quickly and repeatedly, making automated target portfolios practical. A useful formulation states the objective, supplies estimates such as expected returns and a covariance matrix, and adds constraints that express what the portfolio is allowed to hold. section 10.0 section 10.2

A basic mean-variance formulation minimizes portfolio variance, wᵀΣw, while requiring expected return μᵀw to meet a target and weights to sum to one. The inverse formulation maximizes expected return while keeping variance below a volatility budget. These are convex formulations, so they can be solved efficiently and yield a truly optimal solution when their convexity conditions hold. section 10.2.1 section 10.4

Listing 10.3 Formulating the mean-variance problem
import numpy as np
Sigma = np.matrix([[0.0225 , 0.0216 , 0.00075],   
                   [0.0216 , 0.0324 , 0.00045], 
                   [0.00075, 0.00045, 0.0025]])
mu = np.array([.06, .05, .03])
mu_p = .055
N = len(mu)
w = cp.Variable(N)
objective = cp.Minimize(cp.quad_form(w, Sigma))
constraints = [w.T @ mu >= mu_p, cp.sum(w) == 1]
prob = cp.Problem(objective, constraints)
result = prob.solve()
w.value

This cvxpy formulation makes the decision variable, quadratic risk objective, return target, and full-investment constraint visible in a small three-asset problem. section 10.2.1

Constraints convert a mathematical optimum into a portfolio with intended operating characteristics:

  • A long-only constraint requires every weight to be nonnegative, preventing short positions. section 10.2.1 section 10.3.1
  • A full-investment constraint requires weights to sum to one. section 10.2.1 section 10.3.1
  • A volatility or tracking-error constraint limits total risk or deviation from a reference portfolio. section 10.3.1
  • Constraints can reduce the concentrated or unintuitive allocations produced by naive, unconstrained optimization. section 10.4

In Python, define a weight variable, write the objective and constraints, solve the cvxpy problem, then map the resulting values back to asset names. For reusable workflows, constraint objects can generate cvxpy constraints while a mean-variance problem class assembles inputs, solves the problem, and returns a labeled weight series. section 10.2.1 section 10.3.1

A volatility-limited allocation

With expected returns and a covariance matrix for VTI, VEA, VWO, AGG, BNDX, and EMB, maximize expected return subject to long-only, fully invested weights and a 15% annualized-volatility cap. The reported solution allocates about 73.2% to VWO and 26.8% to AGG, illustrating that a constrained optimum can still be concentrated. section 10.3.1

Optimization output depends heavily on its inputs. A one-percentage-point decrease in VWO expected return and increase in VTI expected return produces a sharply different allocation, demonstrating sensitivity to return estimates. Practical use therefore requires inspecting assumptions and constraints rather than treating a solver result as a guaranteed recommendation. section 10.3.1 section 10.4

Portfolio optimization also supports later allocation methods. Under assumptions linking expected returns to volatility and using a diagonal covariance matrix, an unconstrained mean-variance solution assigns weights inversely proportional to asset volatilities, connecting the framework to risk parity. Reverse optimization instead derives expected returns that make a chosen reference portfolio optimal under a basic mean-variance problem. section 11.1.3 section 12.1.1

How Build a Robo-Advisor with Python (From Scratch) covers it

How deep it goes
Works through convex mean-variance formulations, cvxpy code, reusable constraint classes, and sensitivity examples; defers advanced construction techniques.
Examples
Uses three-asset mean-variance weights, a 15% volatility-capped ETF portfolio, and perturbed expected-return inputs.
What it assumes
Assumes prior exposure to mean-variance optimization while refreshing optimization, convexity, and Python solver use.
What you can do after
Build it from scratch in cvxpy as constrained mean-variance optimizations and interpret input-sensitive portfolio weights.
How it connects
Connects mean-variance optimization with convexity, tracking error, volatility, risk parity, and reverse optimization.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Robo-Advisor with Python (From Scratch). The section links point to the parts of the book it draws on.