Skills DirectorySkills Directory
SkillsLearnSecurityCategoriesDocsBlogPro
Sign InSubmit Skill
Skills Directory

Security-tested agent skills for Claude, coding agents, and AI workflows.

Directory

  • Browse Skills
  • All Skills A–Z
  • Claude Skills
  • Claude Code Skills
  • Agent Skills
  • Categories
  • Authors
  • Submit a Skill

Learn

  • Learn Hub
  • Install Claude Skills
  • Write SKILL.md
  • Skills vs MCP
  • Directories Compared

Security

  • Security
  • Methodology
  • Secure Claude Skills
  • Security Badges
  • Chrome Extension
  • Skill Manager

Company

  • About
  • Community
  • Blog
  • API Docs
  • Advertise

2026 Skills Directory. All rights reserved.

ProTermsPrivacyRefunds
Back to skills

Matlab Optimize Portfolio

ASecurity

Help users formulate and solve portfolio optimization problems using Financial Toolbox's Portfolio object. Covers mean-variance (Markowitz), maximum Sharpe ratio (tangency), and efficient frontier workflows. Use when users ask about portfolio optimization, Markowitz, efficient frontier, Sharpe ratio, or attempt to use generic solvers (quadprog, fmincon, ga, problem-based optimize) for portfolio problems.

1,122 stars
0 votes
0 copies
1 views
Added 9/22/2026
ai-agentsapi

Works with

api

Security Analysis

A100/100

Pro scans all 6 files and shows the line behind each finding

Scanned 9/25/2026

$npx -y skills add matlab/matlab-agentic-toolkit --skill matlab-optimize-portfolio --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Matlab Optimize Portfolio?

Add the live security badge to your README — it updates automatically with every re-scan.

Security grade badge for Matlab Optimize Portfolio
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/matlab-matlab-optimize-portfolio/badge)](https://www.skillsdirectory.com/skills/matlab-matlab-optimize-portfolio)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
Files
SKILL.md
---
name: matlab-optimize-portfolio
description: Help users formulate and solve portfolio optimization problems using Financial Toolbox's Portfolio object. Covers mean-variance (Markowitz), maximum Sharpe ratio (tangency), and efficient frontier workflows. Use when users ask about portfolio optimization, Markowitz, efficient frontier, Sharpe ratio, or attempt to use generic solvers (quadprog, fmincon, ga, problem-based optimize) for portfolio problems.
license: https://www.mathworks.com/content/dam/mathworks/license/pmrl/license.md
metadata:
  author: MathWorks
  version: "1.0"
---

# Portfolio Optimization with Financial Toolbox

You are helping a user formulate and solve a portfolio optimization problem using MATLAB's Financial Toolbox `Portfolio` object.

## When to Use

- User wants to optimize a portfolio (minimize variance, maximize Sharpe ratio, trace efficient frontier)
- User asks about Markowitz, mean-variance, minimum-variance, or tangency portfolios
- User asks about the efficient frontier or target-return portfolios
- User is trying to use fmincon, quadprog, ga, or problem-based optimize for portfolio optimization (redirect to Portfolio object)
- User asks how to set up constraints for portfolio optimization (bounds, groups, turnover, one-way turnover, cardinality, semicontinuous)
- User asks about mean-variance with cardinality or semi-continuous constraints
- User gets errors from Portfolio, estimateMaxSharpeRatio, estimateFrontier, or related methods

## When NOT to Use

- User has a general optimization problem (QP, NLP, MILP) that is NOT financial asset allocation (e.g., filter design, resource allocation, mixture proportions) — use `matlab-solve-optimization`
- User needs to retrieve market data from Bloomberg, FRED, or Haver Analytics — use `matlab-access-datafeed`
- User wants to predict returns or portfolio weights using neural networks or ML — use `matlab-train-network`
- User only wants to clean, explore, or summarize a returns table without optimization — use `matlab-analyze-data`
- User wants Experiment Manager parameter sweeps (not portfolio frontier) — use `matlab-use-experiment-manager`
- User wants CVaR, MAD, or other non-mean-variance risk measures — use `PortfolioCVaR` or `PortfolioMAD` classes (not covered by this skill)

## Key Principle

**Always use the `Portfolio` class** — never let users manually 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.

## 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) | [formulation-mean-variance.md](references/formulation-mean-variance.md) |
| Minimize risk for a given target return | Mean-variance (target-return) | [formulation-mean-variance.md](references/formulation-mean-variance.md) |
| Trace the efficient frontier | Mean-variance (frontier) | [formulation-mean-variance.md](references/formulation-mean-variance.md) |
| Maximize risk-adjusted return (Sharpe ratio) | Max Sharpe / tangency | [formulation-max-sharpe.md](references/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 [reference-core.md](references/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 [reference-core.md](references/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

- [reference-core.md](references/reference-core.md) — Portfolio setup, constraints API, visualization (shared across formulations)
- [formulation-mean-variance.md](references/formulation-mean-variance.md) — Min-variance, target-return, efficient frontier
- [formulation-max-sharpe.md](references/formulation-max-sharpe.md) — Max Sharpe ratio / tangency portfolio
- [examples.md](references/examples.md) — Complete runnable examples for all formulations

----

Copyright 2026 The MathWorks, Inc.

----

Attribution

matlabmatlab
View sourceSee grades on GitHubMore from matlab →
SSkills DirectorySkills Directory

Ship a skill? Prove it's safe.

Free 120-pattern security scan, letter grade, and an embeddable README badge.

Submit a skill

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments (0)

No comments yet. Be the first to comment!

SSkills DirectorySkills Directory

Ship a skill? Prove it's safe.

Free 120-pattern security scan, letter grade, and an embeddable README badge.

Submit a skill

Related Skills

Caveman

Terse caveman voice: answer first, fluff gone, every technical fact kept. Use for /caveman, "caveman mode", "talk like caveman", "be brief", "less tokens". Stays on until "stop caveman" or "normal mode".

1100021 votes

Hyperplan

Adversarial multi-agent planning skill. Self-orchestrates 5 hostile category members (unspecified-low, unspecified-high, deep, ultrabrain, artistry) via team-mode for ruthless cross-critique debate, distills only the defensible insights, then MANDATORILY hands the distilled insight bundle to the `plan` agent for executable plan formalization. Use when planning needs maximum rigor and surfacing of weak assumptions, blind spots, and over-engineering. Triggers: 'hyperplan', 'hpp', '/hyperplan', ...

698621 votes

Writing Skills

Create and manage Claude Code skills in HASH repository following Anthropic best practices. Use when creating new skills, modifying skill-rules.json, understanding trigger patterns, working with hooks, debugging skill activation, or implementing progressive disclosure. Covers skill structure, YAML frontmatter, trigger types (keywords, intent patterns), UserPromptSubmit hook, and the 500-line rule. Includes validation and debugging with SKILL_DEBUG. Examples include rust-error-stack, cargo-dep...

3931 votes

Mcp Code Execution

Routes multi-tool workflows through MCP servers for large datasets and pipelines. Use when Bash tool overhead is limiting throughput on data-heavy tasks.

3421 votes

catchup

Recovers the conversation and failed tool calls of a previous Codex, Amp, Claude Code, Antigravity, Cline, Copilot CLI, Cursor, DeepSeek Harness, Grok Build, Kimi, OpenCode, Pi Agent, or ZCode session. Use when the user says "catch up", "what did the last session do", "get me up to speed", "I switched agents", asks to recover/summarize a previous session before continuing, or asks to diagnose or report a catchup failure. Do NOT use for the current conversation, git history, or any non-agent log.

741 votes
View all in ai-agents →