matlab/matlab-agentic-toolkit / skills-catalog/computational-finance/matlab-optimize-portfolio
matlab-optimize-portfolio
VerifiedFormulate 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 constraints; or to the problem-based framework (optimproblem/optimvar/solve) when constraints exceed what Portfolio can express: integer round lots, whole shares, per-lot or fixed commissions, minimum trade sizes, semi-integer positions, wash-sale rules, realized capital-gains caps, tax-loss harvesting, asymmetric or bucketed transaction- cost caps, or a custom efficient frontier over such constraints. Use when users ask about portfolio optimization, when they attempt quadprog, fmincon, intlinprog, or ga for a portfolio problem, or when code rounds Portfolio weights to whole shares or lots.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 9.0 KB |
| manifest.yaml | config | 762 B |
| references/portfolio-object.md | markdown | 6.3 KB |
| references/portfolio-object/examples.md | markdown | 8.8 KB |
| references/portfolio-object/formulation-max-sharpe.md | markdown | 3.6 KB |
| references/portfolio-object/formulation-mean-variance.md | markdown | 3.7 KB |
| references/portfolio-object/reference-core.md | markdown | 6.8 KB |
| references/problem-based.md | markdown | 19.2 KB |
| references/problem-based/custom-frontier.md | markdown | 9.3 KB |
| references/problem-based/disjunctive-big-m.md | markdown | 5.9 KB |
| references/problem-based/mip-recognition.md | markdown | 4.0 KB |
| references/problem-based/problem-based-framework.md | markdown | 6.1 KB |
| references/problem-based/problem-data-hygiene.md | markdown | 7.4 KB |
| references/problem-based/tax-aware-constraints.md | markdown | 5.0 KB |
| references/problem-based/trade-aux-variables.md | markdown | 5.7 KB |
references/problem-based/mip-recognition.md
View on GitHub# Recognize MIP Problems and Reject Anti-Patterns
Before picking a decomposition, decide the *class* of the problem. A
portfolio problem is a **mixed-integer program (MIP)** whenever it needs
integer or binary decision variables — not only for integer lots. Common
sources of integer variables in portfolio problems:
- **Integer lots or whole shares** — the position itself is integer.
- **Fixed per-trade or per-lot costs** — a binary "trade / no trade"
indicator to switch the cost on and off (`references/problem-based/trade-aux-variables.md`).
- **Cardinality constraints** — a binary selector per asset with `sum <= K`
(see also semi-integer bounds in `references/problem-based/disjunctive-big-m.md`).
- **Minimum trade size as a disjunction** — `|w - w0| >= δ` OR `w = w0`
needs a direction binary and big-M (`references/problem-based/disjunctive-big-m.md`).
- **Semi-integer / semi-continuous positions** — `w = 0` OR `L <= w <= U`
(`references/problem-based/disjunctive-big-m.md`).
- **Tax-lot decisions** — a binary "sell this lot / do not sell this lot"
or an integer share count from a lot (`references/problem-based/tax-aware-constraints.md`).
- **Wash-sale coupling** — a binary buy indicator that must go to zero when
the associated loss lot is sold (`references/problem-based/tax-aware-constraints.md`).
**The right pattern.** Use `optimvar(..., "Type", "integer")` for the
integer decision variable and let `solve` dispatch — `intlinprog` for MILP,
`ga` for MIQP.
**Prefer linear reformulations over MINLP.** When the "nonlinearity" is
`abs()`, `max()`, disjunction, or a fixed cost on a positive activity, a
linear reformulation almost always exists via auxiliary variables and
big-M constraints. Reach for `ga` only when the nonlinearity is real
(nonconvex utility, nonlinear risk metric) — it is slow, non-deterministic,
and gives no optimality guarantee.
## Three MIP anti-patterns to reject
Three common ways of avoiding the standard MIP pattern. All three yield
wrong or dominated answers.
### (a) Signed decision variable with `abs()` in the objective
A signed integer lot variable with `abs()` in the objective (typically to
express a symmetric or fixed commission) forces the problem into MINLP —
nonlinear only because of the formulation choice. `solve` dispatches to
`ga` and every solve is non-deterministic.
```matlab
% Anti-pattern: k is a signed integer, positive = buy, negative = sell
k = optimvar("k", nAssets, Type="integer", LowerBound=-Kmax, UpperBound=Kmax);
commission = fcn2optimexpr(@(k) 5*sum(abs(k)), k); % ✗ nonlinear -> ga
prob.Objective = mu' * (price .* (h0 + lotSize*k)) - commission;
```
**Fix.** Buy/sell aux split (`references/problem-based/trade-aux-variables.md`). Two non-negative integer aux vars,
`commission` becomes linear, dispatch drops to `intlinprog`.
### (b) Continuous relaxation + rounding
A continuous `Portfolio` + `estimateFrontier` + `round` on the output. The
rounded point is not the efficient frontier of the discrete problem — it
is commonly Pareto-dominated by other integer allocations reachable by lot
swaps. Greedy "repair" heuristics restore feasibility, not optimality.
```matlab
% Anti-pattern — TEMPLATE — not executable
p = Portfolio(AssetMean=mu, AssetCovar=Sigma);
p = setDefaultConstraints(p);
pwgt = estimateFrontier(p, 10);
lots = round(V0 * pwgt ./ (lotSize * price)); % ✗ not the frontier
```
**Fix.** Formulate the discrete problem directly with
`optimvar(..., "Type", "integer")` and call `solve` (`references/problem-based/trade-aux-variables.md`).
### (c) Hand-rolled branch-and-bound
A DIY branch-and-bound driver — priority queue, `solveRelax` wrapping
`quadprog`, per-point warm-start plumbing — instead of `optimproblem` +
integer `optimvar`. Redundant with `intlinprog`/`ga` and error-prone.
**Fix.** `solve` dispatches to `intlinprog` and `ga` automatically when
integer variables are present. Both are correctness-hardened.
----
Copyright 2026 The MathWorks, Inc.
----
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementation | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirements | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |