roll_cov

Defined in fynance.features.roll_functions

roll_cov(x, y, w=63)[source]

Trailing rolling covariance between two aligned 1-D series.

\[roll\_cov^w_t(x, y) = \frac{1}{w} \sum_{i=t-w+1}^{t} (x_i - \bar{x}_t)(y_i - \bar{y}_t)\]

where \(\bar{x}_t\) and \(\bar{y}_t\) are the means of x and y over the trailing window \([t - w + 1, t]\) (inclusive of t, the “house” trailing-window convention shared with roll_min/roll_max). The variance used is the biased (ddof=0) estimator.

Parameters:
xnp.ndarray[float64, ndim=1]

First series. Cast to float64 if needed; must not contain NaN.

ynp.ndarray[float64, ndim=1]

Second series, same length as x. Cast to float64 if needed; must not contain NaN.

wint, optional

Size of the trailing window, must be an integer >= 2. Default is 63.

Returns:
np.ndarray[float64, ndim=1]

Trailing rolling covariance. The first w - 1 entries are np.nan (insufficient history).

See also

roll_corr, roll_beta

Examples

>>> x = np.array([1., 2., 3., 4., 5., 6.])
>>> y = 2 * x
>>> roll_cov(x, y, w=2)
array([nan, 0.5, 0.5, 0.5, 0.5, 0.5])