Time Series Analysis & ARIMA
551 words
3 min read
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) Yt=Tt×St×Rt(multiplicative)- Tt: Trend — long-term direction
- St: Seasonality — repeating patterns (weekly, yearly)
- Rt: 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:
- H0: Series has a unit root (non-stationary)
- H1: Series is stationary
- If p-value < 0.05: reject H0, series is stationary Making a series stationary:
- Differencing: Yt′=Yt−Yt−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+εt MA(q): Yt=c+εt+θ1εt−1+⋯+θqε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: Yt depends on Yt−1,Yt−2,…. MA uses past forecast errors: Yt depends on εt−1,εt−2,…. 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