Skip to content

BaseStatsForecaster

yohou_nixtla.stats.BaseStatsForecaster

Bases: BaseNixtlaForecaster

Base class for statsforecast model wrappers in yohou.

Wraps a statsforecast model class using the BaseNixtlaForecaster pattern, making it fully compatible with yohou's forecaster API (fit / predict / observe / clone / get_params).

Subclasses only need to set _estimator_default_class to a specific statsforecast model class (e.g., AutoARIMA).

Parameters

Name Type Description Default
model type or None

The statsforecast model class to wrap. Must be a subclass of statsforecast.models._TS. If not provided, _estimator_default_class is used.

None
freq str or None

Frequency string (pandas offset alias). If None, auto-inferred from the data at fit time.

None
**params dict

Parameters forwarded to the statsforecast model constructor.

{}

Attributes

Name Type Description
nixtla_forecaster_ StatsForecast

The fitted Nixtla StatsForecast orchestrator (internal).

freq_ str

The inferred or provided frequency string.

instance_ _TS

The constructed statsforecast model instance (from BaseClassWrapper).

y_columns_ list of str

Original target column names from the training data.

See Also

yohou_nixtla.stats.AutoARIMAForecaster : AutoARIMA wrapper. yohou_nixtla.stats.NaiveForecaster : Naive baseline wrapper.

Source Code

Show/Hide source
class BaseStatsForecaster(BaseNixtlaForecaster):
    """Base class for statsforecast model wrappers in yohou.

    Wraps a statsforecast model class using the ``BaseNixtlaForecaster``
    pattern, making it fully compatible with yohou's forecaster API
    (``fit`` / ``predict`` / ``observe`` / ``clone`` / ``get_params``).

    Subclasses only need to set ``_estimator_default_class`` to a specific
    statsforecast model class (e.g., ``AutoARIMA``).

    Parameters
    ----------
    model : type or None, default=None
        The statsforecast model class to wrap. Must be a subclass of
        ``statsforecast.models._TS``. If not provided,
        ``_estimator_default_class`` is used.
    freq : str or None, default=None
        Frequency string (pandas offset alias). If None, auto-inferred
        from the data at fit time.
    **params : dict
        Parameters forwarded to the statsforecast model constructor.

    Attributes
    ----------
    nixtla_forecaster_ : StatsForecast
        The fitted Nixtla StatsForecast orchestrator (internal).
    freq_ : str
        The inferred or provided frequency string.
    instance_ : _TS
        The constructed statsforecast model instance (from ``BaseClassWrapper``).
    y_columns_ : list of str
        Original target column names from the training data.

    See Also
    --------
    yohou_nixtla.stats.AutoARIMAForecaster : AutoARIMA wrapper.
    yohou_nixtla.stats.NaiveForecaster : Naive baseline wrapper.

    """

    _estimator_name = "model"
    _estimator_base_class = _TS
    _estimator_default_class: type | None = None

    @_fit_context(prefer_skip_nested_validation=True)
    def fit(
        self,
        y: pl.DataFrame,
        X_actual: pl.DataFrame | None = None,
        forecasting_horizon: int = 1,
        X_future: pl.DataFrame | None = None,
        X_forecast: pl.DataFrame | None = None,
        **params,
    ) -> BaseStatsForecaster:
        """Fit the statsforecast model to the training data.

        Parameters
        ----------
        y : pl.DataFrame
            Target time series with ``time`` column.
        X_actual : pl.DataFrame or None, default=None
            Actual observation features with ``time`` column.
        forecasting_horizon : int, default=1
            Number of steps to forecast.
        X_future : pl.DataFrame or None, default=None
            Known future features with ``time`` column.
        X_forecast : pl.DataFrame or None, default=None
            Not supported. Raises ``ValueError`` if provided.
        **params : dict
            Additional metadata routing parameters.

        Returns
        -------
        self
            Fitted forecaster.

        """
        return super().fit(
            y=y,
            X_actual=X_actual,
            forecasting_horizon=forecasting_horizon,
            X_future=X_future,
            X_forecast=X_forecast,
            **params,
        )

    def _fit_backend(self, nixtla_df: Any, forecasting_horizon: int) -> None:
        """Create and fit the StatsForecast orchestrator.

        Parameters
        ----------
        nixtla_df : pd.DataFrame
            Training data in Nixtla long format.
        forecasting_horizon : int
            Number of steps to forecast.

        """
        sf = StatsForecast(
            models=[self.instance_],
            freq=self.freq_,
        )
        sf.fit(df=nixtla_df)
        self.nixtla_forecaster_ = sf

    def _predict_backend(self, forecasting_horizon: int, X_future: Any = None) -> Any:
        """Generate raw predictions from the StatsForecast orchestrator.

        Parameters
        ----------
        forecasting_horizon : int
            Number of steps to forecast.
        X_future : pd.DataFrame or None, default=None
            Known future features in Nixtla long format, passed as
            ``X_df`` to ``StatsForecast.predict()``.

        Returns
        -------
        pd.DataFrame
            Raw statsforecast predictions.

        """
        check_is_fitted(self, ["nixtla_forecaster_"])
        return self.nixtla_forecaster_.predict(h=forecasting_horizon, X_df=X_future)

Methods

fit(y, X_actual=None, forecasting_horizon=1, X_future=None, X_forecast=None, **params)

Fit the statsforecast model to the training data.

Parameters
Name Type Description Default
y DataFrame

Target time series with time column.

required
X_actual DataFrame or None

Actual observation features with time column.

None
forecasting_horizon int

Number of steps to forecast.

1
X_future DataFrame or None

Known future features with time column.

None
X_forecast DataFrame or None

Not supported. Raises ValueError if provided.

None
**params dict

Additional metadata routing parameters.

{}
Returns
Type Description
self

Fitted forecaster.

Source Code
Show/Hide source
@_fit_context(prefer_skip_nested_validation=True)
def fit(
    self,
    y: pl.DataFrame,
    X_actual: pl.DataFrame | None = None,
    forecasting_horizon: int = 1,
    X_future: pl.DataFrame | None = None,
    X_forecast: pl.DataFrame | None = None,
    **params,
) -> BaseStatsForecaster:
    """Fit the statsforecast model to the training data.

    Parameters
    ----------
    y : pl.DataFrame
        Target time series with ``time`` column.
    X_actual : pl.DataFrame or None, default=None
        Actual observation features with ``time`` column.
    forecasting_horizon : int, default=1
        Number of steps to forecast.
    X_future : pl.DataFrame or None, default=None
        Known future features with ``time`` column.
    X_forecast : pl.DataFrame or None, default=None
        Not supported. Raises ``ValueError`` if provided.
    **params : dict
        Additional metadata routing parameters.

    Returns
    -------
    self
        Fitted forecaster.

    """
    return super().fit(
        y=y,
        X_actual=X_actual,
        forecasting_horizon=forecasting_horizon,
        X_future=X_future,
        X_forecast=X_forecast,
        **params,
    )