module statsforecast.core


class GroupedArray

method __init__

__init__(data: ndarray, indptr: ndarray)

method cross_validation

cross_validation(
    models,
    h,
    test_size,
    fallback_model=None,
    step_size=1,
    input_size=None,
    fitted=False,
    level=(),
    refit=True,
    verbose=False,
    target_col='y'
)

method fit

fit(models, fallback_model=None)

method fit_predict

fit_predict(models, h, X=None, level=())

method forecast

forecast(
    models,
    h,
    fallback_model=None,
    fitted=False,
    X=None,
    level=(),
    verbose=False,
    target_col='y'
)

method predict

predict(fm, h, X=None, level=())

method split

split(n_chunks)

method split_fm

split_fm(fm, n_chunks)

method take

take(idxs)

class ParallelBackend


method cross_validation

cross_validation(
    df,
    models,
    freq,
    fallback_model,
    h,
    n_windows,
    step_size,
    test_size,
    input_size,
    level,
    refit,
    fitted,
    prediction_intervals,
    id_col,
    time_col,
    target_col
) → Any

method forecast

forecast(
    models,
    fallback_model,
    freq,
    h,
    df,
    X_df,
    level,
    fitted,
    prediction_intervals,
    id_col,
    time_col,
    target_col
) → Any

class StatsForecast

method __init__

__init__(
    models: List[Any],
    freq: Union[str, int],
    n_jobs: int = 1,
    fallback_model: Optional[Any] = None,
    verbose: bool = False
)
Train statistical models. Args:
  • models (List[Any]): List of instantiated objects models.StatsForecast.
  • freq (str or int): Frequency of the data. Must be a valid pandas or polars offset alias, or an integer.
  • n_jobs (int): Number of jobs used in the parallel processing, use -1 for all cores. Defaults to 1.
  • df (pandas or polars DataFrame): DataFrame with ids, times, targets and exogenous.
  • fallback_model (Any, optional): Model to be used if a model fails. Only works with the forecast and cross_validation methods. Defaults to None.
  • verbose (bool): Prints TQDM progress bar when n_jobs=1. Defaults to True.

method cross_validation

cross_validation(
    h: int,
    df: Any,
    n_windows: int = 1,
    step_size: int = 1,
    test_size: Optional[int] = None,
    input_size: Optional[int] = None,
    level: Optional[List[int]] = None,
    fitted: bool = False,
    refit: Union[bool, int] = True,
    prediction_intervals: Optional[ConformalIntervals] = None,
    id_col: str = 'unique_id',
    time_col: str = 'ds',
    target_col: str = 'y'
)

method cross_validation_fitted_values

cross_validation_fitted_values() → Union[DataFrame, DataFrame]
Access insample cross validated predictions. After executing StatsForecast.cross_validation, you can access the insample prediction values for each model and window. To get them, you need to pass fitted=True to the StatsForecast.cross_validation method and then use the StatsForecast.cross_validation_fitted_values method. Returns:
  • pandas or polars DataFrame: DataFrame with insample models columns for point predictions and probabilistic predictions for all fitted models.

method fit

fit(
    df: Union[DataFrame, DataFrame],
    prediction_intervals: Optional[ConformalIntervals] = None,
    id_col: str = 'unique_id',
    time_col: str = 'ds',
    target_col: str = 'y'
)
Fit statistical models. Fit models to a large set of time series from DataFrame df and store fitted models for later inspection. Args:
  • df (pandas or polars DataFrame): DataFrame with ids, times, targets and exogenous.
  • prediction_intervals (ConformalIntervals, optional): Configuration to calibrate prediction intervals (Conformal Prediction). Defaults to None.
  • id_col (str): Column that identifies each serie. Defaults to ‘unique_id’.
  • time_col (str): Column that identifies each timestep, its values can be timestamps or integers. Defaults to ‘ds’.
  • target_col (str): Column that contains the target. Defaults to ‘y’.
Returns:
  • StatsForecast: Returns with stored StatsForecast fitted models.

method fit_predict

