ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Time Series Analysis and Forecasting
Chapter 1: Introduction to Time Series Data
Characteristics of Time Series Data
Components: Trend, Seasonality, Cyclical, Irregular
Loading and Handling Time Series in Pandas
Time Shifting, Lagging, and Rolling Windows
Visualizing Time Series Data
Hands-on Practice: Loading and Plotting Data
Quiz for Chapter 1
Chapter 2: Time Series Decomposition and Stationarity
Understanding Stationarity
Methods for Time Series Decomposition
Implementing Decomposition in Python
Testing for Stationarity: Visual Inspection
Statistical Tests for Stationarity (ADF Test)
Achieving Stationarity: Differencing
Hands-on Practice: Decomposition and Stationarity Tests
Quiz for Chapter 2
Chapter 3: Autocorrelation and Model Identification
Autocorrelation Function (ACF)
Partial Autocorrelation Function (PACF)
Plotting ACF and PACF in Python
Interpreting ACF/PACF for Model Selection
Hands-on Practice: ACF/PACF Plotting and Interpretation
Quiz for Chapter 3
Chapter 4: ARIMA Models for Forecasting
Autoregressive (AR) Models
Moving Average (MA) Models
Combining AR and MA: ARMA Models
Introducing Integration: ARIMA Models
Selecting ARIMA Order (p, d, q)
Fitting ARIMA Models in Python (statsmodels)
Model Diagnostics and Residual Analysis
Forecasting with ARIMA Models
Hands-on Practice: Building an ARIMA Model
Quiz for Chapter 4
Chapter 5: Handling Seasonality with SARIMA
Limitations of ARIMA with Seasonal Data
Introduction to Seasonal ARIMA (SARIMA)
Identifying Seasonal Components (ACF/PACF)
Selecting SARIMA Order (p, d, q)(P, D, Q)m
Fitting SARIMA Models in Python
Diagnosing SARIMA Models
Forecasting with SARIMA
Hands-on Practice: Building a SARIMA Model
Quiz for Chapter 5
Chapter 6: Model Evaluation and Selection
Need for Model Evaluation
Train-Test Split for Time Series
Common Evaluation Metrics (MAE, MSE, RMSE, MAPE)
Information Criteria (AIC, BIC)
Comparing Forecasts from Different Models
Visualizing Forecast Performance
Hands-on Practice: Evaluating Forecasts
Quiz for Chapter 6
Forecasting Future Trends in a Selected Domain

Testing for Stationarity: Visual Inspection

Stationarity is a fundamental property in time series analysis. Verifying this condition is essential because many statistical models, including ARIMA, assume that the underlying time series data exhibits stationarity. A primary and intuitive method for assessing stationarity is visual inspection. While not conclusive on its own, plotting the data and its rolling statistics can often reveal obvious trends or changes in variance that indicate non-stationarity.

Plotting the Time Series Directly

The most basic check involves simply plotting the time series values against time. Look for the following visual cues:

  1. Trend: Is there a clear long-term upward or downward slope? A consistent trend suggests the mean is not constant, violating stationarity.
  2. Seasonality: Are there patterns that repeat over fixed intervals (e.g., daily, monthly, yearly)? While some definitions of stationarity allow for predictable seasonality, strong seasonal patterns often need specific handling (like seasonal differencing or SARIMA models, discussed later). For now, recognize it as a pattern that might violate simple stationarity assumptions.
  3. Changing Variance: Does the magnitude of fluctuations change significantly over time? For instance, does the series become much more volatile in later periods? This suggests non-constant variance (heteroscedasticity), another violation of stationarity.

Let's look at an example using Python. We'll assume you have your time series loaded into a Pandas Series or DataFrame indexed by time.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import plotly.graph_objects as go # Using plotly for interactive plots

# Sample Data: Create a non-stationary series with a trend
dates = pd.date_range(start='2023-01-01', periods=50, freq='D')
data_nonstationary = np.linspace(10, 25, 50) + np.random.normal(0, 2, 50)
ts_nonstationary = pd.Series(data_nonstationary, index=dates)

# Sample Data: Create a potentially stationary series
data_stationary = np.random.normal(15, 3, 50)
ts_stationary = pd.Series(data_stationary, index=dates)

# --- Plotting Non-Stationary Data ---
fig_nonstationary = go.Figure()
fig_nonstationary.add_trace(go.Scatter(x=ts_nonstationary.index, y=ts_nonstationary.values,
                                     mode='lines', name='Time Series', line=dict(color='#339af0'))) # blue
fig_nonstationary.update_layout(
    title='Time Series with an Upward Trend',
    xaxis_title='Date',
    yaxis_title='Value',
    template='plotly_white',
    height=350,
    margin=dict(l=20, r=20, t=40, b=20)
)
# fig_nonstationary.show() # Use this in a notebook

