-
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 a Portfolio Part 2
pd.concat() returns a dataframe of whatever we feed it put together. By default it puts these pieces together vertically, but using the argument axis=1 we can put the pieces together horizontally.
The plan is to create a dataframe with the two stocks each having a column. One task we need to do before this is renaming the series so that each column has a unique name (otherwise both series are named “close”).
import pandas as pd
ford = ford.rename("Ford")
toyota = toyota.rename("Toyota")
stocks = pd.concat([ford,toyota],axis=1)
print(stocks)
Now, let’s get the first elements in a new way. Using .iloc[] we can index based on integer positions like an array. The first argument is the row, the second is the column. So to get the first row (0), and all the columns we would do this.
print(stocks.iloc[0,:])
Let’s normalzie the data so that each row shows the cummulative return over time. To do this, we divide by the first row for each stock.
print(stocks/stocks.iloc[0,:])
Let’s also now multiply each return by the original investment, this let’s us see what the investment is worth at each time.
print(stocks/stocks.iloc[0,:]*[3000,7000])
Finally, let’s add in a column for the total value of the portfolio (the sum each day)
stocks = stocks/stocks.iloc[0,:]*[3000,7000]
stocks["Portfolio"]=stocks["Ford"]+stocks["Toyota"]
print(stocks)
To visualize this data, we can do stocks.plot.line(), we will also want to set 0 as the bottom of the graph with a y limit function.
stocks.plot.line()
plt.gca().set_ylim(bottom=0)
plt.show()
A new equation is the variance of a portfolio of two stocks.
Equation
p
= w
1
2
*var
1
+w
2
2
*var
2
+w
1
*w
2
*Cov
1,2
w
i
= Weight on Stock i
var
i
= Variance on Stock i
Cov
i,j
= Covariance between stock i and stock j
Let’s do a quick proof with our data.
print(np.var(stocks["Portfolio"],ddof=1)**.5)
var1 = np.var(stocks["Ford"],ddof=1)
var2 = np.var(stocks["Toyota"],ddof=1)
cov = np.cov(stocks["Ford"],stocks["Toyota"])[0][1]
w1=.3
w2=.7
print((w1**2*var1+w2**2*var2+2*w1*w2*cov)**.5)
Source Code