StatsForecast provide a comprehensive interface for fitting, predicting, forecasting, and evaluating statistical forecasting models on large sets of time series.
Overview
The main methods include:StatsForecast.fit- Fit statistical modelsStatsForecast.predict- Predict using fitted modelsStatsForecast.forecast- Memory-efficient predictions without storing modelsStatsForecast.cross_validation- Temporal cross-validationStatsForecast.plot- Visualization of forecasts and historical data
StatsForecast Class
StatsForecast
Bases: _StatsForecast
The StatsForecast class allows you to efficiently fit multiple StatsForecast models
for large sets of time series. It operates on a DataFrame df with at least three columns:
ids, times, and targets.
The class has a memory-efficient StatsForecast.forecast method that avoids storing partial
model outputs, while the StatsForecast.fit and StatsForecast.predict methods with the
Scikit-learn interface store the fitted models.
The StatsForecast class offers parallelization utilities with Dask, Spark, and Ray back-ends.
See distributed computing example here.
Parallelism across series is controlled by the constructor argument n_jobs, not by the individual fit, predict, forecast, or cross_validation methods. Pass n_jobs=-1 when instantiating StatsForecast to use all available cores:
Accessing fitted models per series
AfterStatsForecast.fit, the fitted model objects are stored in sf.fitted_
as a 2-D NumPy array of shape (n_series, n_models):
- The first axis is indexed by series in the order the
unique_idvalues first appear in the input DataFrame. - The second axis follows the order of the
modelslist passed to theStatsForecastconstructor.
.model_ attribute, which
holds fit-specific state such as residuals. To iterate over every series
alongside its unique_id:
unique_id
instead, pass fitted=True to forecast or cross_validation and call
sf.forecast_fitted_values() or sf.cross_validation_fitted_values().
StatsForecast.fit
predict method. This follows the scikit-learn fit/predict interface.
Parameters:
Returns:
StatsForecast.predict
fit method to generate predictions for the
specified forecast horizon. This follows the scikit-learn fit/predict interface.
Parameters:
Returns:
StatsForecast.fit_predict
fit and predict methods in a single operation. The fitted models
are stored internally in the fitted_ attribute for later use, making this method
suitable when you need both training and immediate predictions.
Parameters:
Returns:
StatsForecast.forecast
fit_predict
when you don’t need to inspect or reuse the fitted models. Models are trained and
used for forecasting within each time series, then discarded.
Parameters:
Returns:
StatsForecast.cross_validation
Returns:
StatsForecast.plot
Returns:
StatsForecast.save
load() method
to restore the exact state for making predictions.
Parameters:
StatsForecast.load
save() method,
restoring all fitted models and configuration. The loaded object is ready to
generate predictions immediately.
Parameters:
Returns:
Usage Examples
Basic Forecasting
Cross-Validation
Prediction Intervals
Conformal Prediction Intervals
Advanced Features
Integer Datestamps
TheStatsForecast class can work with integer datestamps instead of datetime objects:
External Regressors
Every column aftery is considered an external regressor and will be passed to models that support them:
Distributed Computing
TheStatsForecast class offers parallelization utilities with Dask, Spark and Ray backends for distributed computing. See the distributed computing examples for more information.
