Time Series Analysis
Time series data has a time dimension that introduces unique challenges and opportunities. Learn to decompose trends, detect seasonality, test for stationarity, and build forecasting models.
Time Series Components
Every time series can be decomposed into fundamental components:
Trend
Long-term direction — is the series generally increasing, decreasing, or flat over time?
Seasonality
Regular, repeating patterns at fixed intervals — monthly sales spikes, daily website traffic patterns.
Residuals (Noise)
Random fluctuations left after removing trend and seasonality. Ideally, residuals look like white noise.
import pandas as pd from statsmodels.tsa.seasonal import seasonal_decompose import matplotlib.pyplot as plt # Load time series data df = pd.read_csv('monthly_sales.csv', parse_dates=['date'], index_col='date') # Decompose the time series decomposition = seasonal_decompose(df['sales'], model='additive', period=12) # Plot components fig = decomposition.plot() fig.set_size_inches(12, 8) plt.tight_layout() plt.show()
Stationarity
A stationary time series has constant statistical properties (mean, variance, autocorrelation) over time. Most forecasting models require stationarity.
from statsmodels.tsa.stattools import adfuller # Augmented Dickey-Fuller test for stationarity result = adfuller(df['sales']) print(f"ADF Statistic: {result[0]:.4f}") print(f"p-value: {result[1]:.4f}") if result[1] < 0.05: print("Series is stationary") else: print("Series is NOT stationary - apply differencing") # Make stationary by differencing df['sales_diff'] = df['sales'].diff().dropna()
Moving Averages
Moving averages smooth out short-term fluctuations to reveal underlying trends. They are also used as baselines for forecasting.
# Simple Moving Average df['SMA_7'] = df['sales'].rolling(window=7).mean() df['SMA_30'] = df['sales'].rolling(window=30).mean() # Exponential Moving Average (more weight on recent values) df['EMA_7'] = df['sales'].ewm(span=7).mean() # Plot plt.figure(figsize=(12, 5)) plt.plot(df['sales'], alpha=0.5, label='Actual') plt.plot(df['SMA_30'], label='30-day SMA') plt.plot(df['EMA_7'], label='7-day EMA') plt.legend() plt.title('Sales with Moving Averages') plt.show()
ARIMA Models
ARIMA (AutoRegressive Integrated Moving Average) is the most widely used time series forecasting model. It combines three components:
| Component | Parameter | What It Does |
|---|---|---|
| AR (AutoRegressive) | p | Uses past values to predict future values |
| I (Integrated) | d | Number of differences needed for stationarity |
| MA (Moving Average) | q | Uses past forecast errors to improve predictions |
from statsmodels.tsa.arima.model import ARIMA import warnings warnings.filterwarnings('ignore') # Fit ARIMA model (p=1, d=1, q=1) model = ARIMA(df['sales'], order=(1, 1, 1)) results = model.fit() print(results.summary()) # Forecast next 30 days forecast = results.forecast(steps=30) # Plot actual vs forecast plt.figure(figsize=(12, 5)) plt.plot(df['sales'], label='Actual') plt.plot(forecast, color='red', label='Forecast') plt.legend() plt.title('Sales Forecast') plt.show() # Auto-select best ARIMA parameters from pmdarima import auto_arima auto_model = auto_arima(df['sales'], seasonal=True, m=12, suppress_warnings=True) print(auto_model.summary())
Exponential Smoothing
Exponential smoothing methods weight recent observations more heavily than older ones. The Holt-Winters method handles trend and seasonality together.
from statsmodels.tsa.holtwinters import ExponentialSmoothing # Holt-Winters (Triple Exponential Smoothing) model = ExponentialSmoothing( df['sales'], trend='add', # Additive trend seasonal='add', # Additive seasonality seasonal_periods=12 # Monthly data with yearly cycle ) results = model.fit() # Forecast forecast = results.forecast(12) # Next 12 months plt.figure(figsize=(12, 5)) plt.plot(df['sales'], label='Actual') plt.plot(results.fittedvalues, label='Fitted', alpha=0.7) plt.plot(forecast, color='red', label='Forecast') plt.legend() plt.title('Holt-Winters Forecast') plt.show()
Real-World Applications
| Domain | Application | Common Models |
|---|---|---|
| Finance | Stock prices, trading volume, risk forecasting | GARCH, ARIMA, LSTM |
| Weather | Temperature, rainfall, wind speed prediction | SARIMA, state-space models |
| Retail | Demand forecasting, inventory planning | Holt-Winters, Prophet, XGBoost |
| Energy | Power consumption, renewable energy output | ARIMA, neural networks |
| Healthcare | Disease outbreaks, patient admissions | SIR models, SARIMA |
pip install prophet.
Lilly Tech Systems