A portfolio may be required to hold a specified number of assets. In an equal-weight model, choosing which assets to hold is a discrete decision: the selected assets all have the same weight, and every other asset has weight zero. The decision can be represented with a binary quadratic objective and one linear constraint.

From returns and covariance to binary decisions

Let x indicate the selected assets and require its entries to sum to k. With expected returns μ, covariance matrix Σ and risk-aversion coefficient q ≥ 0, the model minimizes

q(xTΣ x)/(k2) − (μTx)/k,   ∑i xi = k.

At q = 0, choose the k highest expected returns. As q increases, variance receives more weight; at a sufficiently large q, a finite model selects a minimum-variance portfolio (breaking ties by expected return). A weighted-sum sweep can reveal several risk-return trade-offs, but a discrete model can also contain nondominated portfolios that no weighted sum selects.

Construct the model

The symmetric matrix convention is E(x) = xTQx. Off-diagonal entries occur twice in this expression. Because binary variables satisfy xi2 = xi, the linear return term can be included on the diagonal.

import numpy as np
import fourcolors.qubo

# Illustrative inputs: expected returns and a positive diagonal covariance.
mu = np.array([0.10, 0.08, 0.05])
Sigma = np.diag([0.09, 0.04, 0.01])
n, k, q = len(mu), 2, 4.0
Q = q * Sigma / k**2 - np.diag(mu / k)
m = fourcolors.qubo.to_model(Q)
m.add_constraint({i: 1 for i in range(n)}, lo=k, hi=k)
r = fourcolors.qubo.solve(m, time_limit_s=5, seed=1)

This example uses the Python interface. Confirm the returned status before reading a solution, then recompute its cardinality, variance, return and objective from the original inputs.

An exact reference on a small model

The illustrative data above give a simple reference calculation. Enumerate all equal-weight pairs; their returns are the mean of the two returns, and their variances are one quarter of the sum of the selected variances because this example assumes zero cross-covariance.

from itertools import combinations
returns = [0.10, 0.08, 0.05]
variances = [0.09, 0.04, 0.01]
rows = [(p, sum(returns[i] for i in p)/2,
         sum(variances[i] for i in p)/4) for p in combinations(range(3),2)]
for q in (0,4):
    p, r, v = min(rows,key=lambda row:q*row[2]-row[1])
    assert p == ((0,1) if q == 0 else (1,2))

At zero risk aversion the two highest-return assets are selected. At the larger illustrative weight, the pair with the lowest variance has the lowest objective. In a general covariance model, correlations between selected assets also affect the choice.

Study the sampled trade-off

The portfolio example repository includes a synthetic data generator and a sweep over risk aversion. For a small instance, enumeration establishes the exact objective at each setting. A larger instance can use swap search as a heuristic reference. Agreement with that reference describes relative solution quality; independent feasibility and rescoring checks establish whether a returned assignment satisfies the model.

A plot of expected return against volatility should show the sampled portfolios as discrete points. Allocations between two plotted portfolios are not part of the discrete model.

For repeated selections, transaction costs and changes to the asset universe can also matter.

Explore the 4colors Optimizer for QUBO.