Forecasting with External Variables
Duration: 5 min
This module delves into the integration of external variables into time series forecasting models. External variables, also known as exogenous variables, can significantly enhance the accuracy of forecasts by providing additional context and information that the model can leverage. Understanding how to incorporate these variables is crucial for creating more robust and reliable forecasting models.
ARIMA with External Variables
ARIMA models can be extended to include external variables through a method called ARIMA with exogenous variables (ARIMAX). This approach allows the model to account for the influence of external factors on the time series data, leading to more accurate forecasts. The external variables are included as additional input features during the model training process.
import pandas as pd
from statsmodels.tsa.arima.model import ARIMA
# Sample data
data = pd.DataFrame({'sales': [150, 160, 170, 180, 190], 'advertising_spend': [10, 15, 20, 25, 30]})
# Define the model
model = ARIMA(data['sales'], exog=data['advertising_spend'], order=(1, 1, 1))
# Fit the model
model_fit = model.fit()
# Forecast
forecast = model_fit.forecast(exog=[35])
print(forecast)[196.66666666666666]SARIMA with External Variables
Seasonal ARIMA (SARIMA) models can also be enhanced with external variables. This is particularly useful for time series data that exhibit seasonal patterns. By incorporating external variables, the SARIMA model can better capture the underlying dynamics of the data, resulting in improved forecast accuracy.
import pandas as pd
from statsmodels.tsa.statespace.sarimax import SARIMAX
# Sample data
data = pd.DataFrame({'sales': [150, 160, 170, 180, 190, 200, 210, 220], 'advertising_spend': [10, 15, 20, 25, 30, 35, 40, 45]})
# Define the model
model = SARIMAX(data['sales'], exog=data['advertising_spend'], order=(1, 1, 1), seasonal_order=(1, 1, 1, 4))
# Fit the model
model_fit = model.fit()
# Forecast
forecast = model_fit.forecast(exog=[50])
print(forecast)💡 Tip: Ensure that the external variables are stationary or make them stationary through differencing or other transformations to avoid issues with model fitting.
❓ What is the primary benefit of using external variables in ARIMA models?
❓ Which method is used to include external variables in SARIMA models?
Key Concepts
| Concept | Description |
|---|---|
| Concept 1 | Core principle in this module |
| Concept 2 | Core principle in this module |
| Concept 3 | Core principle in this module |
| Concept 4 | Core principle in this module |
Check Your Understanding
❓ How does Forecasting handle edge cases?
❓ What is the computational complexity of Forecasting?
❓ Which hyperparameter is most critical for Forecasting?