correlation

correlation(x, y, w=Window(w=None, r=0), type_=<SeriesType.PRICES: 'prices'>)[source]

Rolling correlation of two price series

Parameters
  • x (Series) – price series

  • y (Series) – price series

  • w (Union[Window, int]) – Window or int: number of observations and ramp up to use. e.g. Window(22, 10) where 22 is the window size

and 10 the ramp up value. Window size defaults to length of series. :param type_: type of both input series :rtype: Series :return: date-based time series of correlation

Usage

Calculate rolling realized correlation, \(\rho_t\) of two price series over a given window:

\(\rho_t = \frac{\sum_{i=t-w+1}^t (R_t - \overline{R_t})(Y_t - \overline{S_t})}{(N-1)\sigma R_t\sigma S_t}\)

where N is the number of observations in each rolling window, \(w\), and \(R_t\) and \(S_t\) are the simple returns for each series on time \(t\):

\(R_t = \frac{X_t}{X_{t-1}} - 1\) and \(S_t = \frac{Y_t}{Y_{t-1}} - 1\)

If prices = False, assumes returns are provided:

\(R_t = X_t\) and \(S_t = Y_t\)

\(\overline{R_t}\), \(\overline{S_t}\) are the mean values, and \(\sigma R_{t}\) and \(\sigma S_{t}\) are the sample standard deviations, of series \(R_t\) and \(S_t\) over the same window

If window is not provided, computes realized correlation over the full series

Examples

Compute rolling \(1\) month (\(22\) business day) correlation of price series

>>> series1 = generate_series(100)
>>> series2 = generate_series(100)
>>> corr = correlation(series1, series2, 22)

See also

std() returns()