Time Series Analysis & Forecasting
Time series data is ordered by time β stock prices, weather, sales. Forecasting predicts future values.
Time Series Components
Trend: Long-term increase/decrease
Seasonality: Repeating patterns (daily, weekly, yearly)
Cyclical: Irregular patterns (not fixed period)
Noise: Random variation
Decomposition:
y(t) = Trend(t) + Seasonal(t) + Residual(t)
Or multiplicative:
y(t) = Trend(t) Γ Seasonal(t) Γ Residual(t)
Stationarity
Stationary: Statistical properties don't change over time
ββ Constant mean
ββ Constant variance
ββ Constant autocorrelation
Why it matters: Most models assume stationarity
Tests: Augmented Dickey-Fuller (ADF) test
p-value < 0.05 β stationary
Make non-stationary:
ββ Differencing: y'(t) = y(t) - y(t-1)
ββ Log transform
ββ Detrending
ARIMA
ARIMA(p, d, q):
ββ AR (p): Autoregressive β uses past values
ββ I (d): Integrated β differencing order
ββ MA (q): Moving average β uses past errors
AR(1): y(t) = c + Οβy(t-1) + Ξ΅(t)
MA(1): y(t) = c + Ξ΅(t) + ΞΈβΞ΅(t-1)
from statsmodels.tsa.arima.model import ARIMA
model = ARIMA(train, order=(1, 1, 1))
fitted = model.fit()
forecast = fitted.forecast(steps=30)
Facebook Prophet
from prophet import Prophet
# Data must have 'ds' and 'y' columns
df = pd.DataFrame({'ds': dates, 'y': values})
model = Prophet(
yearly_seasonality=True,
weekly_seasonality=True,
daily_seasonality=False
)
model.fit(df)
future = model.make_future_dataframe(periods=365)
forecast = model.predict(future)
model.plot(forecast)
Key Takeaways
- Stationarity is assumed by most time series models
- ARIMA is the classical approach
- Prophet handles seasonality and holidays automatically
- LSTM neural networks capture complex patterns
- Always split by time (not random) for validation
- Feature engineering (lags, rolling stats) helps ML models
- Exponential smoothing is simple but effective
- Ensemble multiple forecasting methods for best results