<a id="skfolio-descriptor-rollingmomentum"></a>

# skfolio.descriptor.RollingMomentum

<a id="skfolio.descriptor.RollingMomentum"></a>

### *class* skfolio.descriptor.RollingMomentum(window=252, skip=21, exponentiate=False)

Fixed-window momentum descriptor.

Computes the sum of log returns over a trailing window with an optional skip period
to exclude the most recent observations:

The skip period separates medium-term momentum from short-term reversal. The classic
“12-1” momentum signal uses a skip of approximately one month [[1]](#r00dbc6ffe58d-1).

$$
\[
\begin{aligned}
x(k)
    &= \log(1 + r(k)) \\[0.75em]
S(t)
    &= \sum_{k=t-\text{skip}-\text{window}+1}^{t-\text{skip}}
       x(k) \\[0.75em]
\text{momentum}(t)
    &=
    \begin{cases}
    \exp(S(t)) - 1 & \text{if } \texttt{exponentiate=True} \\
    S(t) & \text{otherwise}
    \end{cases}
\end{aligned}
\]
$$

The window uses the $\text{window}$ observations ending at
$t - \text{skip}$. Output is NaN until the asset has a full active lookback
window.

By default, 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:**
  **window** *int, default=252*
  : Number of observations in the lookback window.

  **skip** *int, default=21*
  : Number of most recent observations excluded from the window. The last
    observation included is at $t - \text{skip}$. Classic 12-1 momentum uses a
    skip of about one month (21 daily obs). Set to 0 for no skip.

  **exponentiate** *bool, default=False*
  : If True, output is $\exp(S(t)) - 1$ (return units). If False, output is
    $S(t)$ (log space). Cross-sectional ranking is unchanged and only the 
    scale differs.
* **Attributes:**
  **n_assets_** *int*
  : Number of assets seen during fitting.

  **asset_names_** *ndarray of shape (n_assets,)*
  : Asset names seen during fitting.

  **momentum_** *ndarray of shape (n_assets,)*
  : Last rolling momentum value for each asset.

### Methods

| [`fit_transform`](#skfolio.descriptor.RollingMomentum.fit_transform)(X[, y])         | Compute the rolling log-return descriptor from a clean state.   |
|--------------------------------------------------------------------------------|-----------------------------------------------------------------|
| [`get_metadata_routing`](#skfolio.descriptor.RollingMomentum.get_metadata_routing)()        | Get metadata routing of this object.                            |
| [`get_params`](#skfolio.descriptor.RollingMomentum.get_params)([deep])            | Get parameters for this estimator.                              |
| [`partial_fit_transform`](#skfolio.descriptor.RollingMomentum.partial_fit_transform)(X[, y]) | Update state and compute the rolling log-return descriptor.     |
| [`set_params`](#skfolio.descriptor.RollingMomentum.set_params)(\*\*params)        | Set the parameters of this estimator.                           |

#### SEE ALSO
[`EWMomentum`](https://skfolio.org/generated/skfolio.descriptor.EWMomentum.html.md#skfolio.descriptor.EWMomentum)
: Exponentially weighted momentum.

### 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
  $L = \text{skip} + \text{window}$ with a running sum. Per observation: one
  subtract (value leaving the window), one add (value entering), one write.
  Time $O(n)$ per step, space $O(L \cdot n)$, zero allocation.

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

NaNs are allowed as missing observations. Non-missing `returns` values must be
finite and greater than `-1`, so $\log(1 + r)$ is finite. Active assets with
NaN returns (e.g. holidays) contribute 0 to the sum. Inactive asset outputs are
set to NaN.

### References

* <a id='r00dbc6ffe58d-1'>**[1]**</a> “Returns to buying winners and selling losers: Implications for stock market efficiency” The Journal of Finance. Jegadeesh, N., & Titman, S. (1993).

### Examples

```pycon
>>> from skfolio.datasets import make_synthetic_characteristics
>>> from skfolio.descriptor import RollingMomentum
>>>
>>> X = make_synthetic_characteristics()
>>>
>>> # 12-1 momentum
>>> descriptor = RollingMomentum(window=252, skip=21)
>>> momentum = descriptor.fit_transform(X)
>>>
>>> # Log-space output
>>> descriptor = RollingMomentum(window=252, skip=21, exponentiate=False)
>>> momentum_log = descriptor.fit_transform(X)
```

<a id="skfolio.descriptor.RollingMomentum.fit_transform"></a>

#### fit_transform(X, y=None, \*\*fit_params)

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

* **Parameters:**
  **X** *AssetPanel*
  : Input panel containing `returns`.

  **y** *None*
  : Ignored. Present for compatibility with scikit-learn’s API.

  **\*\*fit_params** *dict*
  : Additional fit parameters. Ignored.
* **Returns:**
  **descriptor** *ndarray of shape (n_observations, n_assets)*
  : Rolling log-return descriptor for each observation and asset.

<a id="skfolio.descriptor.RollingMomentum.get_metadata_routing"></a>

#### get_metadata_routing()

Get metadata routing of this object.

Please check [User Guide](https://skfolio.org/user_guide/metadata_routing.html.md#metadata-routing) on how the routing
mechanism works.

* **Returns:**
  **routing** *MetadataRequest*
  : A `MetadataRequest` encapsulating
    routing information.

<a id="skfolio.descriptor.RollingMomentum.get_params"></a>

#### get_params(deep=True)

Get parameters for this estimator.

* **Parameters:**
  **deep** *bool, default=True*
  : If True, will return the parameters for this estimator and
    contained subobjects that are estimators.
* **Returns:**
  **params** *dict*
  : Parameter names mapped to their values.

<a id="skfolio.descriptor.RollingMomentum.partial_fit_transform"></a>

#### 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:**
  **X** *AssetPanel*
  : Input panel containing `returns`.

  **y** *None*
  : Ignored. Present for compatibility with scikit-learn’s API.

  **\*\*fit_params** *dict*
  : Additional fit parameters. Ignored.
* **Returns:**
  **descriptor** *ndarray of shape (n_observations, n_assets)*
  : Rolling log-return descriptor for each observation and asset.

<a id="skfolio.descriptor.RollingMomentum.set_params"></a>

#### 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:**
  **\*\*params** *dict*
  : Estimator parameters.
* **Returns:**
  **self** *estimator instance*
  : Estimator instance.

