Portfolio selection¶
An investor splits a budget among assets. Markowitz's model measures a portfolio by its expected return and its risk, the variance of the return. Higher returns come with higher risk, so there is no single best portfolio: the efficient frontier holds the best trade-offs, the highest return for each level of risk. The investor then picks a point on it.
Without other rules, the frontier comes from quadratic programming. Real portfolios have more rules, which make the problem hard for exact methods. Here, as in Chang, Meade, Beasley and Sharaiha (2000), the portfolio holds exactly 10 of the 31 assets of the Hang Seng data (OR-Library port1), each with at least 1 percent of the budget. This is a problem with two objectives, mixed variables and a combinatorial part: which 10 assets to hold.
The data ship with ypea as ypea.problems.port1(), with 2000 points of the unconstrained efficient frontier (any number of assets, no minimum share). No portfolio with 10 assets can beat it, so it is the reference.
The model¶
Two variables describe a portfolio:
assets: aSelectionof 10 distinct assets out of 31,share: 10 real numbers in [0, 1], one per held asset. The objective turns them into weights that sum to 1, each at least 1 percent. So every solution is a valid portfolio, and no constraints are needed.
The two objectives are the variance (minimized) and the expected return (maximized, so the objective returns its negative). The objective is vectorized and evaluates a whole population at once.
import numpy as np
import ypea
mean, cov, frontier = ypea.problems.port1() # 31 assets of the Hang Seng index
n_assets = 10 # assets in the portfolio
min_share = 0.01 # smallest share of the budget for a held asset
def risk_and_return(sol):
held = sol.assets # (n, 10): which assets each portfolio holds
share = sol.share # (n, 10): free shares in [0, 1]
# weights that sum to 1, each at least min_share
share = np.where(np.sum(share, axis=1, keepdims=True) > 0, share, 1.0) # all zero: equal shares
total = np.sum(share, axis=1, keepdims=True)
w = min_share + (1 - n_assets * min_share) * share / total
expected = np.sum(w * mean[held], axis=1)
held_cov = cov[held[:, :, None], held[:, None, :]]
variance = np.einsum("ni,nij,nj->n", w, held_cov, w)
return np.column_stack([variance, -expected]) # minimize the risk, maximize the return
problem = ypea.Problem(
[
ypea.Selection("assets", n=len(mean), k=n_assets),
ypea.Real("share", shape=n_assets, lb=0, ub=1),
],
risk_and_return,
n_obj=2,
vectorized=True,
name="port1",
)
result = ypea.NSGAII(max_nfe=100_000).solve(problem, seed=1)
print(result.summary())
NSGA-II on port1
front: 1537 solutions, 1537 feasible
objective ranges: [0.00065602, 0.00411486], [-0.0103074, -0.00301727]
evaluations: 100000, iterations: 999, time: 6.72 s, stopped by: max_nfe
Differs from Python: MATLAB draws its own random numbers, so the results differ from Python's. The selected assets are numbered from 1.
[mean_return, cov_matrix, frontier] = ypea.problems.port1(); % 31 assets of the Hang Seng index
n_assets = 10; % assets in the portfolio
min_share = 0.01; % smallest share of the budget for a held asset
problem = ypea.Problem( ...
vars={
ypea.Selection("assets", n=numel(mean_return), k=n_assets)
ypea.Real("share", shape=n_assets, lb=0, ub=1)
}, ...
objective=@(sol) risk_and_return(sol, mean_return, cov_matrix, n_assets, min_share), ...
n_obj=2, ...
vectorized=true, ...
name="port1");
result = ypea.NSGAII(max_nfe=100000).solve(problem, seed=1);
disp(result.summary())
function f = risk_and_return(sol, mean_return, cov_matrix, n_assets, min_share)
held = sol.assets; % (n, 10): which assets each portfolio holds
share = sol.share; % (n, 10): free shares in [0, 1]
% weights that sum to 1, each at least min_share
all_zero = sum(share, 2) == 0;
share(all_zero, :) = 1; % all zero: equal shares
total = sum(share, 2);
w = min_share + (1 - n_assets * min_share) * share ./ total;
expected = sum(w .* mean_return(held), 2);
n = size(held, 1);
variance = zeros(n, 1);
for i = 1:n
variance(i) = w(i, :) * cov_matrix(held(i, :), held(i, :)) * w(i, :)';
end
f = [variance, -expected]; % minimize the risk, maximize the return
end
NSGA-II on port1
front: 2577 solutions, 2577 feasible
objective ranges: [0.000655206, 0.00411934], [-0.0103153, -0.00319658]
evaluations: 100000, iterations: 999, time: 12.9 s, stopped by: max_nfe
NSGA-II finds 1537 portfolios on the front in under 10 seconds. The full script, examples/applications/portfolio.py, also measures the front and plots it.
Results¶
Five multi-objective algorithms, each with 10 seeded runs of 100 000 evaluations. The quality measure is the IGD (inverted generational distance) to the unconstrained frontier, with both objectives scaled to [0, 1] by the range of the frontier: the mean distance from the frontier's points to the nearest point found. Lower is better. It cannot reach 0, because the 10-asset front lies slightly below the unconstrained frontier.
| Algorithm | Best IGD | Mean IGD | Worst IGD | Mean front size |
|---|---|---|---|---|
| NSGA-II | 0.0107 | 0.0127 | 0.0158 | 2286 |
| NSGA-III | 0.0114 | 0.0178 | 0.0261 | 5159 |
| SPEA2 | 0.0101 | 0.0123 | 0.0155 | 2655 |
| MOEA/D | 0.0185 | 0.0258 | 0.0440 | 9873 |
| MOPSO | 0.0241 | 0.0427 | 0.0746 | 136 |
SPEA2 and NSGA-II give the best fronts, about 1 percent of the frontier's range away from it on average. MOPSO keeps a small archive and covers the front more thinly.
Try it on your own data¶
Replace mean and cov with the mean returns and the covariance matrix of your assets, and set n_assets and min_share. To limit each asset to a maximum share as well, add it as an inequality constraint. To choose one portfolio from the front, take for example the one with the best ratio of return to risk.