-
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
-
Tangency Portfolio/Capital Market Line
Solution
con1 = {'type': 'eq',
'fun': lambda x: sum(x)-1}
con2 = {'type': 'ineq',
'fun': lambda x: .25-(portfolioSTD(x,matrix))}
ar = scipy.optimize.minimize(lambda x: -portfolioReturn(x,rets),x0=[np.zeros(10)],bounds=[(0,1)]*10,constraints=[con1,con2])["x"]
print(ar)
print(portfolioSTD(ar,matrix))
print(portfolioReturn(ar,rets))
Our second constraint limits the maximum value of the standard deviation to .25 since the constraint needs to be greater than or equal to 0.
Now, let’s put it all together. We are going to first create an array with the minimum variance portfolio as the first points, then we will iterate through the linspace optimizing with the restraint of the standard deviation at that point. We will also iterate through the best portfolios array and use them as the starting point for our optimization.
startAr = scipy.optimize.minimize(lambda x: portfolioSTD(x,matrix),x0=[np.zeros(10)],bounds=[(0,1)]*10,constraints=con1)["x"]
effecientLine = [[startAr,portfolioSTD(startAr,matrix),portfolioReturn(startAr,rets)]]
i = 0
con1 = {'type': 'eq',
'fun': lambda x: sum(x)-1}
for std in spaces:
startAr = bestPortfolios[i,0]
con2 = {'type': 'ineq',
'fun': lambda x: std-(portfolioSTD(x,matrix))}
x = scipy.optimize.minimize(lambda x: -portfolioReturn(x,rets),x0=startAr,bounds=[(0,1)]*10,constraints=[con1,con2])["x"]
effecientLine.append([x,portfolioSTD(x,matrix),portfolioReturn(x,rets)])
i+=1
effecientLine = np.array(effecientLine)
Plotting it…..
plt.scatter(portfolios[:,2],portfolios[:,1],c=portfolios[:,3],cmap=coolwarm)
plt.plot(effecientLine[:,1],effecientLine[:,2])
plt.ylim([.04,.075])
plt.xlabel("Standard Deviation")
plt.ylabel("Expected Return")
plt.title("Portfolio Sets")
plt.show()
Our final task is to find the best sharpe ratio on this line. This point will create the tangency portfolio because it’s slope will be high enough that it will touch no other point in the line. First, we are going to use the line to find the best sharpe ratio, then optimize that as the starting point, and finally plot it.
sharpeRatio = (effecientLine[:,2]-.02)/effecientLine[:,1]
print(sharpeRatio)
i = sharpeRatio.argmax()
print("The best starting point is.....")
print(effecientLine[i,:])
start = effecientLine[i,0]
To maximize the sharpe ratio, we only need the first constraint, standard deviation only matters in the equation we are maximizing.
x = scipy.optimize.minimize(lambda x: -(portfolioReturn(x,rets)-.02)/portfolioSTD(x,matrix),x0=start,bounds=[(0,1)]*10,constraints=[con1])["x"]
print(x)
print(portfolioSTD(x,matrix))
print(portfolioReturn(x,rets))
best = [x,portfolioSTD(x,matrix),portfolioReturn(x,rets)]
We will add the best as a dot.
plt.scatter(portfolios[:,2],portfolios[:,1],c=portfolios[:,3],cmap=coolwarm)
plt.plot(effecientLine[:,1],effecientLine[:,2])
plt.ylim([.04,.075])
plt.plot(best[1],best[2],"ko")
plt.xlabel("Standard Deviation")
plt.ylabel("Expected Return")
plt.title("Portfolio Sets")
plt.show()
And the final part of this lesson. We are going to create a line. At the low end, with 0% in the portfolio, we get 2% the risk-free rate, on the other end if we have twice the portfolio we get twice the return and twice the risk but also pay 2% to get a loan for the money (in theory only, in reality you need to pay a higher rate to borrow).
bestLine = []
#0%
bestLine.append([0,.02])
#100%
bestLine.append([best[1],best[2]])
#200%
bestLine.append([best[1]*2,best[2]*2-.02])
#300%
bestLine.append([best[1]*3,best[2]*3-.02*2])
bestLine = np.array(bestLine)
print(bestLine)
At 300%, we end up making much more, but we also have to get a loan for the 200% we do not have to invest.
plt.scatter(portfolios[:,2],portfolios[:,1],c=portfolios[:,3],cmap=coolwarm)
plt.plot(effecientLine[:,1],effecientLine[:,2])
plt.ylim([.04,.075])
plt.plot(best[1],best[2],"ko")
plt.plot(bestLine[:,0],bestLine[:,1],"k")
plt.xlabel("Standard Deviation")
plt.ylabel("Expected Return")
plt.title("Portfolio Sets")
plt.show()
And there you have it, the tangency portfolio! You can get the best possible sharpe ratio at any level of risk by varying how much is in the portfolio versus how much is in the risk-free asset. This is called the capital market line, the line representing investing in the most effecient portfolio.
Source Code