fit_predict(
    h: int,
    df: Union[DataFrame, DataFrame],
    X_df: Optional[DataFrame, DataFrame] = None,
    level: Optional[List[int]] = None,
    prediction_intervals: Optional[ConformalIntervals] = None,
    id_col: str = 'unique_id',
    time_col: str = 'ds',
    target_col: str = 'y'
) → Union[DataFrame, DataFrame]
Fit and Predict with statistical models. This method avoids memory burden due from object storage. It is analogous to Scikit-Learn fit_predict without storing information. It requires the forecast horizon h in advance. In contrast to StatsForecast.forecast this method stores partial models outputs. Args:
  • h (int): Forecast horizon.
  • df (pandas or polars DataFrame): DataFrame with ids, times, targets and exogenous.
  • X_df (pandas or polars DataFrame, optional): DataFrame with ids, times and future exogenous. Defaults to None.
  • level (List[float], optional): Confidence levels between 0 and 100 for prediction intervals. Defaults to None.
  • prediction_intervals (ConformalIntervals, optional): Configuration to calibrate prediction intervals (Conformal Prediction). Defaults to None.
  • id_col (str): Column that identifies each serie. Defaults to ‘unique_id’.
  • time_col (str): Column that identifies each timestep, its values can be timestamps or integers. Defaults to ‘ds’.
  • target_col (str): Column that contains the target. Defaults to ‘y’.
Returns:
  • pandas or polars DataFrame: DataFrame with models columns for point predictions and probabilistic predictions for all fitted models.

method forecast

forecast(
    h: int,
    df: Any,
    X_df: Optional[DataFrame, DataFrame] = None,
    level: Optional[List[int]] = None,
    fitted: bool = False,
    prediction_intervals: Optional[ConformalIntervals] = None,
    id_col: str = 'unique_id',
    time_col: str = 'ds',
    target_col: str = 'y'
)

method forecast_fitted_values

forecast_fitted_values()

method load

load(path: Union[Path, str])
Automatically loads the model into ready StatsForecast. Args:
  • path (str or pathlib.Path): Path to saved StatsForecast file.
Returns:
  • StatsForecast: Previously saved StatsForecast

method plot

plot(
    df: Union[DataFrame, DataFrame],
    forecasts_df: Optional[DataFrame, DataFrame] = None,
    unique_ids: Union[List[str], NoneType, ndarray] = None,
    plot_random: bool = True,
    models: Optional[List[str]] = None,
    level: Optional[List[float]] = None,
    max_insample_length: Optional[int] = None,
    plot_anomalies: bool = False,
    engine: str = 'matplotlib',
    id_col: str = 'unique_id',
    time_col: str = 'ds',
    target_col: str = 'y',
    resampler_kwargs: Optional[Dict] = None
)
Plot forecasts and insample values. Args:
  • df (pandas or polars DataFrame): DataFrame with ids, times, targets and exogenous.
  • forecasts_df (pandas or polars DataFrame, optional): DataFrame ids, times and models. Defaults to None.
  • unique_ids (list of str, optional): ids to plot. If None, they’re selected randomly. Defaults to None.
  • plot_random (bool): Select time series to plot randomly. Defaults to True.
  • models (List[str], optional): List of models to plot. Defaults to None.
  • level (List[float], optional): List of prediction intervals to plot if paseed. Defaults to None.
  • max_insample_length (int, optional): Max number of train/insample observations to be plotted. Defaults to None.
  • plot_anomalies (bool): Plot anomalies for each prediction interval. Defaults to False.
  • engine (str): Library used to plot. ‘plotly’, ‘plotly-resampler’ or ‘matplotlib’. Defaults to ‘matplotlib’.
  • id_col (str): Column that identifies each serie. Defaults to ‘unique_id’.
  • time_col (str): Column that identifies each timestep, its values can be timestamps or integers. Defaults to ‘ds’.
  • target_col (str): Column that contains the target. Defaults to ‘y’.
  • resampler_kwargs (dict): Kwargs to be passed to plotly-resampler constructor. For further custumization (“show_dash”) call the method, store the plotting object and add the extra arguments to its show_dash method.

method predict

predict(
    h: int,
    X_df: Optional[DataFrame, DataFrame] = None,
    level: Optional[List[int]] = None
)
Predict statistical models. Use stored fitted models to predict large set of time series from DataFrame df. Args:
  • h (int): Forecast horizon.
  • X_df (pandas or polars DataFrame, optional): DataFrame with ids, times and future exogenous. Defaults to None.
  • level (List[float], optional): Confidence levels between 0 and 100 for prediction intervals. Defaults to None.
Returns:
  • pandas or polars DataFrame: DataFrame with models columns for point predictions and probabilistic predictions for all fitted models.

method save

save(
    path: Optional[Path, str] = None,
    max_size: Optional[str] = None,
    trim: bool = False
)
Function that will save StatsForecast class with certain settings to make it reproducible. Args:
  • path (str or pathlib.Path, optional): Path of the file to be saved. If None will create one in the current directory using the current UTC timestamp. Defaults to None.
  • max_size (str, optional): StatsForecast object should not exceed this size.
  • Available byte naming: [‘B’, ‘KB’, ‘MB’, ‘GB’]. Defaults to None.
  • trim (bool): Delete any attributes not needed for inference. Defaults to False.