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.md
View on GitHub# Optimize with the Portfolio Object
Formulate and solve mean-variance portfolio problems with MATLAB's Financial
Toolbox `Portfolio` object.
## When to Use
The risk measure is variance (or standard deviation), the objective is
minimum variance, target return, mean-variance utility, or Sharpe ratio, and
**every** constraint is one the `Portfolio` object accepts:
- Long-only or long/short bounds (`setBounds`, `setDefaultConstraints`)
- Semi-continuous bounds — `w(i) = 0` OR `L(i) <= w(i) <= U(i)`
(`setBounds(..., 'BoundType', 'Conditional')`)
- Budget and self-financing (`setBudget`)
- Conditional budget (`setConditionalBudget`, R2024b and later) — caps total
exposure to assets whose individual weight is large
- Group constraints (`setGroups`, `setGroupRatio`)
- Linear equality / inequality constraints (`setEquality`, `setInequality`)
- Turnover and one-way turnover (`setTurnover`, `setOneWayTurnover`)
- Tracking-error constraints (`setTrackingError`)
- Cardinality (`setMinMaxNumAssets`)
- Proportional transaction costs (`setCosts` — proportional in `w-w0`)
Also use when the user gets errors from `Portfolio`, `estimateMaxSharpeRatio`,
`estimateFrontier`, or related methods.
## When NOT to Use
- Any constraint above is joined by one the `Portfolio` object cannot express
— integer lots, whole shares, fixed or per-lot commissions, minimum trade
size on `w-w0`, semi-integer positions, wash-sale, realized capital-gains
caps, tax-loss harvesting, a cap on total transaction cost — read
`references/problem-based.md`
- Visible code rounds the output of this workflow to whole shares or lots —
read `references/problem-based.md`
## Key Principle
For problems the `Portfolio` object can express, **always use the `Portfolio`
class** — never hand-code the optimization with `fmincon` or `quadprog`. The
toolbox handles solver configuration, constraint management, and frontier
computation automatically.
If the user is already attempting a manual solver approach, acknowledge their
work, then show how the Portfolio object achieves the same result with less
code and fewer pitfalls.
## Workflow
### Step 1: Identify the Formulation
Determine which problem the user is trying to solve:
| User wants to... | Formulation | Reference |
|------------------|-------------|-----------|
| Minimize portfolio risk (no return target) | Mean-variance (min-variance) | `references/portfolio-object/formulation-mean-variance.md` |
| Minimize risk for a given target return | Mean-variance (target-return) | `references/portfolio-object/formulation-mean-variance.md` |
| Trace the efficient frontier | Mean-variance (frontier) | `references/portfolio-object/formulation-mean-variance.md` |
| Maximize risk-adjusted return (Sharpe ratio) | Max Sharpe / tangency | `references/portfolio-object/formulation-max-sharpe.md` |
Consult the relevant formulation file for problem-specific guidance and methods.
### Step 2: Determine What Data the User Has
Ask (if not clear) whether they have:
- A matrix of historical asset returns (or prices that need converting)
- Pre-computed mean returns (mu) and covariance matrix (Sigma)
- A risk-free rate (relevant for Sharpe ratio; defaults to 0 if unspecified)
### Step 3: Create the Portfolio Object
See `references/portfolio-object/reference-core.md` for all creation patterns. The most common:
**From return statistics:**
```matlab
p = Portfolio('AssetMean', mu, 'AssetCovar', Sigma);
```
**From historical returns:**
```matlab
p = Portfolio;
p = setAssetMoments(p, mean(returns)', cov(returns));
```
### Step 4: Set Constraints
Always set constraints. At minimum, use default constraints (fully invested, long-only):
```matlab
p = setDefaultConstraints(p);
```
For other constraint types (bounds, groups, turnover, one-way turnover, cardinality), see `references/portfolio-object/reference-core.md`.
### Step 5: Solve
Use the method appropriate to the formulation (see the formulation file). Common patterns:
```matlab
wMinVar = estimateFrontierLimits(p, 'Min'); % minimum-variance
wTarget = estimateFrontierByReturn(p, targetRet); % target-return
wSharpe = estimateMaxSharpeRatio(p); % max Sharpe ratio
wFrontier = estimateFrontier(p, 20); % efficient frontier
```
### Step 6: Analyze and Visualize
Use built-in methods for portfolio statistics — never compute them manually:
```matlab
portRisk = estimatePortRisk(p, w);
portRet = estimatePortReturn(p, w);
[risk, ret] = estimatePortMoments(p, w);
```
Always use `plotFrontier` as the primary frontier visualization — add custom annotations (special portfolios, CAL line) with `hold on`/`hold off` afterward:
```matlab
wFrontier = estimateFrontier(p, 20);
plotFrontier(p, wFrontier);
hold on
[risk, ret] = estimatePortMoments(p, wSpecial);
plot(risk, ret, 'r*', 'MarkerSize', 12);
hold off
```
## Common Pitfalls
1. **Manual solver usage** — quadprog/fmincon for portfolio problems is error-prone; Portfolio handles it.
2. **Missing constraints** — A Portfolio without constraints is underdetermined.
3. **Redundant solves** — Pass weights to `plotFrontier`, not a number of portfolios, if you already solved.
4. **Manual risk/return formulas** — Use `estimatePortRisk`, `estimatePortReturn`, `estimatePortMoments`.
5. **Cardinality/semicontinuous constraints** — When calling `estimateMaxSharpeRatio`, specify `'Method','iterative'` (MATLAB auto-selects with a warning if omitted, but explicit is cleaner). Frontier methods (`estimateFrontier`, `estimateFrontierLimits`, `estimateFrontierByReturn`) auto-detect these constraints and select the mixed-integer solver internally — do NOT pass `'Method','iterative'` to them.
## Tone
Be direct and practical. Show working MATLAB code. If the user provides data, use their actual data. If not, use a small illustrative example so they can see the pattern and adapt.
## Reference Materials
- `references/portfolio-object/reference-core.md` — Portfolio setup, constraints API, visualization (shared across formulations)
- `references/portfolio-object/formulation-mean-variance.md` — Min-variance, target-return, efficient frontier
- `references/portfolio-object/formulation-max-sharpe.md` — Max Sharpe ratio / tangency portfolio
- `references/portfolio-object/examples.md` — Complete runnable examples for all formulations
----
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 |