Introduction

You have a sum of money and a menu of assets — stocks, bonds, a bit of gold. The question is deceptively simple: how much should go into each one? Pour everything into the asset with the highest expected return and one bad year wipes you out. Spread it too thin and you barely grow at all.

In 1952 a young economist named Harry Markowitz reframed the whole puzzle. Don't chase return alone, he said — track two numbers at once: the portfolio's expected return and its risk, measured as the variance of its outcomes. The magic is that risk isn't just the average of the parts: when assets don't move in lockstep, mixing them cancels out some of the wobble. Diversification is mathematics, not folklore.

That insight earned Markowitz the Nobel Memorial Prize in Economic Sciences in 1990. And the best part for us: the question of the best mix turns out to be one of the easy problems in computing.

Trace the Frontier

Below are three assets, each with its own expected return and risk, and they don't all move together. Drag the target return slider: for every target, the demo computes the mix of weights that achieves it with the least possible risk, and plots that point.

<p class="hint">{{hint}}</p>
<div class="wrap">
  <canvas id="plot" width="360" height="300"></canvas>
  <div class="panel">
    <label>{{label_target}} <b id="tval">8.0%</b></label>
    <input id="target" type="range" min="40" max="150" value="80">
    <div class="row"><span class="dot a"></span>{{asset_a}} &nbsp;<span id="wa">0%</span></div>
    <div class="row"><span class="dot b"></span>{{asset_b}} &nbsp;<span id="wb">0%</span></div>
    <div class="row"><span class="dot c"></span>{{asset_c}} &nbsp;<span id="wc">0%</span></div>
    <div class="risk">{{label_risk}} <b id="rval">–</b></div>
    <button id="trace" type="button">{{btn_trace}}</button>
  </div>
</div>
* { box-sizing: border-box; }
body { font-family: system-ui, sans-serif; color: #222; margin: 0; }
.hint { font-size: .88rem; color: #444; margin: 0 0 .7rem; line-height: 1.45; }
.wrap { display: flex; gap: 1rem; flex-wrap: wrap; align-items: flex-start; }
canvas { background: #f7f9fb; border: 1px solid #d6dee6; border-radius: 8px; }
.panel { flex: 1; min-width: 180px; }
label { font-size: .9rem; font-weight: 600; display: block; margin-bottom: .3rem; }
input[type=range] { width: 100%; }
.row { font-size: .92rem; margin: .35rem 0; display: flex; align-items: center; }
.dot { width: 12px; height: 12px; border-radius: 50%; display: inline-block; margin-right: .4rem; }
.dot.a { background: #e63946; } .dot.b { background: #1d3557; } .dot.c { background: #2a9d8f; }
.risk { font-size: .95rem; margin: .6rem 0; }
button { font: 600 14px system-ui, sans-serif; padding: .45rem .9rem; border: 1px solid #1d3557;
         background: #1d3557; color: #fff; border-radius: 8px; cursor: pointer; margin-top: .3rem; }
// Code not found

Sweep the slider from low to high and the minimum-risk points trace a smooth curve — the efficient frontier. Every portfolio on it is optimal: you cannot get more return without taking on more risk. Notice there's no guessing and no getting stuck: because the problem is convex, there is a single best answer for each target, and the computer finds it instantly. Compare that with the Traveling Salesman Problem, where finding the true optimum can take forever.

The Real Complexity

How hard is it to find the best portfolio? For the classic Markowitz setup, wonderfully easy.

  • The model. Choose weights ww that sum to 1 to minimize the portfolio variance wΣww^\top \Sigma w (Σ\Sigma is the covariance matrix) subject to hitting a target expected return μw=r\mu^\top w = r. That objective is a quadratic form, and because a covariance matrix is positive semidefinite, the function is convex.
  • Convex = solvable. Convex problems have no bad local minima: any minimum you reach is the global one. A convex quadratic program like this is solved in polynomial time by interior-point methods — for a handful of assets it's instant; for thousands it's routine.
  • Status: solved. This is not an open question or an intractable one. The theory was laid down by Harry Markowitz in 1952 and honored with the 1990 Nobel Prize in Economics. The efficient frontier is computed exactly, every day, in every quant fund.
  • But add the wrong constraint… Demand "hold at most 10 of these 500 stocks," or "buy in whole lots," and the variables become discrete. The problem leaves the convex world and becomes a mixed-integer quadratic program — NP-hard, in the same league as P vs NP.

That's the real lesson: the boundary between easy and hard is razor-thin. Continuous weights give you a convex problem and an instant answer; integer choices about which assets to even hold can blow the difficulty up exponentially.

Where It Matters

"Balance reward against risk across many choices" is one of the most useful shapes a problem can take, and Markowitz gave it a computable form:

  • Asset management: pension funds, endowments and mutual funds set their stock/bond/alternative mix on the efficient frontier.
  • Robo-advisors: services like automated wealth managers solve a mean-variance problem behind every "moderate" or "aggressive" risk profile you pick.
  • Risk budgeting: banks and insurers allocate capital so no single exposure dominates — the same variance-minimizing logic.
  • Beyond finance: any resource allocation under uncertain payoffs — energy grids, supply chains, even ad spend — borrows the mean-variance frame.

And because the heart of it is convex optimization, the toolkit reaches far past money. The same solvers crack problems like linear programming and underpin much of modern machine learning, where convexity is what makes training reliable.

Conclusion

Portfolio optimization is a rare and satisfying story in this collection: a real-world problem that turned out to be genuinely easy. By measuring risk as variance and demanding convexity, Markowitz made the search for the best mix into a problem a computer settles in a blink — and the efficient frontier you traced is its exact answer.

The cautionary note is just as elegant. Keep the weights continuous and you stay in the convex paradise; insist on holding whole shares or capping the number of holdings, and you tumble into P vs NP territory. Easy and hard live a single constraint apart — and knowing which side you're on is half the battle.

Share this article

Pick a channel — or use your device's native share sheet.

Comments

Loading comments...

https://www.kipuhub.com/en/article/portfolio-optimization/Content licensed under CC BY-NC 4.0.