All problems
Mean-Variance Weights
Problem
Compute the optimal two-asset mean-variance weights with a budget constraint.
For expected returns mu1, mu2, variances var1, var2, and risk aversion gamma, return [w1, 1 - w1] with w1 = (mu1 - mu2 + gamma * var2) / (gamma * (var1 + var2)). Return [0.5, 0.5] when the denominator is zero.
Hint
Higher risk aversion tilts the allocation away from the higher-variance asset.
Test cases
case 1
in: [0.1,0.05,0.04,0.01,2]
expected: [0.7000000000000001,0.29999999999999993]
case 2
in: [0.08,0.08,0.09,0.01,3]
expected: [0.1,0.9]
case 3
in: [0.05,0.1,0.04,0.04,1]
expected: [-0.12500000000000003,1.125]
Starter codePython
def mean_variance_weights(mu1, mu2, var1, var2, gamma):
# Returns [w1, w2]
# Your code here
pass3 test cases · run your code against them in the editor