skfolio.descriptor.Reversal#

class skfolio.descriptor.Reversal(window=21)[source]#

Fixed-window short-term reversal descriptor.

Computes the negated cumulative log return over a trailing window:

\[\text{reversal}(t) = -\sum_{k=t-w+1}^{t} \log(1 + r_k)\]

where \(w\) is the window size and \(r_k\) is the asset return at observation \(k\). High values indicate recent poor performance (reversal candidates).

Short-term reversal captures mean reversion in returns driven by temporary price pressure, liquidity provision and microstructure effects [1] [2].

The output is NaN until an asset has a full active lookback window. Active assets with missing returns contribute zero to the log-return sum. Non-missing returns values must be finite and greater than -1.

The descriptor is returned in log-return space. Log cumulative returns are more symmetric than simple cumulative returns, which makes them better suited to cross-sectional standardization. Because the logarithm is monotonic, log-space and simple cumulative returns produce the same cross-sectional rankings when returns are finite and greater than -1.

Parameters:
windowint, default=21

Number of trailing observations for the cumulative return. Common choices for daily data:

  • window=1: 1-day reversal

  • window=5: 1-week reversal

  • window=21: 1-month reversal (default)

Attributes:
n_assets_int

Number of assets seen during fitting.

asset_names_ndarray of shape (n_assets,)

Asset names seen during fitting.

reversal_ndarray of shape (n_assets,)

Last short-term reversal value for each asset.

Methods

fit_transform(X[, y])

Compute the rolling log-return descriptor from a clean state.

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

partial_fit_transform(X[, y])

Update state and compute the rolling log-return descriptor.

set_params(**params)

Set the parameters of this estimator.

See also

EWMomentum

Exponentially weighted momentum (medium/long-term).

RollingMomentum

Fixed-window momentum with optional skip.

Notes

Two code paths are used depending on context:

  • Batch (first call with sufficient data): vectorized cumsum over the full panel. Time \(O(T \cdot n)\), space \(O(T \cdot n)\).

  • Online (subsequent calls or streaming): ring buffer of size \(w\) (the window) with a running sum. Per observation: one subtract (value leaving the window), one add (current value), one write. Time \(O(n)\) per step, space \(O(w \cdot n)\), zero allocation.

After a batch computation, the ring buffer state is populated for subsequent online calls.

References

[1]

“Evidence of predictable behavior of security returns” The Journal of Finance. Jegadeesh, N. (1990).

[2]

“Fads, martingales, and market efficiency” The Quarterly Journal of Economics. Lehmann, B. N. (1990).

Examples

>>> from skfolio.datasets import make_synthetic_characteristics
>>> from skfolio.descriptor import Reversal
>>>
>>> X = make_synthetic_characteristics()
>>>
>>> # 1-month reversal (default)
>>> descriptor = Reversal()
>>> reversal = descriptor.fit_transform(X)
>>>
>>> # 1-day reversal
>>> descriptor = Reversal(window=1)
>>> reversal_1d = descriptor.fit_transform(X)
>>>
>>> # 1-week reversal
>>> descriptor = Reversal(window=5)
>>> reversal_5d = descriptor.fit_transform(X)
fit_transform(X, y=None, **fit_params)#

Compute the rolling log-return descriptor from a clean state.

Parameters:
XAssetPanel

Input panel containing returns.

yNone

Ignored. Present for compatibility with scikit-learn’s API.

**fit_paramsdict

Additional fit parameters. Ignored.

Returns:
descriptorndarray of shape (n_observations, n_assets)

Rolling log-return descriptor for each observation and asset.

get_metadata_routing()#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating routing information.

get_params(deep=True)#

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

partial_fit_transform(X, y=None, **fit_params)#

Update state and compute the rolling log-return descriptor.

This method supports online updates by continuing from the current fitted state. Use fit_transform to start from a clean state.

Parameters:
XAssetPanel

Input panel containing returns.

yNone

Ignored. Present for compatibility with scikit-learn’s API.

**fit_paramsdict

Additional fit parameters. Ignored.

Returns:
descriptorndarray of shape (n_observations, n_assets)

Rolling log-return descriptor for each observation and asset.

set_params(**params)#

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.