Implement portfolio optimization simulation
Company: DRW
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
Quick Answer: This question evaluates proficiency in numerical simulation, portfolio optimization, time-series data processing, and computation of risk-return metrics such as expected return, volatility, and the Sharpe ratio.
Constraints
- 1 <= n (number of assets) <= 50
- cov is an n x n symmetric, positive semidefinite matrix
- Each candidate weight vector has length n
- 0 <= number of candidates <= 10000
- Weights, returns, covariances, and risk_free are real numbers (may be negative; weights need not be normalized)
- Skip any candidate whose portfolio variance is <= 0 (Sharpe undefined)
- Return the first (lowest-index) candidate on a tie; return None if no candidate has positive variance
Examples
Input: ([0.1, 0.2], [[0.04, 0.0], [0.0, 0.09]], 0.02, [[1.0, 0.0], [0.0, 1.0], [0.5, 0.5]])
Expected Output: [0.5, 0.5]
Explanation: All-A: return 0.1, vol 0.2, Sharpe (0.1-0.02)/0.2 = 0.40. All-B: return 0.2, vol 0.3, Sharpe (0.2-0.02)/0.3 = 0.60. 50/50: return 0.15, variance 0.25*0.04 + 0.25*0.09 = 0.0325, vol 0.1803, Sharpe (0.15-0.02)/0.1803 = 0.721. The diversified 50/50 portfolio has the highest Sharpe.
Input: ([0.05], [[0.01]], 0.0, [[1.0]])
Expected Output: [1.0]
Explanation: Single asset, single candidate: variance = 1*0.01*1 = 0.01, vol 0.1, Sharpe (0.05-0)/0.1 = 0.5. It is the only valid candidate, so it is returned.
Hints
- Portfolio variance is the quadratic form w^T * cov * w: a double loop sum over i and j of w[i] * cov[i][j] * w[j]. Don't forget the off-diagonal cross terms.
- Sharpe ratio = (portfolio_return - risk_free) / sqrt(variance). Guard against zero/negative variance before taking the square root, and skip those candidates.
- Track the best Sharpe seen so far and only replace it on a strictly-greater value, so the earliest candidate wins ties.