Correlation of Two Time Series

Time Series Analysis in Python

Rob Reider

Adjunct Professor, NYU-Courant Consultant, Quantopian

Correlation of Two Time Series

  • Plot of S&P500 and JPMorgan stock
Time Series Analysis in Python

Correlation of Two Time Series

  • Scatter plot of S&P500 and JP Morgan returns
Time Series Analysis in Python

More Scatter Plots

  • Correlation = 0.9

  • Correlation = -0.9

  • Correlation = 0.4

  • Corelation = 1.0

Time Series Analysis in Python

Common Mistake: Correlation of Two Trending Series

  • Dow Jones Industrial Average and UFO Sightings (www.nuforc.org)

  • Correlation of levels: 0.94

  • Correlation of percent changes: $\large \approx$ 0
Time Series Analysis in Python

Example: Correlation of Large Cap and Small Cap Stocks

  • Start with stock prices of SPX (large cap) and R2000 (small cap)
  • First step: Compute percentage changes of both series
      df['SPX_Ret'] = df['SPX_Prices'].pct_change()
      df['R2000_Ret'] = df['R2000_Prices'].pct_change()
    
Time Series Analysis in Python

Example: Correlation of Large Cap and Small Cap Stocks

  • Visualize correlation with scattter plot
      plt.scatter(df['SPX_Ret'], df['R2000_Ret'])
      plt.show()
    

Time Series Analysis in Python

Example: Correlation of Large Cap and Small Cap Stocks

  • Use pandas correlation method for Series
correlation = df['SPX_Ret'].corr(df['R2000_Ret'])
print("Correlation is: ", correlation)
Correlation is: 0.868
Time Series Analysis in Python

Let's practice!

Time Series Analysis in Python

Preparing Video For Download...