Lesson 0001

Formulate a Portfolio Optimizer

Your first durable skill: looking at a portfolio problem and naming the variables, data, objective, constraints, and solver family.

The Pattern

Optimization is disciplined choosing. A solver does not understand "build me a good portfolio"; it understands a mathematical formulation. The core move is to separate what the solver may choose from what is fixed while it chooses.

1. Variables

What is the optimizer allowed to choose? In a basic portfolio problem, the variable is usually the weight vector w.

2. Data

What is known at solve time? Expected returns mu, covariance Sigma, current holdings, and cost estimates are data.

3. Objective

What single score is being optimized? A common score rewards expected return and penalizes risk and trading cost.

4. Constraints

What must always be true? Examples: fully invested, long-only, leverage cap, turnover cap, or sector exposure bounds.

The official CVXPY quadratic-program example gives a finance case in which portfolio allocation balances expected return and variance under long-only and full-investment constraints. The MOSEK Portfolio Optimization Cookbook organizes a much wider set of portfolio formulations, including Markowitz optimization, transaction costs, robust optimization, and multiperiod optimization.

A First Formulation

Suppose you allocate across n assets, with expected returns mu and covariance matrix Sigma. A simple long-only Markowitz-style problem is:

choose        w in R^n
maximize     mu' w - gamma * w' Sigma w
subject to   1' w = 1
             w >= 0

Read it literally: choose portfolio weights; reward expected return; penalize variance; require all capital to be allocated; forbid short positions. The parameter gamma controls how expensive risk is. Larger gamma usually pushes the solution toward lower-variance portfolios.

Interview habit: when given an optimizer, first ask "what is chosen, what is known, what is optimized, and what cannot be violated?" This is often more valuable than jumping to a solver.

Why This Leads to Multiperiod Optimization

Single-period optimization chooses today's portfolio. Multiperiod optimization chooses a plan over several future periods: trade now, anticipate future risks/costs/constraints, then usually execute only the first trade and re-solve later. Boyd et al. describe this as closely related to model predictive control.

single-period: choose w_today
multiperiod:  choose w_1, w_2, ..., w_T
              execute first trade
              update forecasts
              solve again

The mental pieces stay the same. There are more variables, more data indexed by time, and constraints that connect periods, but the formulation discipline does not change.

Retrieval Practice

In maximize mu' w - gamma * w' Sigma w, what is w?

Which item is problem data, not a decision variable, in the simple formulation?

What changes first when moving from single-period to multiperiod optimization?

Primary Source

Read the first two pages of "Multi-Period Trading via Convex Optimization" by Boyd et al. slowly. Your job is not to understand every equation yet; it is to identify variables, data, objective terms, and constraints.

Keep the formulation glossary open while reading. For broad optimization background, add Brunton's Optimization Bootcamp to your reading queue.