Quant Memo
Advanced

Cardinality and Sparsity Constraints

Sometimes a portfolio needs to hold at most 30 names, not because the optimizer thinks 500 is wrong, but because operations, compliance, and monitoring only have bandwidth for 30. Forcing that count directly turns a smooth problem into a combinatorial one.

Prerequisites: Short-Sale Constraints in Portfolio Optimization, Regularization as a Prior

A pension fund's mean-variance optimizer, given 500 candidate stocks, might output non-zero weights in all 500 of them, most tiny. That portfolio is optimal on paper and unmanageable in practice: every position needs monitoring, every trade costs money to execute, and a fund holding 500 names at an average of 0.2% each has effectively index-hugged its way out of any real conviction. The desk wants at most, say, 30 names. That is not a preference an ordinary optimizer can express.

The analogy before any symbols

Packing a carry-on bag for a trip is easy if you can bring anything; the problem becomes hard the moment there is a hard limit of, say, seven items. Now you are not just choosing what is useful, you are choosing which useful things to leave out, and the two decisions interact: leaving out the umbrella changes whether the raincoat earns its slot. A cardinality constraint on a portfolio is exactly this seven-item suitcase problem, applied to hundreds of stocks instead of clothes.

The mechanics

A standard mean-variance problem minimizes wΣwημww^\top \Sigma w - \eta \, \mu^\top w subject to 1w=1\mathbf{1}^\top w = 1 (and usually w0w \geq 0). A cardinality constraint adds a cap on how many weights may be nonzero:

w0K,\|w\|_0 \leq K,

where w0\|w\|_0 counts the number of nonzero entries and KK is the maximum number of names allowed, say 30. In words: at most KK assets may be held at all; the rest must be exactly zero, not just small. This "counting" function is not smooth or convex, so it cannot be handed to a standard quadratic-program solver the way a variance term can; choosing which KK assets is a combinatorial decision, and with 500 candidates and K=30K=30 there are more possible subsets than atoms worth checking one by one.

Two practical workarounds exist. The exact route uses mixed-integer optimization: attach a binary variable zi{0,1}z_i \in \{0,1\} to each asset meaning "held or not," force wiziw_i \leq z_i, and cap ziK\sum z_i \leq K. This is solvable for hundreds of assets with modern solvers but scales badly beyond a few thousand. The approximate route relaxes w0\|w\|_0 into the 1\ell_1 penalty λw1\lambda\|w\|_1, the same idea as lasso regularization: penalizing the sum of absolute weights pushes many of them to exactly zero as a side effect, without ever explicitly counting names, and it is fast because it stays convex.

unconstrained: all 40 nonzero many tiny positions, hard to manage K=8: rest forced to zero eight meaningful positions
Cardinality does not shrink every position a little; it zeroes most positions completely and lets the survivors carry more weight each.

Distribution · normal
-2.000.002.00μvalue →
Within ±1σ 68.3%mean μ 0.00std σ 1.00

Estimation noise is why cardinality constraints often help out-of-sample despite discarding "optimal" information: each additional tiny weight is itself an estimate with error, so a portfolio that keeps only the assets with the strongest, most reliably estimated signal, rather than spreading conviction thin across everything within a standard deviation of noise, can generalize better even though it looks less optimal in-sample.

Worked example: exact versus relaxed on six assets

Six assets, unconstrained minimum-variance weights: 22%, 3%, 19%, 4%, 31%, 21%. With K=3K=3, the exact mixed-integer solver checks combinations of three assets and finds that keeping assets 1, 3, and 5 (the three largest) and reoptimizing their weights among just those three gives variance only 4% higher than the six-asset optimum. The lasso relaxation, tuned so that λ\lambda shrinks three weights to exactly zero, happens to select the same three assets here and lands within 1% of the exact solution's variance, cheaply.

Worked example: when the relaxation disagrees with the exact answer

Now suppose two of the six assets are highly correlated with each other (0.95) and both have moderate individual value. The lasso penalty, applied evenhandedly, often keeps both correlated assets at reduced weight rather than picking one, because 1\ell_1 regularization does not "know" they are redundant. The exact cardinality solver, forced to choose only K=3K=3 total, recognizes that holding both correlated assets wastes a slot and instead picks one of them plus two more diversifying names, achieving lower variance for the same KK. This gap, roughly 6% worse variance for the lasso version in this case, is the price of using a convex proxy instead of solving the real combinatorial problem.

What this means in practice

Cardinality constraints are standard at long-short equity funds with position-count mandates, and at any desk where operational cost per name (data feeds, compliance review, execution relationships) is a real budget line, not just an abstraction. Exact solvers are used up to a few thousand assets with a time budget; beyond that, or for daily rebalancing where speed matters, the 1\ell_1 relaxation or clustering-based methods like Nested Clustered Optimization are the practical default.

A cardinality constraint caps the number of nonzero positions directly, which is combinatorial and hard to solve exactly at scale; an 1\ell_1 penalty approximates the same sparsity behavior while staying solvable, at the cost of sometimes keeping redundant, correlated names the exact solver would have dropped.

Do not treat the assets a cardinality-constrained optimizer drops as "worthless." They were dropped because a slot was scarce and something else used it more efficiently, which is a statement about the whole set jointly, not about any single asset in isolation. Rerunning the same optimization on a slightly different universe, or with KK changed by even one, can swap which specific names survive even when the objective value barely moves, so treat the identity of the selected names as far less stable than the achieved risk level.

Related concepts

Practice in interviews

Further reading

  • Bertsimas & Shioda (2009), Algorithm for Cardinality-Constrained Quadratic Optimization
  • Fastrich, Paterlini & Winker (2015), Cardinality versus q-norm constraints
ShareTwitterLinkedIn