Skip to content

Return Autocorrelation

Intuição

A Return Autocorrelation mede a correlação entre retornos atuais e retornos defasados (lag) dentro de uma janela. Ajuda a identificar regimes de momentum (autocorr positiva) ou reversão (autocorr negativa).

Definição

Em uma janela n e lag k:

AC(k) = corr(r_t, r_{t-k})

Uso

from quantmaster.features.statistical import return_autocorrelation

df["ac_60_1"] = return_autocorrelation(df, window=60, lag=1)

API

Source code in src/quantmaster/features/statistical.py
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
def return_autocorrelation(
    data: pd.DataFrame | pd.Series,
    *,
    window: int = 60,
    lag: int = 1,
    price_col: str = "close",
    log_returns: bool = True,
) -> pd.Series:
    window = validate_positive_int(window, name="window")
    lag = validate_positive_int(lag, name="lag")
    if lag >= window:
        raise ValueError(f"lag must be < window, got lag={lag} window={window}")

    price = get_price_series(data, price_col=price_col).astype(float)
    price = price.where(price > 0)

    if log_returns:
        r = np.log(price).diff()
    else:
        r = price.pct_change()

    def _fn(w: np.ndarray) -> float:
        a = w[lag:]
        b = w[:-lag]
        return _rolling_corr_1d(a, b)

    out = r.rolling(window).apply(_fn, raw=True)
    out.name = f"return_autocorrelation_{window}_{lag}"
    return out