adaptive_volatilityΒΆ
Defined in fynance.features.engineering
- adaptive_volatility(X, windows, regimes, period=252)[source]
Regime-adaptive realized volatility (worked example of
adaptive_roll).Uses a short volatility window in some regimes and a longer one in others, so the estimate reacts fast in turbulent regimes and stays smooth in calm ones.
- Parameters:
- Xnp.ndarray
One-dimensional price/level series.
- windowsmapping of int to int
Volatility window for each regime label.
- regimesnp.ndarray
Causal integer regime label per bar, aligned with
X.- periodint, optional
Annualization factor. Default 252.
- Returns:
- np.ndarray
Regime-adaptive annualized volatility, shape
(len(X),).
Examples
>>> import numpy as np >>> rng = np.random.default_rng(0) >>> X = 100 * np.exp(np.cumsum(rng.standard_normal(100) * 0.01)) >>> regimes = (np.arange(100) // 50) # two regimes >>> adaptive_volatility(X, {0: 5, 1: 20}, regimes).shape (100,)