-
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
-
Diversification Part 2
Solution
stocks10 = pdr.get_data_yahoo(stocks[:10], start, end)["Adj Close"]
stocks10 = stocks10/stocks10.iloc[0,:]
stocks10 = stocks10.sum(axis=1)/10
print(stocks10)
Notice you can feed an array of stocks like we did in the first line. Also, the way I went about this problem was to first normalize each stock, then add them (axis=1 means add across the rows) and divide by 10 since there were 10 stocks.
stocks10 = stocks10.pct_change().dropna()
print((np.var(stocks10,ddof=1)*252)**.5)
Once we add a few stocks the standard deviation drops from .26 to .17. Time to try this with 50 stocks. We need to use retry_count=10 because sometimes, as you may have noticed, panads data-reader can fail. This will make sure we try it a few times before we give up.
stocks50 = pdr.get_data_yahoo(stocks[:50], start, end,retry_count=10)["Adj Close"]
stocks50 = stocks50/stocks50.iloc[0,:]
stocks50 = stocks50.sum(axis=1)/10
stocks50 = stocks50.pct_change().dropna()
print((np.var(stocks50,ddof=1)*252)**.5)
Now we have an even better standard deviation!
You can’t get rid of all risk, there will always be something called systematic risk which represents the risk of the entire market as a whole. The way diversification works is that as you increase the number of stocks you lower your non-systematic risk until eventually all you have left is systematic risk.
Source Code