-
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
-
Manipulating Data
One cool thing about pandas dataframes is how many functions are built in. Let’s say we want daily returns, we can use .pct_change() as so.
print(stock["Adj Close"].pct_change())
Assign it to be a new series, which is similar to a dataframe except it is only one column. When we index with only one column we get a series pandas object.
dailyReturns = stock["Adj Close"].pct_change()
Notice the first row has NaN. This is because we don’t have the previous day data. Let’s get rid of it with another function.
dailyReturns = dailyReturns.dropna()
print(dailyReturns)
Now, let’s bring in numpy to so that we can find the newest measure, variance and standard deviation. Variance measures the volatility of data, in this case our returns. Standard deviation is equal to the square root of variance, both are used depending on the situation or equation.
import numpy as np
print(np.std(dailyReturns, ddof=1))
print(np.var(dailyReturns, ddof=1))
ddof represents delta degrees of freedom and is set to 1 since it is the version we are using for finance. You can read more about the difference between ddof=0 and ddof=1 online, but it isn’t directly important for us. Let’s prove the standard deviation and variance relationship.
print(np.std(dailyReturns, ddof=1))
print(np.var(dailyReturns, ddof=1)**.5)
We might also be interested in the daily return average, in which case we could find the mean.
print(np.mean(dailyReturns))
It is also not too hard to visualize the data, to plot a histogram, there’s a built in function.
import matplotlib.pyplot as plt
dailyReturns.plot.hist()
plt.show()
The default number of bins is 10, but you can get a different number by giving the argument bins = n
Challenge