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/portfolio-object/formulation-mean-variance.md
View on GitHub# Solve Mean-Variance Portfolio Problems
## Problem Statement
Minimize portfolio variance (risk) subject to constraints:
```
minimize w' * Σ * w
subject to sum(w) = 1 (fully invested)
w >= 0 (long-only, optional)
additional constraints (bounds, groups, etc.)
```
Optionally, with a target return constraint:
```
μ' * w >= targetReturn
```
## When This Formulation Applies
- User wants the **minimum-variance portfolio** (lowest risk, no return target)
- User wants to **minimize risk for a given target return**
- User wants to **trace the efficient frontier**
- User asks about "Markowitz optimization" or "mean-variance optimization"
- User is trying to use quadprog to minimize w'Σw
## Key Methods
### Frontier Endpoints: `estimateFrontierLimits`
Returns the portfolios at the endpoints of the efficient frontier.
```matlab
wMinVar = estimateFrontierLimits(p, 'Min'); % minimum-variance portfolio
wMaxRet = estimateFrontierLimits(p, 'Max'); % maximum-return portfolio
wBoth = estimateFrontierLimits(p); % [wMinVar, wMaxRet] (both columns)
```
- `'Min'` — the left-most point on the frontier (lowest risk)
- `'Max'` — the right-most point on the frontier (highest return)
- No argument — returns both as an N-by-2 matrix
### Target-Return Portfolio: `estimateFrontierByReturn`
Find the minimum-variance portfolio that achieves a specified target return.
```matlab
wTarget = estimateFrontierByReturn(p, targetReturn);
```
- Can pass a vector of target returns to get multiple portfolios at once
- Targets below the min-variance return or above the max return are infeasible: `estimateFrontierByReturn` warns and returns the endpoint portfolio (see Common Pitfalls)
### Target-Risk Portfolio: `estimateFrontierByRisk`
Find the maximum-return portfolio at a specified risk level.
```matlab
wTarget = estimateFrontierByRisk(p, targetRisk);
```
### Efficient Frontier: `estimateFrontier`
Compute equally-spaced portfolios along the entire efficient frontier.
```matlab
wFrontier = estimateFrontier(p, nPortfolios); % N-by-nPortfolios matrix
```
## Typical Workflow
```matlab
% 1. Setup
p = Portfolio('AssetMean', mu, 'AssetCovar', Sigma);
p = setDefaultConstraints(p);
% 2. Solve for minimum-variance portfolio
wMinVar = estimateFrontierLimits(p, 'Min');
% 3. Analyze
portRisk = estimatePortRisk(p, wMinVar);
portRet = estimatePortReturn(p, wMinVar);
fprintf('Risk: %.4f, Return: %.4f\n', portRisk, portRet);
% 4. Efficient frontier
wFrontier = estimateFrontier(p, 20);
plotFrontier(p, wFrontier);
```
## Relationship to Max Sharpe Ratio
The maximum Sharpe ratio portfolio lies on the efficient frontier — it is the point where a line from the risk-free rate is tangent to the frontier. If the user wants this specific point, use [formulation-max-sharpe.md](formulation-max-sharpe.md) instead.
## Common Pitfalls
1. **Using quadprog directly** — The Portfolio object handles the QP formulation internally. Users who build H, f, Aeq, beq matrices manually often get sign conventions or constraint setup wrong.
2. **Requesting infeasible target returns** — If `targetReturn` is outside the range [min-variance return, max-return], `estimateFrontierByReturn` warns (`InfeasibleTargetReturn`) and returns the endpoint portfolio instead, which does not meet the requested target. Check the feasible range first:
```matlab
wLimits = estimateFrontierLimits(p);
muRange = estimatePortReturn(p, wLimits); % [minReturn; maxReturn]
```
3. **Confusing risk and variance** — `estimatePortRisk` returns standard deviation (not variance). The Portfolio object works in std-dev space throughout.
----
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 |