Time Series Analysis & ARIMA

551 words
3 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# Time Series Analysis & ARIMA ## ๐ŸŽฏ Learning Objectives - Decompose a time series into trend, seasonality, and residuals - Test for stationarity using the Dickey-Fuller test - Build ARIMA models for forecasting - Evaluate time series forecasts ## ๐Ÿ“– Core Content ### 5.1 Intuition: Predicting the Future from the Pas...

Time Series Analysis & ARIMA

๐ŸŽฏ Learning Objectives

  • Decompose a time series into trend, seasonality, and residuals
  • Test for stationarity using the Dickey-Fuller test
  • Build ARIMA models for forecasting
  • Evaluate time series forecasts

๐Ÿ“– Core Content

5.1 Intuition: Predicting the Future from the Past

Time series data has temporal structure โ€” today's value is related to yesterday's. Unlike standard ML where rows are independent, time series requires special handling. ARIMA (AutoRegressive Integrated Moving Average) models capture three aspects: the relationship with past values (AR), the trend (differencing), and the relationship with past errors (MA).

5.2 Time Series Components

Yt=Tt+St+Rt(additive)Y_t = T_t + S_t + R_t \quad \text{(additive)} Yt=Ttร—Stร—Rt(multiplicative)Y_t = T_t \times S_t \times R_t \quad \text{(multiplicative)}
  • TtT_t: Trend โ€” long-term direction
  • StS_t: Seasonality โ€” repeating patterns (weekly, yearly)
  • RtR_t: Residuals โ€” random noise

5.3 Stationarity

A time series is stationary if statistical properties (mean, variance) don't change over time. ARIMA requires stationarity. Augmented Dickey-Fuller (ADF) test:
  • H0H_0: Series has a unit root (non-stationary)
  • H1H_1: Series is stationary
  • If p-value < 0.05: reject H0H_0, series is stationary Making a series stationary:
  • Differencing: Ytโ€ฒ=Ytโˆ’Ytโˆ’1Y_t' = Y_t - Y_{t-1}
  • Log transformation (stabilizes variance)
  • Seasonal differencing

5.4 ARIMA Model

ARIMA(p, d, q):
  • p (AR order): Use p past values as predictors
  • d (Integration): Number of differencing steps
  • q (MA order): Use q past forecast errors AR(p): Yt=c+ฯ•1Ytโˆ’1+ฯ•2Ytโˆ’2+โ‹ฏ+ฯ•pYtโˆ’p+ฮตtY_t = c + \phi_1 Y_{t-1} + \phi_2 Y_{t-2} + \dots + \phi_p Y_{t-p} + \varepsilon_t MA(q): Yt=c+ฮตt+ฮธ1ฮตtโˆ’1+โ‹ฏ+ฮธqฮตtโˆ’qY_t = c + \varepsilon_t + \theta_1 \varepsilon_{t-1} + \dots + \theta_q \varepsilon_{t-q} ARIMA(p,d,q): AR on differenced series + MA components. Selecting p, q: Use ACF and PACF plots:
  • ACF cuts off after q โ†’ MA(q)
  • PACF cuts off after p โ†’ AR(p)

5.5 Implementation

python
# runnable
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.arima.model import ARIMA
from statsmodels.tsa.stattools import adfuller
from sklearn.metrics import mean_squared_error
# Generate sample time series
np.random.seed(42)
n = 200
t = np.arange(n)
y = 0.005 * t + np.sin(2 * np.pi * t / 50) * 0.5 + np.random.randn(n) * 0.3
# Train/test split
train, test = y[:150], y[150:]
# Fit ARIMA
model = ARIMA(train, order=(2, 1, 2))
fitted = model.fit()
print(fitted.summary())
# Forecast
forecast = fitted.forecast(steps=len(test))
rmse = np.sqrt(mean_squared_error(test, forecast))
print(f"Test RMSE: {rmse:.3f}")
# Check stationarity
result = adfuller(y)
print(f"ADF statistic: {result[0]:.3f}, p-value: {result[1]:.3f}")

๐Ÿ“ Practice Questions

Q1: What's the difference between AR and MA components?
AR uses past observed values as predictors: YtY_t depends on Ytโˆ’1,Ytโˆ’2,โ€ฆY_{t-1}, Y_{t-2}, \dots. MA uses past forecast errors: YtY_t depends on ฮตtโˆ’1,ฮตtโˆ’2,โ€ฆ\varepsilon_{t-1}, \varepsilon_{t-2}, \dots. AR captures persistence of values; MA captures shock effects that last q periods. Q2: Why must a time series be stationary for ARIMA?
Non-stationary series have time-varying mean/variance, so AR/MA coefficients learned from past data don't apply to future data. Differencing removes trends to achieve stationarity. Without it, forecasts would be biased. Q3: What does d=1 mean in ARIMA(p,1,q)?
The series is differenced once before applying AR and MA components. If original series has a linear trend, one differencing removes it. d=2 removes quadratic trends (rarely needed). Join Discord PreviousKernel MethodsNextAnomaly Detection
Document outline

Keep your place and jump directly to a heading.

Table of contents

No note selected

Open a chapter to see its outline, backlinks, and sources here.