1. Correlation of Two Time Series
Often, two time series vary together.
2. Correlation of Two Time Series
Often, two time series vary together. Here is a plot of the stock prices of JP Morgan and the S&P500. You can see from this plot that, in general, when the market drops, JP Morgan drops as well, and when the market rises, JP Morgan also rises.
3. Correlation of Two Time Series
A scatter plot of the returns of JP Morgan and the returns of the market help to visualize the relationship between the two time series.
4. More Scatter Plots
The correlation coefficient is a measure of how much two series vary together. A correlation of one means that the two series have a perfect linear relationship with no deviations. High correlations mean that the two series strongly vary together. A low correlation means they vary together, but there is a weak association. And a high negative correlation means they vary in opposite directions, but still with a linear relationship.
5. Common Mistake: Correlation of Two Trending Series
Consider two time series that are both trending. Even if the two series are totally unrelated, you could still get a very high correlation. That's why, when you look at the correlation of say, two stocks, you should look at the correlation of their *returns*, not their levels. In this example, the two series, stock prices and UFO sightings, both trend up over time. Of course, there is no relationship between those two series, but the correlation is 0-point-94. If you compute the correlation of percent changes, the correlation goes down to approximately zero.
6. Example: Correlation of Large Cap and Small Cap Stocks
Now you'll see how to compute the correlation of two financial time series, the S&P500 index of large cap stocks and the Russell 2000 index of small cap stocks, using the pandas correlation method. First compute the percent changes using the pct_change method. This gives the returns of these series instead of prices.
7. Example: Correlation of Large Cap and Small Cap Stocks
You can also visualize the correlation with a scatter plot.
8. Example: Correlation of Large Cap and Small Cap Stocks
Then, use the pandas correlation method for Series. The correlation between large cap and small cap stocks is very high: 0-point-868
9. Let's practice!
Time to put this into practice.