Simulated Max-Sharpe Weights with Missing Histories
Problem statement
You are given a table of daily returns for $n$ assets and need a quick simulation-based allocator: draw many random long-only weight vectors, score each one, and keep the best. No finance background is needed; every formula is below.
Write a function that, given a $T \times n$ list returns (row = trading day, column = asset, None = no data that day), an annual risk-free rate risk_free, a number of draws n_portfolios, a seed and a per-asset cap max_weight:
- Estimates annual inputs (252 trading days a year). The expected annual return of asset $i$ is $\mu_i = 252 \cdot$ (mean of asset $i$'s non-missing daily returns). The annual covariance is $\Sigma = 252 \cdot S$, where $S_{ij}$ is the sample covariance (denominator $m - 1$) of assets $i$ and $j$ over the $m$ days on which BOTH are present (pairwise-complete, exactly what
pandas.DataFrame.cov()computes). Assets start trading on different days and have scattered gaps, so do not throw away a whole day because one asset is missing. - Simulates weights reproducibly. Draw
U = np.random.default_rng(seed).random((n_portfolios, n))and divide each row by its sum, so every weight is $\ge 0$ and each row sums to 1. Keep only rows whose largest weight is at mostmax_weight(tolerance $10^{-12}$), in their original order. - Scores every kept portfolio $w$: annual return $r = w^T \mu$, annual volatility $\sigma = \sqrt{w^T \Sigma w}$, and Sharpe ratio $(r - r_f) / \sigma$.
- Returns
[best_weights, best_return, best_volatility, best_sharpe, top5], where "best" is the kept portfolio with the highest Sharpe ratio (the earliest one on an exact tie),best_weightsis a list of $n$ floats, andtop5is the list of the up-to-5 highest Sharpe ratios in descending order. Return[]if no row survives the cap.
Use NumPy / pandas vectorization rather than a Python loop over portfolios.
def optimize_portfolio(returns: list, risk_free: float, n_portfolios: int,
seed: int, max_weight: float) -> list:
...
Constraints
- $2 \le n \le 6$, $20 \le T \le 250$; every asset has at least 20 non-missing days, and the pairwise covariance matrix is positive definite.
- $1 \le$
n_portfolios$\le 10^4$, $0 <$max_weight$\le 1$, $0 \le$risk_free$\le 0.5$. - Floats are compared with a relative tolerance of $10^{-6}$.
Example 1
returns = [[0.010, 0.002, None], [-0.004, 0.001, None], [0.006, -0.003, None],
[0.002, 0.004, 0.012], [-0.001, 0.000, -0.006], [0.008, 0.003, 0.009],
[None, 0.002, 0.004], [0.003, -0.001, -0.002]]
optimize_portfolio(returns, 0.02, 5, 7, 1.0)
-> [[0.4459, 0.2888, 0.2653], 0.6854, 0.0640, 10.4014,
[10.4014, 9.3487, 8.7475, 8.0913, 7.1293]] (rounded here)
The third asset only has data from day 4 and the first asset misses day 7; each mean and each covariance uses the days that are available for it. (Eight days of made-up data give an unrealistically high Sharpe ratio; the point is the mechanics.)
Example 2
optimize_portfolio(returns, 0.02, 4, 1, 0.34) -> []
With three assets a cap of 0.34 forces every weight into $[0.32, 0.34]$; none of the 4 draws is that even, so nothing survives.
Example 3
returns = [[0.01, 0.002], [0.0, 0.001], [0.004, -0.002],
[-0.003, 0.003], [0.006, 0.0], [0.002, 0.001]]
optimize_portfolio(returns, 0.0, 1, 3, 1.0)
-> [[0.2656, 0.7344], 0.3662, 0.0237, 15.4333, [15.4333]]Show hint
Which pandas operations already skip missing values per column, and which one handles each pair of columns separately?
Relevant roles: ML Engineer and Data Scientist.