Matlab optimize portfolio

From matlab/matlab-agentic-toolkit · View interactive version

Matlab optimize portfolio is an AI agent skill from the GitHub repository matlab/matlab-agentic-toolkit. Formulate and solve portfolio optimization problems in MATLAB. Routes to the Financial Toolbox Portfolio object for mean-variance (Markowitz), minimum- variance, target-return, maximum Sharpe ratio, and efficient frontier problems with bounds, budget, group, turnover, tracking-error, or cardinality

How to use this skill

Skill file

Portfolio Optimization in MATLAB

Formulate and solve portfolio optimization problems in MATLAB. Route to the

correct framework — the Financial Toolbox Portfolio object or the

problem-based framework (optimproblem/optimvar/solve) — based on the

constraints in the user's request, then follow that framework's reference for

implementation details.

When to Use

When NOT to Use

Framework Routing

Identify the framework from the user's constraints and read the corresponding reference:

| Framework | Reference | When to Use |

|-----------|-----------|-------------|

| Portfolio object | references/portfolio-object.md | Mean-variance, max Sharpe, or efficient frontier where every constraint is one the Portfolio object accepts |

| Problem-based | references/problem-based.md | Any constraint the Portfolio object cannot express, alone or mixed with constraints it accepts |

Routing Rules

Read exactly one reference (and any relevant sub-references). Apply the rules in order; the first match wins:

  1. Risk measure is not variance (CVaR, MAD, downside deviation, custom risk) -> say this case is not covered by this skill and read neither reference
  2. Any constraint the Portfolio object cannot express -> references/problem-based.md. One such constraint is enough, whatever else is in the problem:
  1. Visible code rounds Portfolio output (round/floor/ceil on weights to get shares or lots) -> references/problem-based.md
  2. User names a Portfolio API (Portfolio, estimateFrontier*, estimateMaxSharpeRatio, setBounds, ...) -> references/portfolio-object.md
  3. User writes quadprog/fmincon/optimproblem for a problem the Portfolio object can express -> references/portfolio-object.md, and show the Portfolio equivalent
  4. Ambiguous (e.g., "optimize my portfolio") -> references/portfolio-object.md

Lookalike constraints — check these before applying rule 2:

| Constraint | Framework |

|------------|-----------|

| Semi-continuous on the level: w = 0 OR L <= w <= U | Portfolio (setBounds(..., 'BoundType', 'Conditional')) |

| Minimum trade on the change: \|w-w0\| >= δ OR w = w0 | Problem-based |

| Proportional costs in net return | Portfolio (setCosts) |

| Proportional costs alone, not as part of net return | Problem-based |

| Cap on total cost, or a fixed fee per trade or lot | Problem-based |

| Cardinality on its own | Portfolio (setMinMaxNumAssets) |

Shared Conventions

Both frameworks share these conventions:

Common Mistakes (Cross-Framework)

| Mistake | Correct |

|---------|---------|

| Hand-coded quadprog/fmincon for a problem the Portfolio object can express | Use the Portfolio object — see references/portfolio-object.md |

| Forcing integer lots or fixed commissions into Portfolio with workarounds | Formulate in optimproblem/optimvar — see references/problem-based.md |

| Continuous solve followed by round to get lots | Formulate an integer program with optimvar(..., 'Type', 'integer') — see references/problem-based.md |

| abs() on a signed decision variable | Linearize with auxiliary variables (magnitude aux or buy/sell split) — see references/problem-based.md |

| Assuming setCosts can cap total transaction cost | setCosts models proportional costs only; a cost cap needs references/problem-based.md |

Reference Materials


Copyright 2026 The MathWorks, Inc.


Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.