-
Return and Variance 7
-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
Lecture1.4
-
Lecture1.5
-
Lecture1.6
-
Lecture1.7
-
-
Solving Equations 5
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
Lecture2.4
-
Lecture2.5
-
-
Capital Allocation Line 6
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
Lecture3.4
-
Lecture3.5
-
Lecture3.6
-
-
Diversification 3
-
Lecture4.1
-
Lecture4.2
-
Lecture4.3
-
-
Investment Sets 3
-
Lecture5.1
-
Lecture5.2
-
Lecture5.3
-
-
Portfolios 7
-
Lecture6.1
-
Lecture6.2
-
Lecture6.3
-
Lecture6.4
-
Lecture6.5
-
Lecture6.6
-
Lecture6.7
-
-
Capital and Security Market Lines 3
-
Lecture7.1
-
Lecture7.2
-
Lecture7.3
-
-
Arbitrage 3
-
Lecture8.1
-
Lecture8.2
-
Lecture8.3
-
-
Dividend Discount Model 2
-
Lecture9.1
-
Lecture9.2
-
-
Fixed Income 4
-
Lecture10.1
-
Lecture10.2
-
Lecture10.3
-
Lecture10.4
-
-
Duration and Immunization 4
-
Lecture11.1
-
Lecture11.2
-
Lecture11.3
-
Lecture11.4
-
Solving Equations Part 2
Solution
p1 = sympy.Symbol("p1")
p2 = sympy.Symbol("p2")
def twoChoice(ret1,ret2,var1,var2,cov,goalReturn):
retEquation = sympy.Eq(goalReturn,p1*ret1+p2*ret2)
totalPercent = sympy.Eq(1,p1+p2)
dictionary = sympy.solve((retEquation,totalPercent))
percent1 = dictionary[p1]
percent2 = dictionary[p2]
variance = percent1**2*var1+percent2**2*var2+2*percent1*percent2*cov
print("Put "+str(percent1*100)+"% in asset 1.")
print("Put "+str(percent2*100)+"% in asset 2.")
print("Your variance will be "+str(variance))
twoChoice(.03,.09,.05,.07,.03,.06)
We could also solve instead for a variance we want by feeding the portfolio variance equation.
p1 = sympy.Symbol("p1")
p2 = sympy.Symbol("p2")
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)
dictionary = sympy.solve((varEquation,totalPercent))
return dictionary
twoChoice2(.03,.09,.05,.07,.03,.045)
You’re probably confused now, why is there two different weights that cause the same variance? This is because of the covariance term, it introduces non-linearity so in this case we can have more than one combination that causes the variance.
Let’s create a function that prints out the combinations.
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))
for dictionary in dictionaries:
percent1 = dictionary[p1]
percent2 = dictionary[p2]
ret = percent1*ret1+percent2*ret2
print("Put "+str(percent1*100)+"% in asset 1.")
print("Put "+str(percent2*100)+"% in asset 2.")
print("Your return will be "+str(ret))
twoChoice2(.03,.09,.05,.07,.03,.045)
Challenge
Modify the function so that it only returns the weights with the highest return.
Prev
Solving Equations
Next
Solution