Multiple Stocks
Solution
import matplotlib.pyplot as plt
dailyReturns.plot.hist(bins=20)
plt.show()
import matplotlib.pyplot as plt
dailyReturns.plot.hist(bins=50)
plt.show()
Let’s get a few different stocks now.
ford = pdr.get_data_yahoo("F", start, end)["Adj Close"].pct_change().dropna()
honda = pdr.get_data_yahoo("HMC", start, end)["Adj Close"].pct_change().dropna()
toyota = pdr.get_data_yahoo("TM", start, end)["Adj Close"].pct_change().dropna()
One of the most important statistics you’ll use is covariance. What it measures is how related two sets of data are. For the mathematical explanation, I will point you here (if you’re interested):
Covariance Proof
.
The equation of covariance is:
Equation
And the equation of correlation is:
Equation
Numpy allows us to find a matrix of covariances between multiple sets of data. The row i, column j holds the covariance between stock i and stock j.
matrix = np.cov([ford,honda,toyota])
print(matrix)
Two things you will notice. The diagonal represents the covariance of each stock with itself. This will be equal to the variance of the stock! Another thing you may notice is that the rest of the values are repeated, that’s because there will be a column with the covariance of Ford with Honda, as well as one for Honda with Ford. The order does not matter for covariance.
We can also get the correlation, which is the covariance divided by the the standard deviation of both i and j. This number will go from -1 meaning the two stocks move in completely opposite directions, 0 meaning there is no correlation, or 1 meaning there is a perfect correlation.
print(np.corrcoef(ford,honda))
The two corner values are both 1 because Ford is perfectly correlated with Ford, and the same for Honda.
Challenge