Skip to content
DeepForgeby svx
All problems
HardTime Seriests-132

Maximum Sharpe Weights

Problem

Compute tangency portfolio weights for two assets by mean-variance optimization: raw weights are the inverse covariance matrix times the excess expected returns, then normalized to sum to 1. Inputs are expected returns mu1 and mu2, volatilities sigma1 and sigma2, correlation rho, and risk-free rate rf.

Return [0.5, 0.5] when the covariance matrix is singular or the raw weights sum to zero.

Hint

The tangency portfolio maximizes the Sharpe ratio, not the raw return.

Test cases

case 1
in: [0.1,0.2,0.2,0.3,0,0]
expected: [0.5294117647058822,0.47058823529411775]
case 2
in: [0.1,0.1,0.2,0.3,0.5,0]
expected: [0.857142857142857,0.142857142857143]
case 3
in: [0.05,0.15,0.2,0.3,0,0.02]
expected: [0.34177215189873417,0.6582278481012659]
case 4
in: [0.1,0.2,0.2,0.3,1,0]
expected: [0.5,0.5]
Starter codePython
def max_sharpe_weights(mu1, mu2, sigma1, sigma2, rho, rf):
    # Your code here
    pass

4 test cases · run your code against them in the editor