Skip to content

Forecast

cli_forecast(args, sf_model_args=(), sf_model_kwargs={})

run_forecasting(df, sf_model, baseline_hours=72.0, sf_model_args=tuple(), sf_model_kwargs=dict(), *args, **kwargs)

Runs a forecasting model on a DataFrame, splitting it into training and testing sets, and adding the forecast results to the original DataFrame.

PARAMETER DESCRIPTION
df

The input DataFrame containing the data to forecast.

TYPE: DataFrame

sf_model

The forecasting model class to be used.

TYPE: Any

baseline_hours

The time window in hours for the training set. Defaults to 72.0.

TYPE: float | int DEFAULT: 72.0

sf_model_args

Positional arguments to pass to the forecasting model constructor. Defaults to an empty tuple.

TYPE: tuple[Any, ...] DEFAULT: tuple()

sf_model_kwargs

Keyword arguments to pass to the forecasting model constructor. Defaults to an empty dictionary.

TYPE: dict[str, Any] DEFAULT: dict()

RETURNS DESCRIPTION
DataFrame

pl.DataFrame: The original DataFrame with an additional 'y_hat' column containing the forecasted values.

RAISES DESCRIPTION
ValueError

If the 'y' column is not found in the training DataFrame.

TypeError

If 'baseline_hours' is not a float or int.

Examples:

>>> import polars as pl
>>> from some_forecasting_library import SomeForecastingModel
>>> data = {
...     'ds': ["2023-01-01 00:00:00", "2023-01-02 00:00:00", "2023-01-03 00:00:00",
...            "2023-01-04 00:00:00", "2023-01-05 00:00:00"],
...     'y': [10, 20, 30, 40, 50]
... }
>>> df = pl.DataFrame(data)
>>> df = run_forecasting(df, SomeForecastingModel, baseline_hours=48)
>>> df
shape: (5, 3)
┌─────────────────────┬─────┬───────┐
│ ds                  ┆ y   ┆ y_hat │
│ ---                 ┆ --- ┆ ---   │
│ datetime[ms]        ┆ i64 ┆ f64   │
╞═════════════════════╪═════╪═══════╡
│ 2023-01-01 00:00:00 ┆ 10  ┆ 10.1  │
│ 2023-01-02 00:00:00 ┆ 20  ┆ 19.9  │
│ 2023-01-03 00:00:00 ┆ 30  ┆ 29.8  │
│ 2023-01-04 00:00:00 ┆ 40  ┆ 39.5  │
│ 2023-01-05 00:00:00 ┆ 50  ┆ 49.2  │
└─────────────────────┴─────┴───────┘