-
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
-
Creating the Line
The first way we are going to create the line of effecient frontiers is by looking at our simulation and seeing what the most effecient portfolio is at each standard deviation range.
We are going to use the function argmax() which returns the index of the largest value, we need to feed it only the returns slice of the numpy array.
m = portfolios[:,1].argmax()
print(m)
print(portfolios[m,:])
Now, what we are going to do is run a loop over each range. We can restrict the array by using a truth series like so, if we wanted to restrict our array to being only standard deviations less than .2.
print(portfolios[:,2]<.2)
print("The truth array telling us which values are under .2")
print(portfolios[portfolios[:,2]<.2])
print("The filtered array")
Let’s get the minimum and maximum standard deviation, and then get equally spaced increments. We can do this with np.linspace()
a = min(portfolios[:,2])
b = max(portfolios[:,2])
spaces = np.linspace(a,b,num=50)
print(spaces)
Now, let’s make the line!
bestPortfolios = []
for s in spaces:
sliced = portfolios[portfolios[:,2]<=s]
i = sliced[:,1].argmax()
bestPortfolios.append(sliced[i,:])
bestPortfolios = np.array(bestPortfolios)
print(bestPortfolios)
And plot it.
plt.scatter(portfolios[:,2],portfolios[:,1],c=portfolios[:,3],cmap=coolwarm)
plt.plot(bestPortfolios[:,2],bestPortfolios[:,1])
plt.xlim([.12,.3])
plt.ylim([.04,.075])
plt.xlabel("Standard Deviation")
plt.ylabel("Expected Return")
plt.title("Portfolio Sets")
plt.show()
Now comes the intense part, we are going to start using optimization. First define the standard deviation for a portfolio function.
def portfolioSTD(weights,matrix):
weights = np.mat(weights)
matrix = np.mat(matrix)
return ((weights*matrix*weights.transpose()).item()*252)**.5