-
Compound Interest Part 1 6
-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
Lecture1.4
-
Lecture1.5
-
Lecture1.6
-
-
Compound Interest Part 2 3
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
-
Present Value 4
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
Lecture3.4
-
-
Annuities 6
-
Lecture4.1
-
Lecture4.2
-
Lecture4.3
-
Lecture4.4
-
Lecture4.5
-
Lecture4.6
-
-
Perpetuities 2
-
Lecture5.1
-
Lecture5.2
-
-
Bonds 6
-
Lecture6.1
-
Lecture6.2
-
Lecture6.3
-
Lecture6.4
-
Lecture6.5
-
Lecture6.6
-
-
Dividend Discount Model 3
-
Lecture7.1
-
Lecture7.2
-
Lecture7.3
-
-
Risk 8
-
Lecture8.1
-
Lecture8.2
-
Lecture8.3
-
Lecture8.4
-
Lecture8.5
-
Lecture8.6
-
Lecture8.7
-
Lecture8.8
-
-
Capital Asset Pricing Model 6
-
Lecture9.1
-
Lecture9.2
-
Lecture9.3
-
Lecture9.4
-
Lecture9.5
-
Lecture9.6
-
Graphing Market Returns
Covariance¶
Covariance measures the way in which two things move together or opposite one and other. These values need to be paired up, so when we look at stock returns over time, for example, we compare each dates stock returns! Or in our case the market and the stock returns. The formulas are:
Population Covariance:
$ COV(X, Y) = \frac{\Sigma (X_{i} – \bar{X})(Y_{i} – \bar{Y})}{N} $
Sample Covariance:
$ COV(X, Y) = \frac{\Sigma (X_{i} – \bar{X})(Y_{i} – \bar{Y})}{N-1} $
where
$ X_{i} = \text{The ith value of X} $
$ \bar{X} = \text{Average value of X} $
$ Y_{i} = \text{The ith value of Y} $
$ \bar{Y} = \text{Average value of Y} $
$ N = \text{The number of pairs of observations} $
First, we could find the covariance manually.
#Let's find covariance manually
#Find the deviations
deviations_market = [r - mu_market for r in market_returns]
deviations_stock = [r - mu_stock for r in stock_returns]
#Multiply each deviation by eachother and sum then divide by N or N-2
cov_sample = sum([x*y for x,y in zip(deviations_market, deviations_stock)]) / (len(market_returns) - 1)
cov_population = sum([x*y for x,y in zip(deviations_market, deviations_stock)]) / (len(market_returns))
print(cov_sample)
print(cov_population)
0.00030720450305014174
0.00030659009404404147
If you use numpy's covariance function you will get back a covariance matrix between the two assets. The i=0, j=0 cell will be the variance for the first asset, i=1, j=1 will be the variance for the second asset, and then the other two cells are going to be the covariance between the two assets.
#Or find it with numpy, it returns a covariance matrix
print(np.cov(market_returns,stock_returns))
[[0.00039156 0.0003072 ]
[0.0003072 0.00034763]]
If you index in with [0][1] we can get that covariance between the two assets.
#Find the covariance
print(np.cov(market_returns,stock_returns)[0][1])
0.00030720450305014163
The default version returns the sample covariance but we could change it to be the population covariance with ddof=0.
#Find the population covariance
print(np.cov(market_returns,stock_returns, ddof=0)[0][1])
0.00030659009404404136