Portfolio Optimization

Problem Statement

This example solves a financial portfolio optimization model, using historical return data.

Full example scenario

An investor has historical return data for a set of stocks and wants to build a minimum-risk portfolio, then see how that risk changes as the required return is raised. Which fraction of the budget should go into each stock to minimize portfolio risk, for a given target return?

  • Step 1: calculate the minimum-risk portfolio.

  • Step 2: solve for the efficient frontier by varying the target expected return across a range and re-solving for the minimum-risk portfolio at each one.

  • The portfolio must be fully invested (weights sum to 1) and long-only (no short-selling).

  • The stock return data can be found in portfolio.csv, with one column per stock ticker and one row per periodic return observation (as decimal returns, not prices).

What an optimal solution looks like

Minimum-risk portfolio — volatility 1.29%, expected return 0.24%, 7 of 10 stocks held.

Individual stocks Minimum-risk portfolio Efficient frontier
0.02 0.03 0.04 0.05 -1.5% -1.0% -0.5% 0.0% 0.5% 1.0% 1.5% Volatility (standard deviation) Expected return Efficient frontier AA AA vol 1.60% · ret 0.16% BB BB vol 5.45% · ret 1.42% CC CC vol 4.80% · ret -1.36% DD DD vol 2.43% · ret 0.72% EE EE vol 3.77% · ret 0.18% FF FF vol 3.01% · ret -0.47% GG GG vol 2.30% · ret 0.28% HH HH vol 2.41% · ret 0.51% II II vol 2.57% · ret -0.07% JJ JJ vol 4.05% · ret 0.42% Minimum-risk portfolio vol 1.29% · ret 0.24%

Hover or focus a point for its exact volatility and return.

Built with Modeler

The problem above is stated as a single shot: you hand over the whole problem and get a model back. Modeler takes the other route — you start with the business question you actually have, and it works with you until the problem is pinned down, the data is in, the code runs, and the answer holds up when the problem changes.

The tabs below walk the same portfolio optimization problem through that route, one Modeler capability at a time.

Instead of writing the whole prompt up front, you start with the business question you actually have. Modeler searches the knowledge base, asks one clarifying question at a time — always the most important one first — and tells you which parts of the model are still missing. The options it offers come from examples in the knowledge base and from how similar problems are usually modeled, so you can answer with a number instead of prose.

I have a bunch of stocks I track, and I want to figure out how to split my money across them to minimize risk. Can you help me build a model for that?

“Minimize risk” isn’t a number yet — this is the classic Markowitz portfolio problem, but “risk” could mean pure variance minimization, a risk/return trade-off, or a floor on return. Let me search the knowledge base for how this is usually modeled, then ask which reading the user actually wants before assuming variance alone.

Querying the knowledge base.

Great question! This is a classic portfolio optimization problem, well-suited for Gurobi. Let me ask you a few questions to nail down the details so we can build a precise specification.

Question 1: What does “minimizing risk” mean to you in this context?

  1. Minimize portfolio variance (or equivalently, volatility) — the classic Markowitz mean-variance approach, which uses the covariance matrix of stock returns to measure risk.

  2. Minimize volatility subject to a minimum expected return — you accept a floor on returns in exchange for the lowest possible risk (this traces the efficient frontier).

  3. Minimize a risk/return trade-off — balance risk and return using a user-defined weight parameter (e.g., maximize return − lambda × variance).

Learn More:
• Portfolio Selection Optimization ★★★
• Mean-Variance Portfolio Optimization ★★☆

Option 1 — just minimize portfolio variance for now, no target return floor. One more thing: I’d also like to see how the minimum risk changes if I require a range of different target returns, so I can compare the risk/return trade-off across a few scenarios, not just get the single minimum-risk point.

That settles the objective: pure minimum-variance, no return floor. The volunteered frontier-sweep request is a natural extension of the same objective — solve the same minimum-variance problem repeatedly with an added target-return constraint — so it belongs in the specification as a second solve mode, not a separate model. Recording both. Still open: how the money is actually allowed to be allocated (shorting, per-stock limits, sector caps) — asking that next, since it changes the variable bounds rather than just the objective.

Recorded — minimize portfolio variance, with an efficient frontier sweep across target returns as a second output. The knowledge base confirms this is a classic Markowitz minimum-variance model, a great fit for Gurobi’s quadratic programming capabilities.

Question 2: What constraints do you want to place on how the money is allocated?

  1. Fully invested — the weights of all stocks must sum to exactly 100% (all money is allocated).

  2. Long-only — no short-selling allowed, so each stock weight must be between 0 and 1 (100%).

  3. Weight bounds — each stock has a minimum and/or maximum allocation (e.g., no single stock can exceed 30% of the portfolio).

  4. Cardinality constraint — you must hold exactly (or at most) a fixed number of stocks (e.g., pick at most 10 out of 50 stocks).

  5. Sector/group constraints — the combined allocation to stocks in a given sector must stay within a range (e.g., no more than 40% in technology stocks).

Fully invested (weights sum to 1), long-only (no shorting), no other caps or sector limits for now.

Thinking…

Modeler updates the specification after every answer, even while it is still incomplete — so you can always see what it has understood and what it is still waiting on.