ewma_cov

Defined in fynance.portfolio.covariance

ewma_cov(X, halflife=63.0)[source]

RiskMetrics-style exponentially weighted covariance matrix.

Weights recent observations more heavily: \(\lambda = 0.5^{1 / halflife}\), weight of observation \(t\) (out of \(T\), most recent last) proportional to \(\lambda^{T - 1 - t}\), normalized to sum to one. Data is demeaned by the plain (equally-weighted) column mean before accumulating weighted outer products.

Parameters:
Xarray_like

Returns panel, shape (T,) or (T, N), rows in chronological order (oldest first).

halflifefloat, optional

Number of steps after which a past observation’s weight is halved. Default 63.0 (~ one quarter of trading days).

Returns:
np.ndarray

Symmetric (N, N) covariance matrix.

References

[1]

J.P. Morgan/Reuters, “RiskMetrics – Technical Document”, 4th edition, 1996.

Examples

>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> X = rng.standard_normal((100, 3))
>>> S = ewma_cov(X, halflife=20.0)
>>> S.shape
(3, 3)
>>> bool(np.allclose(S, S.T))
True