Forum Sign in Register

Portfolio Optimization in MATLAB: The Portfolio Object, the Efficient Frontier and Markowitz Done Right

Started by Support 19 hours ago · 0 replies RSS

Portfolio Optimization in MATLAB: The Portfolio Object and the Efficient Frontier

Once you trade more than one strategy or instrument, "how much of each?" becomes a bigger driver of your results than any single entry signal. That question — allocation — is what Markowitz's mean-variance framework answers, and MATLAB's Financial Toolbox ships a purpose-built Portfolio object that handles the whole workflow: estimating inputs, imposing real-world constraints, tracing the efficient frontier and picking a portfolio off it. This guide walks through the workflow and, just as importantly, the ways it goes wrong.

Mean-variance in one paragraph

Every asset (or strategy) is summarized by its expected return and its co-movement with everything else — the covariance matrix. For a target level of risk there is one combination of weights with the highest expected return; the curve of all such combinations is the efficient frontier. Diversification is the free lunch here: two mediocre, weakly correlated return streams often combine into something better than either alone. The math is eighty years old; the difficulty is entirely in the inputs.

The core workflow


% returns: T-by-N matrix of daily strategy/asset returns
p = Portfolio('AssetList', names);
p = estimateAssetMoments(p, returns); % mean vector + covariance from history
p = setDefaultConstraints(p); % fully invested, long-only

wFrontier = estimateFrontier(p, 20); % 20 points along the frontier
[risk, ret] = estimatePortMoments(p, wFrontier);
plotFrontier(p, 20);

wSharpe = estimateMaxSharpeRatio(p); % the tangency portfolio


Three lines of setup and the optimizer is running. The interesting work is in the constraints, which is where the object earns its keep:

  • Bounds: setBounds(p, 0.02, 0.25) keeps any single sleeve between 2% and 25% — the single most effective guard against the optimizer going all-in on whatever backtested best.
  • Groups: setGroups caps exposure by category (e.g. "all trend-following robots together ≤ 50%").
  • Turnover and costs: setTurnover and setCosts stop the "optimal" rebalance from being an expensive round-trip through the spread every week.


Where mean-variance breaks in practice

Expected returns are the weak link. The optimizer is exquisitely sensitive to the mean vector, and historical means are the noisiest estimate in finance. Feed it last year's averages and it will lever up whatever was recently lucky — this is how mean-variance becomes an error-maximizing machine. Practical mitigations: shrink your return estimates toward a common value, work from longer histories than feel natural, or sidestep expected returns entirely and run minimum-variance or risk-based weightings, which need only the covariance matrix.

The covariance matrix is unstable too. With N assets you are estimating N(N+1)/2 parameters; with short histories the sample matrix is noise wearing a suit. Shrinkage estimators (Ledoit–Wolf style, via covarianceShrinkage or your own blend toward a diagonal target) make frontiers dramatically more stable out of sample.

Correlations are regime-dependent. The diversification you measured in a calm market is precisely what disappears in a stressed one, when correlations lurch toward 1. Stress-test any allocation against crisis-period covariance before trusting it, and treat the frontier as a planning tool, not a guarantee.

Validate like a backtest, because it is one. Choosing weights on the same data you evaluate them on is in-sample optimization, with all the usual self-deception. Walk it forward: estimate on a trailing window, hold the weights for a period, roll on, and compare the result against the boring benchmark of equal weighting. If your optimized portfolio cannot beat 1/N out of sample — a disturbingly common outcome — the extra machinery is not paying rent.

Beyond variance

Variance punishes upside and downside alike, and returns from trading strategies are rarely symmetric. The same toolbox offers PortfolioCVaR (optimize on conditional value-at-risk, i.e. the average of the worst tail) and PortfolioMAD (mean absolute deviation) with a nearly identical API, so switching risk measures is a one-line change. For allocating across your own robots, CVaR's focus on tail losses usually matches how you actually experience risk — as drawdowns, not day-to-day wiggle.

Bottom line

The Portfolio object turns a graduate-level optimization problem into a handful of readable lines, and that convenience is exactly why discipline matters: constrain the weights, shrink the estimates, stress the correlations and validate walk-forward. Do that, and mean-variance becomes what it should be — a systematic way to size a stable of strategies, instead of a very confident way to overfit history.

Sign in to reply.