Quick Overview

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.

Implement portfolio optimization simulation

Company: DRW

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

##### Question Given price-return time series in a DataFrame, simulate random portfolio weights, compute expected return, volatility, Sharpe ratio, and return the weight vector that maximizes the Sharpe ratio.

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.

You are building the core selection step of a portfolio optimization simulation (the kind a quant runs by sampling thousands of random portfolios). You are given the per-asset statistics derived from a price-return time series, plus a finite set of candidate portfolio weight vectors (these stand in for the randomly simulated portfolios). For each candidate you must compute its Sharpe ratio and return the single weight vector that maximizes it. For a weight vector w over n assets: - Portfolio expected return: R = sum_i( w[i] * mean_returns[i] ) - Portfolio variance: V = sum_i sum_j( w[i] * cov[i][j] * w[j] ) (the quadratic form w^T * cov * w) - Portfolio volatility: sigma = sqrt(V) - Sharpe ratio: S = (R - risk_free) / sigma Return the candidate weight vector with the highest Sharpe ratio. Inputs: - mean_returns: list of n floats, the expected return of each asset. - cov: an n x n covariance matrix (list of lists of floats), symmetric and positive semidefinite. - risk_free: a float, the risk-free rate to subtract from portfolio return. - candidates: a list of weight vectors; each is a list of n floats. Rules: - If a candidate has zero (or negative) variance, its Sharpe ratio is undefined; skip it. - If two candidates are tied, return the first one encountered (lowest index). - If no candidate has positive variance, return None / null. Return the winning weight vector exactly as it was passed in.

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

  1. 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.
  2. Sharpe ratio = (portfolio_return - risk_free) / sqrt(variance). Guard against zero/negative variance before taking the square root, and skip those candidates.
  3. Track the best Sharpe seen so far and only replace it on a strictly-greater value, so the earliest candidate wins ties.

Loading coding console...