roll_risk_contributionΒΆ
Defined in fynance.portfolio.attribution
- roll_risk_contribution(W, X, n=252, cov=None, pct=True)[source]
Causal rolling risk contributions over a time series.
For each time step \(t \geq n\), estimate the covariance matrix from returns in the past \(n\) periods and compute risk contributions for the weights at time \(t\). Earlier rows are filled with NaN (no covariance estimate yet).
- Parameters:
- Warray_like
Weight time series, shape
(T, N).- Xarray_like
Returns panel, shape
(T, N), rows in chronological order (oldest first).- nint, optional
Window length for covariance estimation. Default 252.
- covcallable, optional
Covariance estimator callable that accepts a returns panel and returns a symmetric
(N, N)matrix. If None (default), usesnumpy.covwithrowvar=False.- pctbool, optional
If True (default), return percentage contributions summing to 1. If False, return absolute contributions summing to portfolio volatility per period.
- Returns:
- np.ndarray
Risk contributions per period, shape
(T, N). Rowst < nare filled with NaN.
Examples
>>> import numpy as np >>> rng = np.random.default_rng(0) >>> X = rng.standard_normal((50, 3)) >>> W = rng.uniform(0.2, 0.4, (50, 3)) >>> W /= W.sum(axis=1, keepdims=True) >>> rc = roll_risk_contribution(W, X, n=20) >>> rc.shape (50, 3) >>> np.isnan(rc[:20]).all() True