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 lengthnand held for the nextsperiods. By construction this respects strict temporal ordering — no future data leaks into the weights — which is the same no-lookahead pattern used infynance.models.rolling._RollingBasisfor 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
Xand**kwargs, and return a vector (asnp.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=252ands=63.- retbool, optional
If True (default) pass to
fthe returns ofX. Otherwise passXtof.- 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
falgorithm.- pd.DataFrame
Weights of the portfolio allocated following
falgorithm.
Notes
Weights are computed on the past data from
t - ntotand are applied to backtest on data fromttot + s.\[\forall t \in [n, T], w_{t:t+s} = f(X_{t-n:t})\]