Relationships between time series: correlation

Manipulating Time Series Data in Python

Stefan Jansen

Founder & Lead Data Scientist at Applied Artificial Intelligence

Correlation & relations between series

  • So far, focus on characteristics of individual variables
  • Now: characteristic of relations between variables
  • Correlation: measures linear relationships
  • Financial markets: important for prediction and risk management
  • pandas & seaborn have tools to compute & visualize
Manipulating Time Series Data in Python

Correlation & linear relationships

  • Correlation coefficient: how similar is the pairwise movement of two variables around their averages?
  • Varies between -1 and +1 $\ \ \ \ \ r = \frac{\sum_{i=1}^{N} (x_i - \bar{x})(y_i - \bar{y})}{s_xs_y}$

 

ch3_4_v2 - Correlation & Heatmaps.011.png

Manipulating Time Series Data in Python

Importing five price time series

data = pd.read_csv('assets.csv', parse_dates=['date'], 
                   index_col='date')

data = data.dropna().info()
DatetimeIndex: 2469 entries, 2007-05-25 to 2017-05-22
Data columns (total 5 columns):
sp500     2469 non-null float64
nasdaq    2469 non-null float64
bonds     2469 non-null float64
gold      2469 non-null float64
oil       2469 non-null float64
Manipulating Time Series Data in Python

Visualize pairwise linear relationships

daily_returns = data.pct_change()

sns.jointplot(x='sp500', y='nasdaq', data=data_returns);

ch3_4_v2 - Correlation & Heatmaps.015.png

Manipulating Time Series Data in Python

Calculate all correlations

correlations = returns.corr()

correlations
bonds       oil      gold     sp500    nasdaq
bonds   1.000000 -0.183755  0.003167 -0.300877 -0.306437
oil    -0.183755  1.000000  0.105930  0.335578  0.289590
gold    0.003167  0.105930  1.000000 -0.007786 -0.002544
sp500  -0.300877  0.335578 -0.007786  1.000000  0.959990
nasdaq -0.306437  0.289590 -0.002544  0.959990  1.000000
Manipulating Time Series Data in Python

Visualize all correlations

sns.heatmap(correlations, annot=True)

ch3_4_v2 - Correlation & Heatmaps.019.png

Manipulating Time Series Data in Python

Let's practice!

Manipulating Time Series Data in Python

Preparing Video For Download...