# --- Plotting Stationary Data ---
fig_stationary = go.Figure()
fig_stationary.add_trace(go.Scatter(x=ts_stationary.index, y=ts_stationary.values,
                                   mode='lines', name='Time Series', line=dict(color='#20c997'))) # teal
fig_stationary.update_layout(
    title='Potentially Stationary Time Series',
    xaxis_title='Date',
    yaxis_title='Value',
    template='plotly_white',
    height=350,
    margin=dict(l=20, r=20, t=40, b=20)
)
# fig_stationary.show() # Use this in a notebook

Running the code above (ideally in an environment that displays Plotly charts) would generate two plots.

The clear upward slope in this plot is a strong indicator of non-stationarity due to a trend.

This series appears to fluctuate around a constant level (around 15) with relatively consistent variance. It looks more likely to be stationary, although we need more checks.

Plotting Rolling Statistics

A more quantitative visual approach is to plot the rolling mean and rolling standard deviation (or variance). If the series is stationary, both the rolling mean and rolling standard deviation should remain roughly constant over time. Significant upward or downward trends, or large fluctuations in these rolling statistics, strongly suggest non-stationarity.

Pandas provides the rolling() method to easily calculate these statistics.

# Define window size for rolling statistics (e.g., 7 days)
window_size = 7

# Calculate rolling mean and standard deviation for the non-stationary series
rolling_mean_ns = ts_nonstationary.rolling(window=window_size).mean()
rolling_std_ns = ts_nonstationary.rolling(window=window_size).std()

# --- Plotting Non-Stationary Data with Rolling Stats ---
fig_roll_ns = go.Figure()
fig_roll_ns.add_trace(go.Scatter(x=ts_nonstationary.index, y=ts_nonstationary.values,
                               mode='lines', name='Original Series', line=dict(color='#adb5bd', width=1))) # gray
fig_roll_ns.add_trace(go.Scatter(x=rolling_mean_ns.index, y=rolling_mean_ns.values,
                               mode='lines', name='Rolling Mean', line=dict(color='#f03e3e', width=2))) # red
fig_roll_ns.add_trace(go.Scatter(x=rolling_std_ns.index, y=rolling_std_ns.values,
                               mode='lines', name='Rolling Std Dev', line=dict(color='#fd7e14', width=2))) # orange

fig_roll_ns.update_layout(
    title='Non-Stationary Series with Rolling Statistics (Window=7)',
    xaxis_title='Date',
    yaxis_title='Value',
    template='plotly_white',
    height=400,
    legend_title_text='Metrics',
    margin=dict(l=20, r=20, t=50, b=20)
)
# fig_roll_ns.show()

Let's visualize this for the non-stationary data.

The rolling mean clearly trends upwards, confirming the visual assessment from the first plot. The rolling standard deviation appears relatively constant here, but the drifting mean is sufficient to classify the series as non-stationary. A similar plot for the stationary example would show both the rolling mean and rolling standard deviation hovering around flat lines.

Limitations of Visual Inspection

Visual inspection is a valuable starting point, offering quick insights into the data's behavior. However, it has limitations:

  • Subjectivity: What looks like a trend or constant variance to one person might not to another.
  • Subtlety: Small trends or minor changes in variance can be difficult to spot visually, especially in noisy data.
  • Scale: Visual patterns can be misleading depending on the scale of the plot axes.

Therefore, while visual checks are useful for identifying obvious non-stationarity, they should always be followed by more rigorous statistical tests, such as the Augmented Dickey-Fuller (ADF) test, which provide a quantitative measure of stationarity. We will cover these tests in the next section.

  • Forecasting: Principles and Practice, Rob J Hyndman and George Athanasopoulos, 2021 (OTexts) - Provides a comprehensive and practical introduction to time series forecasting, including detailed explanations of stationarity and visual diagnostic methods like plotting raw series and rolling statistics.
  • Time Series Analysis: Forecasting and Control, George E. P. Box, Gwilym M. Jenkins, Gregory C. Reinsel, and Greta M. Ljung, 2016 (John Wiley & Sons) - A foundational textbook on time series analysis, providing theoretical definitions of stationarity and its importance for ARIMA models, which are mentioned in the section.
  • Python for Data Analysis, Wes McKinney, 2022 (O'Reilly Media) - A fundamental guide to using Python's Pandas library for data manipulation and analysis, including handling time series data and calculating rolling statistics as demonstrated in the section's code examples.
  • Windowing Operations, The Pandas Development Team, 2023 - Official documentation for Pandas rolling window operations, which are used to compute rolling mean and standard deviation for visual inspection of time series stationarity.

© 2026 ApX Machine Learning