Generated banner for Matlab Optimize Portfolio
Generated logo for Matlab Optimize Portfolio

Matlab Optimize Portfolio

MathWorksOfficialCreated by MathWorks

  • Skill
  • Tool integration
  • Business and productivity
  • BSD-2-Clause
  • No issues found

From the author

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.
— the description by MathWorks

AutoGineer summary

Written by AutoGineer from the skill’s own files, not by the author

This skill provides a structured approach to portfolio optimization using the MATLAB Financial Toolbox. It guides users to leverage the Portfolio object for tasks such as mean-variance analysis, efficient frontier computation, and maximizing the Sharpe ratio. By using built-in methods, users avoid the pitfalls of manual solver configuration and ensure robust, mathematically sound results for asset allocation problems.

What it does

  • Formulates mean-variance portfolio optimization problems.
  • Computes efficient frontiers and target-return portfolios.
  • Calculates the maximum Sharpe ratio (tangency) portfolio.
  • Manages complex constraints like cardinality and semicontinuous bounds.
  • Provides built-in visualization for efficient frontiers and optimal portfolios.
  • Calculates portfolio risk and return metrics automatically.

When to use it

  • When you need to perform mean-variance or Markowitz portfolio optimization.
  • When you want to find the tangency portfolio or maximize the Sharpe ratio.
  • When you need to trace an efficient frontier for a set of assets.
  • When you are struggling with manual solver implementations like fmincon or quadprog for asset allocation.
  • When you need to apply specific constraints such as turnover, groups, or cardinality to a portfolio.

Advantages

  • Reduces code complexity by using high-level object methods.
  • Ensures global optima by using specialized internal solver configurations.
  • Simplifies constraint management through dedicated API methods.
  • Provides reliable, built-in functions for risk and return analysis.
  • Prevents common errors associated with manual nonlinear objective function definitions.

Usage examples

“How do I find the minimum variance portfolio in MATLAB?”
The agent will show how to create a Portfolio object, set default constraints, and use estimateFrontierLimits(p, 'Min') to compute the minimum variance weights.
“I want to maximize my Sharpe ratio, but my manual fmincon code is failing.”
The agent will explain why the Portfolio object is preferred, show how to set the RiskFreeRate, and demonstrate the use of estimateMaxSharpeRatio(p).
“How can I plot the efficient frontier for my asset returns?”
The agent will provide a workflow to set asset moments, define constraints, compute the frontier using estimateFrontier(p, 20), and visualize it with plotFrontier(p, w).

Install

  1. Download the skill as a ZIP:

    https://mp-staging.autogineer.com/agent-skills/matlab/matlab-optimize-portfolio/download
  2. Unzip it into the cross-client skills folder of your project:

    mkdir -p .agents/skills && unzip matlab-optimize-portfolio.zip -d .agents/skills

Documentation by MathWorks

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...FormulationReference
Minimize portfolio risk (no return target)Mean-variance (min-variance)formulation-mean-variance.md
Minimize risk for a given target returnMean-variance (target-return)formulation-mean-variance.md
Trace the efficient frontierMean-variance (frontier)formulation-mean-variance.md
Maximize risk-adjusted return (Sharpe ratio)Max Sharpe / tangencyformulation-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 for all creation patterns. The most common:

From return statistics:

p = Portfolio('AssetMean', mu, 'AssetCovar', Sigma);

From historical returns:

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):

p = setDefaultConstraints(p);

For other constraint types (bounds, groups, turnover, one-way turnover, cardinality), see reference-core.md.

Step 5: Solve

Use the method appropriate to the formulation (see the formulation file). Common patterns:

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:

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:

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


Copyright 2026 The MathWorks, Inc.


Files

ATTRIBUTION.md

Attribution

This folder is an unmodified copy of the Agent Skill "matlab-optimize-portfolio", distributed by AutoGineer (https://autogineer.com/agent-skills).

The skill's own files are unmodified copies. AutoGineer is not affiliated with or endorsed by the author or the publisher.

Files added by AutoGineer

  • ATTRIBUTION.md: this file, written by AutoGineer

  • LICENSE: copied from LICENSE.md

Requirements and compatibility

Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta

“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.

Security

No issues found by our static scan· No issues found

A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.

Attribution and licence

Created by
MathWorks
Publisher
MathWorks (Official)
Source
matlab/matlab-agentic-toolkit · skills-catalog/computational-finance/matlab-optimize-portfolio · commit 2a7f215
Licence
BSD 2-Clause License (redistribution allowed)
  • LICENSE (added by AutoGineer from LICENSE.md)
  • ATTRIBUTION.md (written by AutoGineer)
Synced

AutoGineer is not affiliated with or endorsed by MathWorks.

Download ZIP

The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download matlab-optimize-portfolio.zip