garch_volatilityΒΆ

Defined in fynance.features.garch

garch_volatility(returns, refit=None, min_train=250)[source]

Causal GARCH(1,1) conditional-volatility feature.

Fits a GARCH(1,1) on a training prefix and forward-filters the conditional volatility \(\sigma_t\) over the whole series. The first min_train values are NaN (the warmup whose parameters would be in-sample).

Parameters:
returnsarray-like

One-dimensional return series.

refitint, optional

Refit the parameters on the expanding window every refit steps (each block uses parameters fit strictly on its own past). If None (default), the parameters are fit once on returns[:min_train].

min_trainint, optional

Length of the initial training prefix; values before it are NaN. Default 250.

Returns:
numpy.ndarray

Conditional volatility \(\sigma_t\), same length as returns, NaN on the warmup.

Examples

>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> r = rng.standard_normal(400) * 0.01
>>> sigma = garch_volatility(r, min_train=200)
>>> sigma.shape
(400,)
>>> bool(np.all(np.isnan(sigma[:200])))
True
>>> bool(np.all(sigma[200:] >= 0))
True