adaptive_roll

Defined in fynance.features.engineering

adaptive_roll(X, func, windows, regimes, **kwargs)[source]

Apply a window-based feature with a regime-dependent window.

At each bar t the output is func(X, windows[regimes[t]])[t] — a short window in one regime, a longer one in another. Causal as long as both inputs are: func must be a trailing-window feature (value at t uses X[..t]) and regimes a causal label (e.g. from RegimeDetector, fit-on-train / assign-online).

Parameters:
Xnp.ndarray

One-dimensional input series.

funccallable

A trailing-window feature taking (X, w, **kwargs) and returning an array aligned with X (e.g. sma, realized_volatility).

windowsmapping of int to int

Window size for each regime label. Must cover every label present in regimes.

regimesnp.ndarray

Causal integer regime label per bar, aligned with X.

**kwargs

Extra keyword arguments forwarded to func.

Returns:
np.ndarray

The regime-adaptive feature, shape (len(X),).

Examples

>>> import numpy as np
>>> from fynance.features.momentums import sma
>>> X = np.arange(1., 7.)
>>> regimes = np.array([0, 0, 0, 1, 1, 1])
>>> adaptive_roll(X, sma, {0: 1, 1: 3}, regimes)
array([1., 2., 3., 3., 4., 5.])