Once you have successfully fitted and diagnosed a SARIMA model, ensuring the residuals resemble white noise and the model adequately captures the underlying data patterns (including seasonality), the primary goal is often to generate forecasts for future time periods. The SARIMA model, by incorporating both seasonal and non-seasonal components, allows for predictions that reflect the expected cyclical behavior.
Making predictions with a fitted statsmodels
SARIMA model is straightforward. You typically use the .get_forecast()
method, which is preferred for generating out-of-sample forecasts (predicting future values beyond the observed data). Alternatively, the .predict()
method can be used for both in-sample predictions (predicting values for time points within the original training data) and out-of-sample forecasts.
Let's assume you have a fitted SARIMA results object named sarima_results
. To forecast the next 12 periods, you would use .get_forecast()
:
# Assuming sarima_results is a fitted SARIMA model from statsmodels
# Forecast the next 12 steps
forecast_object = sarima_results.get_forecast(steps=12)
# Get the mean forecast values
mean_forecast = forecast_object.predicted_mean
# Get the confidence intervals
confidence_intervals = forecast_object.conf_int(alpha=0.05) # 95% confidence interval
# Display the forecast and confidence intervals
print("Forecasted Values:")
print(mean_forecast)
print("\nConfidence Intervals (95%):")
print(confidence_intervals)
The steps
argument specifies how many periods into the future you want to predict. The .get_forecast()
method returns an object containing several useful attributes:
predicted_mean
: The point forecasts for each future step.conf_int(alpha=...)
: The lower and upper bounds of the confidence intervals for the forecasts. The alpha
parameter determines the confidence level (e.g., alpha=0.05
for 95% confidence).se_mean
: The standard error of the forecasts.Confidence intervals are particularly important. They provide a range within which the true future value is likely to fall, given a certain probability level. Notice that as you forecast further into the future, the confidence intervals typically widen. This reflects the increasing uncertainty associated with longer-term predictions; the model becomes less certain about predictions that are many steps away from the observed data.
Visualizing the forecast along with the historical data and confidence intervals is essential for understanding the model's predictive behavior.
import pandas as pd
import plotly.graph_objects as go
# Assume 'train_data' is your original time series (Pandas Series/DataFrame)
# Assume 'mean_forecast' is the Pandas Series of forecast values
# Assume 'confidence_intervals' is the Pandas DataFrame with 'lower' and 'upper' columns
# Create figure
fig = go.Figure()
# Add historical data
fig.add_trace(go.Scatter(
x=train_data.index,
y=train_data,
mode='lines',
name='Observed Data',
line=dict(color='#495057') # Gray
))
# Add forecast line
fig.add_trace(go.Scatter(
x=mean_forecast.index,
y=mean_forecast,
mode='lines',
name='SARIMA Forecast',
line=dict(color='#f03e3e') # Red
))
# Add confidence interval bands
fig.add_trace(go.Scatter(
x=confidence_intervals.index,
y=confidence_intervals.iloc[:, 0], # Lower bound
mode='lines',
line=dict(width=0),
hoverinfo='skip',
showlegend=False
))
fig.add_trace(go.Scatter(
x=confidence_intervals.index,
y=confidence_intervals.iloc[:, 1], # Upper bound
mode='lines',
line=dict(width=0),
fillcolor='rgba(255, 107, 107, 0.2)', # Light Red fill
fill='tonexty', # Fill area between this trace and the previous one (lower bound)
hoverinfo='skip',
showlegend=False,
name='Confidence Interval'
))
# Update layout
fig.update_layout(
title='SARIMA Forecast with Confidence Intervals',
xaxis_title='Date',
yaxis_title='Value',
hovermode='x unified',
legend=dict(x=0.01, y=0.99)
)
# The plotly JSON object for embedding (replace with actual data for a real chart)
# Example static Plotly JSON representation:
# ```plotly
# {"layout": {"title": "SARIMA Forecast with Confidence Intervals", "xaxis": {"title": "Date"}, "yaxis": {"title": "Value"}, "hovermode": "x unified", "legend": {"x": 0.01, "y": 0.99}}, "data": [{"type": "scatter", "x": ["2022-01-01", "2022-02-01", "2022-03-01", "2022-04-01", "2022-05-01", "2022-06-01"], "y": [100, 110, 105, 115, 120, 118], "mode": "lines", "name": "Observed Data", "line": {"color": "#495057"}}, {"type": "scatter", "x": ["2022-07-01", "2022-08-01", "2022-09-01"], "y": [125, 122, 128], "mode": "lines", "name": "SARIMA Forecast", "line": {"color": "#f03e3e"}}, {"type": "scatter", "x": ["2022-07-01", "2022-08-01", "2022-09-01"], "y": [120, 116, 122], "mode": "lines", "line": {"width": 0}, "hoverinfo": "skip", "showlegend": false}, {"type": "scatter", "x": ["2022-07-01", "2022-08-01", "2022-09-01"], "y": [130, 128, 134], "mode": "lines", "line": {"width": 0}, "fillcolor": "rgba(255, 107, 107, 0.2)", "fill": "tonexty", "hoverinfo": "skip", "showlegend": false, "name": "Confidence Interval"}]}
# ```
# fig.show() # In a notebook environment or script to display the plot
Historical data shown alongside the SARIMA point forecast and the 95% confidence interval. The widening interval indicates increasing uncertainty for longer forecast horizons.
When interpreting the results, check if the forecast visually continues the patterns observed in the historical data, especially the seasonal cycles. The SARIMA model's strength lies in its ability to project these repeating patterns forward. However, remember that these forecasts are based purely on the historical patterns learned by the model. They assume that the underlying process generating the data remains stable and that past seasonal effects will continue in the same manner. Unforeseen events, changes in trend, or shifts in seasonal behavior are not inherently accounted for and can lead to forecast errors. Evaluating forecast accuracy using metrics discussed in the next chapter is therefore a significant step.
© 2025 ApX Machine Learning