AI & ML5 min read

Time Series Analysis

Analyze and forecast time series data - trend decomposition, stationarity testing, ARIMA/Prophet forecasting, and anomaly detection.

Time SeriesForecastingProphetstatsmodelsAnomaly Detection

skill: Time Series Analysis category: AI & ML tags: [Time Series, Forecasting, Prophet, statsmodels, Anomaly Detection] readingTime: 5

Time series forecasting requires understanding what your data is before selecting a model. Trend, seasonality, autocorrelation, and stationarity are properties of the data that determine which methods work. Prophet handles most business forecasting needs out of the box; ARIMA gives you more control for stationary series; neural approaches (LSTM, N-BEATS) excel for multi-variate problems.

Workflow

  1. Explore and decompose time seriesDecomposition separates trend (long-term direction), seasonality (repeating patterns), and residuals (noise). Understanding these components guides model selection.
  2. Test stationarity and differentiateMany statistical time series models (ARIMA) require stationarity - statistical properties don't change over time. Non-stationary series need differencing.
  3. Forecast with ProphetProphet (Meta) handles trend, weekly/yearly seasonality, holidays, and missing data automatically. It's the default choice for business metrics forecasting.
  4. Detect anomaliesAnomaly detection surfaces unexpected spikes, drops, and pattern changes in time series - for system monitoring, fraud detection, and quality control.
  5. Evaluate forecasts properlyIn-sample error (fit on training data) is optimistic. Proper time series evaluation uses walk-forward validation - simulate how the model would have performed historically.

Output Format

Time series: seasonal_decompose + ACF/PACF, ADF stationarity test → diff if needed, Prophet with add_country_holidays + 95% interval, IsolationForest anomaly detection, and walk-forward validation with MAE/MAPE.

seasonal_decompose(ts, model="additive", period=7)
adfuller(ts): p < 0.05 → stationary; else diff or log
Prophet(changepoint_prior_scale=0.05) + add_country_holidays("US")
IsolationForest(contamination=0.05).fit_predict(ts.reshape(-1,1))
walk_forward: train[:i] → model → predict horizon → MAE

Examples

Input

Output

Edge Cases & Gotchas

  • Zero and negative values in time series - Log transforms fail with zeros. Use log1p (log(1+x)) for zero-inflated series. MAPE fails with zero actuals - use sMAPE or MAE instead. Prophet's multiplicative seasonality fails with negative values.
  • Multiple seasonalities - Hourly data has daily + weekly + yearly seasonality. ARIMA handles one seasonality. Prophet handles multiple: add_seasonality("daily", period=1, fourier_order=3) + weekly + yearly automatically.
  • Non-stationarity vs. structural break - A permanent shift (COVID impact, product launch) looks like non-stationarity but isn't cured by differencing. Add a changepoint or regime-switching model. Prophet's changepoints handle this automatically.
  • Sparse time series - Intermittent demand (products with many zero-sales days) requires specialized models: Croston's method, ADIDA, or Temporal Fusion Transformer - standard ARIMA/Prophet performs poorly on sparse data.

When to Break These Rules

  • Use LightGBM with lag features instead of ARIMA/Prophet for multi-variate time series forecasting with external regressors - gradient boosting often outperforms classical methods on complex feature interactions.
  • Use N-BEATS or Temporal Fusion Transformer (from Darts library) instead of Prophet for high-frequency data (minute/hourly) with complex multi-seasonality patterns where Prophet underfits.

Reference Files

  • https://facebook.github.io/prophet/
  • https://www.statsmodels.org/stable/tsa.html

More skills in this library

Browse all technical skills - Mobile, AI/ML, Web, Backend, Design, and Product.