Solution
Solution
def twoChoice2(ret1,ret2,var1,var2,cov,goalVar):
varEquation = sympy.Eq(goalVar,p1**2*var1+p2**2*var2+2*p1*p2*cov)
totalPercent = sympy.Eq(1,p1+p2)
dictionaries = sympy.solve((varEquation,totalPercent))
best = [1,0,0]
for dictionary in dictionaries:
percent1 = dictionary[p1]
percent2 = dictionary[p2]
ret = percent1*ret1+percent2*ret2
if ret>best[2]:
best = [percent1,percent2,ret]
print("Put "+str(best[0]*100)+"% in asset 1.")
print("Put "+str(best[1]*100)+"% in asset 2.")
print("Your return will be "+str(best[2]))
twoChoice2(.03,.09,.05,.07,.03,.045)
Source Code