rolling_allocation

Defined in fynance.algorithms.allocation

rolling_allocation(f, X, n=252, s=63, ret=True, drift=True, **kwargs)[source]

Roll an algorithm of portfolio allocation.

Generic walk-forward backtester for any allocation function f (e.g. ERC, HRP, MVP). At each step the weights are estimated on a training window of length n and held for the next s periods. By construction this respects strict temporal ordering — no future data leaks into the weights — which is the same no-lookahead pattern used in fynance.models.rolling._RollingBasis for ML models.

Assets that are constant on more than 50% of the training window are dropped from that step’s allocation; the remaining weight mass is redistributed across the active assets.

Parameters:
fcallable

Allocation algorithm that take as parameters a subarray of X and **kwargs, and return a vector (as np.ndarray) of weights.

Xarray_like

Data matrix, each columns is a series of prices, indexes or performances, each row is a observation at time t.

n, sint

Respectively the number of observations to compute weights and the number of observations to roll. Default is n=252 and s=63.

retbool, optional

If True (default) pass to f the returns of X. Otherwise pass X to f.

driftbool, optional

If False performance of the portfolio is computed as if we rebalance the weights of asset at each timeframe. Otherwise we let to drift the weights. Default is True.

**kwargs

Any keyword arguments to pass to f.

Returns:
pd.Series

Performance of the portfolio allocated following f algorithm.

pd.DataFrame

Weights of the portfolio allocated following f algorithm.

Notes

Weights are computed on the past data from t - n to t and are applied to backtest on data from t to t + s.

\[\forall t \in [n, T], w_{t:t+s} = f(X_{t-n:t})\]