Time-Series Forecasting with ARIMA and SARIMAX

A time series forecasting script in Python that predicts daily COVID-19 cases for a country using pmdarima.auto_arima for model selection and statsmodels SARIMAX for fitting — a worked example of the ADF stationarity test, seasonal ARIMA, in-sample validation, and future forecasts with confidence intervals.

Python projects · Machine learning and time series

Start here

Read the requirements and any configuration below, then follow the usage example. Source paths below are relative to the repository root; run commands from the working directory stated in Usage. This example’s source is in ML/COVID Forecast/Console_Code/. Install any third-party packages in an isolated Python environment.

Overview

This script downloads daily COVID-19 case data, performs stationarity analysis, automatically selects the best ARIMA/SARIMA model, fits it, and generates both an in-sample static forecast and a future forecast for the next N days. All results are visualized with matplotlib.

Features

  • Automatically downloads COVID-19 daily case data if not cached locally
  • Augmented Dickey-Fuller (ADF) stationarity test with detailed output
  • Rolling mean and standard deviation visualization for stationarity inspection
  • Automatic ARIMA/SARIMA parameter selection via pmdarima.auto_arima
  • SARIMAX model fitting using statsmodels
  • Static (in-sample) forecast for the last 120 days
  • Future forecast for the next N days with confidence intervals
  • All plots displayed with gridlines, auto-scaled axes, and monthly date formatting

Prerequisites and installation

numpy
pandas
pmdarima
covid_daily
statsmodels
matplotlib

Install dependencies:

python -m pip install numpy pandas pmdarima covid_daily statsmodels matplotlib

Configuration

Edit the variables at the top of CovidForecast.py:

Variable Default Description
country 'india' Country to forecast ('india', 'brazil', etc.)
stepCount 50 Number of future days to forecast
frequency 'D' Time series frequency ('D' = daily)

Usage

From the Console_Code folder:

python CovidForecast.py

On first run, a CSV file (e.g., india_corvid19.csv) is downloaded and saved in the working directory. Subsequent runs load from this cached file.

Output

  1. ADF test results printed to console
  2. Rolling statistics chart (stationarity check)
  3. Static forecast chart (last 120 days vs. in-sample prediction)
  4. Future forecast chart with confidence interval shading and a table of predicted values

How It Works

  1. Data is loaded and resampled to daily frequency
  2. A 12-period rolling detrend and differencing step produces a separate series for the stationarity diagnostics
  3. auto_arima searches for the best ARIMA order using AIC and an ADF test for differencing
  4. The SARIMAX model is fit with Powell optimization
  5. Static in-sample predictions (dynamic=False) and future forecasts are generated and plotted

Architecture

COVID Forecast/
└── Console_Code/
    └── CovidForecast.py    # Main script

Troubleshooting and limitations

  • The transformed series is used for ADF testing and the rolling-statistics chart. Both auto_arima and SARIMAX are fitted to the original y series, not the detrended series. The in-sample chart is not an independent held-out evaluation.
  • The dataset[newColumn].astype(float) result is not assigned back; provide numeric values in a cached CSV. The static forecast needs at least 120 observations.
  • auto_arima is called with seasonal=True and m=12, so it looks for a 12-step seasonal pattern in the daily series. Weekly reporting patterns would correspond to m=7; try both if you adapt the script.
  • The data download depends on the third-party covid_daily package and the site it reads from. If the download fails, place a CSV with the same structure at <country>_corvid19.csv and the script will load it instead.
  • The forecasts are a modelling exercise, not public-health guidance.


Read all Python Projects and Code Examples guides online · Project overview · All developer projects