ANALYSIS TOOL BOX

Predictive Analytics

CreateARIMAModel

Fit, evaluate, and forecast with an ARIMA time series model.

arimatime-seriesforecastingstatsmodelsstationarity

Introduction

Forecasting a time series well starts with understanding its own structure — how strongly it depends on its recent past, whether it needs differencing to become stationary, and how much noise is left once you account for both. CreateARIMAModel builds that understanding into one workflow: it runs an Augmented Dickey-Fuller stationarity test, optionally plots ACF/PACF to guide your (p, d, q) choices, fits an ARIMA model via statsmodels' SARIMAX implementation, and compares training vs. test RMSE so you know whether the fitted model actually generalizes before you trust its forecasts.

Teaching Note

Building ARIMA models is essential for:

  • Forecasting financial indicators like stock prices or currency exchange rates
  • Predicting supply chain requirements and inventory demand cycles
  • Analyzing intelligence trends such as frequency of security incidents over time
  • Monitoring economic throughput and identifying seasonal fluctuations
  • Projecting infrastructure load and server capacity requirements
  • Detecting anomalies and structural shifts in high-frequency sensor data
  • Evaluating the impact of historical interventions on future outcomes

Parameters

ParameterTypeDefaultDescription
dataframerequiredThe input pandas DataFrame containing the time series data to be modeled.
outcome_column_namerequiredThe name of the target variable column in the dataframe.
time_column_nameNoneThe name of the column representing the time axis (e.g., date, month). Required if plot_time_series is True. Defaults to None.
lookback_periods1The number of lags to include in ACF and PACF plots. Defaults to 1.
differencing_periods0The 'd' parameter in ARIMA(p, d, q) — the number of times the raw observations are differenced. Defaults to 0.
lag_periods1The 'p' parameter in ARIMA(p, d, q) — the number of lag observations included in the model (AR order). Defaults to 1.
moving_average_periods1The 'q' parameter in ARIMA(p, d, q) — the size of the moving average window (MA order). Defaults to 1.
test_size0.2Proportion of observations held out as a temporal test set (the last test_size fraction of rows). The model is fit on the remaining earlier observations. Defaults to 0.2.
plot_time_seriesFalseWhether to generate a line plot of the outcome variable over time. Defaults to False.
time_series_figure_size(8, 5)Dimensions (width, height) of the time series plot. Defaults to (8, 5).
line_color'#3269a8'Hex color code for the time series line. Defaults to '#3269a8'.
line_alpha0.8Transparency level for the plot line (0.0 to 1.0). Defaults to 0.8.
number_of_x_axis_ticksNoneDesired number of ticks on the x-axis. Defaults to None.
x_axis_tick_rotationNoneDegree of rotation for x-axis labels. Defaults to None.
title_for_plot'Time Series'Main title for the generated visualization. Defaults to 'Time Series'.
subtitle_for_plot'Shows the time series for the outcome variable.'Brief description shown below the main title.
caption_for_plotNoneExplanatory text displayed in the bottom margin. Defaults to None.
data_source_for_plotNoneSource citation displayed in the caption area. Defaults to None.
x_indent-0.127Horizontal offset for the title, subtitle, and caption text on the time series plot. Defaults to -0.127.
title_y_indent1.125Vertical offset for the plot title on the time series plot. Defaults to 1.125.
subtitle_y_indent1.05Vertical offset for the plot subtitle on the time series plot. Defaults to 1.05.
caption_y_indent-0.3Vertical offset for the plot caption on the time series plot. Defaults to -0.3.
test_for_stationarityTrueIf True, performs an Augmented Dickey-Fuller test and prints a warning if the data appears non-stationary. Defaults to True.
show_acf_pacf_plotsFalseWhether to display Autocorrelation and Partial Autocorrelation plots. Defaults to False.
show_model_resultsFalseIf True, prints a comprehensive summary of the fitted SARIMAX model. Defaults to False.
plot_residualsTrueWhether to show a kernel density estimate (KDE) plot of the model residuals (training set only). Defaults to True.
print_model_training_performanceFalseIf True, prints training RMSE and test RMSE after fitting. Defaults to False.
plot_training_and_test_performanceTrueWhether to render a bar chart comparing training RMSE and test RMSE. Defaults to True.
training_bar_color'#3a86ff'Bar color for the training RMSE bar in the performance comparison chart. Defaults to '#3a86ff'.
test_bar_color'#b0170c'Bar color for the test RMSE bar in the performance comparison chart. Defaults to '#b0170c'.
figure_size_for_performance_comparison_plot(7, 5)Dimensions (width, height) for the training/test RMSE comparison chart. Defaults to (7, 5).
title_for_performance_comparison_plotNoneTitle text for the training/test RMSE comparison chart. Defaults to a sensible built-in title when None.
subtitle_for_performance_comparison_plotNoneSubtitle text for the training/test RMSE comparison chart. Defaults to a sensible built-in subtitle when None.
caption_for_performance_comparison_plotNoneCaption text for the training/test RMSE comparison chart. Defaults to None.
title_y_indent_for_performance_comparison_plot1.1Vertical offset for the title on the performance comparison chart. Defaults to 1.1.
subtitle_y_indent_for_performance_comparison_plot1.05Vertical offset for the subtitle on the performance comparison chart. Defaults to 1.05.
caption_y_indent_for_performance_comparison_plot-0.15Vertical offset for the caption on the performance comparison chart. Defaults to -0.15.
x_indent_for_performance_comparison_plot-0.115Horizontal offset for text elements on the performance comparison chart. Defaults to -0.115.

Returns

The fitted statsmodels.tsa.statespace.sarimax.SARIMAXResultsWrapper model object, containing coefficients, statistics, and prediction methods (e.g. .forecast(steps=n)).

Example

python
from analysistoolbox.predictive_analytics import CreateARIMAModel

model = CreateARIMAModel(
    dataframe=monthly_df,
    outcome_column_name='revenue',
    time_column_name='month',
    lag_periods=2,              # AR(2)
    differencing_periods=1,     # one difference for stationarity
    moving_average_periods=1,   # MA(1)
    test_for_stationarity=True,
    plot_time_series=True,
)

# Forecast next 6 months
forecast = model.forecast(steps=6)