> ## Documentation Index
> Fetch the complete documentation index at: https://nixtlaverse.nixtla.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Automatic Time Series Forecasting

> How to do automatic forecasting using `AutoARIMA`, `AutoETS`,
> `AutoCES` and `AutoTheta`.

> **Tip**
>
> Automatic forecasts of large numbers of univariate time series are
> often needed. It is common to have multiple product lines or skus that
> need forecasting. In these circumstances, an automatic forecasting
> algorithm is an essential tool. Automatic forecasting algorithms must
> determine an appropriate time series model, estimate the parameters
> and compute the forecasts. They must be robust to unusual time series
> patterns, and applicable to large numbers of series without user
> intervention.

## 1. Install statsforecast and load data

Use pip to install statsforecast and load Air Passengers dataset as an
example

```python theme={null}
# uncomment the following line to install the library
# %pip install statsforecast
```

```python theme={null}
from statsforecast.utils import AirPassengersDF
```

```python theme={null}
Y_df = AirPassengersDF
```

## 2. Import StatsForecast and models

Import the core StatsForecast class and the models you want to use

```python theme={null}
import pandas as pd

from statsforecast import StatsForecast
from statsforecast.models import AutoARIMA, AutoETS, AutoTheta, AutoCES
```

## 3. Instantiate the class

Instantiate the StatsForecast class with the appropriate parameters

```python theme={null}
season_length = 12 # Define season length as 12 months for monthly data
horizon = 1 # Forecast horizon is set to 1 month

# Define a list of models for forecasting
models = [
    AutoARIMA(season_length=season_length), # ARIMA model with automatic order selection and seasonal component
    AutoETS(season_length=season_length), # ETS model with automatic error, trend, and seasonal component
    AutoTheta(season_length=season_length), # Theta model with automatic seasonality detection
    AutoCES(season_length=season_length), # CES model with automatic seasonality detection
]

# Instantiate StatsForecast class with models, data frequency ('M' for monthly),
# and parallel computation on all CPU cores (n_jobs=-1)
sf = StatsForecast(
    models=models, # models for forecasting
    freq=pd.offsets.MonthEnd(),  # frequency of the timestamps
    n_jobs=1  # number of jobs to run in parallel, -1 means using all processors
)
```

## 4. a) Forecast with forecast method

The `.forecast` method is faster for distributed computing and does not
save the fittted models

```python theme={null}
# Generate forecasts for the specified horizon using the sf object
Y_hat_df = sf.forecast(df=Y_df, h=horizon) # forecast data
# Display the first few rows of the forecast DataFrame
Y_hat_df.head() # preview of forecasted data
```

|   | unique\_id | ds         | AutoARIMA  | AutoETS    | AutoTheta  | CES       |
| - | ---------- | ---------- | ---------- | ---------- | ---------- | --------- |
| 0 | 1.0        | 1961-01-31 | 444.309575 | 442.357169 | 442.940797 | 453.03418 |

## 4. b) Forecast with fit and predict

The `.fit` method saves the fitted models

```python theme={null}
sf.fit(df=Y_df) # Fit the models to the data using the fit method of the StatsForecast object

sf.fitted_ # Access fitted models from the StatsForecast object

Y_hat_df = sf.predict(h=horizon) # Predict or forecast 'horizon' steps ahead using the predict method

Y_hat_df.head() # Preview the first few rows of the forecasted data
```

|   | unique\_id | ds         | AutoARIMA  | AutoETS    | AutoTheta  | CES       |
| - | ---------- | ---------- | ---------- | ---------- | ---------- | --------- |
| 0 | 1.0        | 1961-01-31 | 444.309575 | 442.357169 | 442.940797 | 453.03418 |

## References

[Hyndman, RJ and Khandakar, Y (2008) “Automatic time series forecasting:
The forecast package for R”, Journal of Statistical Software,
26(3).](https://www.jstatsoft.org/article/view/v027i03)
