Skip to content
Home
Time Series Analysis: Forecasting, Decomposition, and Stationarity

Time Series Analysis: Forecasting, Decomposition, and Stationarity

Data Science Data Science 7 min read 1485 words Beginner ExcellentWiki Editorial Team

Time series analysis is the study of data points collected or recorded at specific time intervals. Unlike cross-sectional data, time series data has a natural temporal ordering — observations close in time tend to be correlated. This autocorrelation makes time series analysis both powerful and challenging. Applications include financial market prediction, demand forecasting, weather modeling, anomaly detection in IoT sensor data, and economic indicator analysis.

Components of Time Series

Every time series can be decomposed into components that capture different patterns.

Trend

The long-term direction of the series — upward, downward, or flat. Trends may be linear or non-linear. A company’s quarterly revenue typically shows an upward trend over years.

Seasonality

Regular, periodic fluctuations with known frequency. Retail sales spike every December. Ice cream sales increase every summer. Sunspot activity follows an 11-year cycle. Seasonality is fixed and predictable.

Cyclic Patterns

Long-term oscillations that are not of fixed period. Economic business cycles (recovery, expansion, contraction, recession) are cyclic. Unlike seasonality, cycles vary in length and magnitude.

Residual (Noise)

Random variation that cannot be attributed to trend, seasonality, or cycles. Noise is what remains after removing systematic components.

Additive vs Multiplicative Decomposition

import statsmodels.api as sm

# Additive: Y(t) = Trend + Seasonal + Residual
decomposition = sm.tsa.seasonal_decompose(series, model='additive', period=12)

# Multiplicative: Y(t) = Trend × Seasonal × Residual
decomposition = sm.tsa.seasonal_decompose(series, model='multiplicative', period=12)

Use additive when seasonal variations are roughly constant over time. Use multiplicative when seasonal variations grow with the trend level.

Stationarity

A stationary time series has constant mean, variance, and autocorrelation over time. Most forecasting models require stationarity.

Why Stationarity Matters

Non-stationary series contain unit roots — shocks have permanent effects. Forecasting with non-stationary data leads to spurious regression: apparently significant relationships that are actually meaningless. Stationarity ensures that the statistical properties of past observations generalize to future observations.

Testing for Stationarity

The Augmented Dickey-Fuller (ADF) test is the most common stationarity test:

from statsmodels.tsa.stattools import adfuller

result = adfuller(series)
print(f'ADF Statistic: {result[0]:.4f}')
print(f'p-value: {result[1]:.4f}')
# p-value < 0.05 indicates stationarity

Making Series Stationary

Differencing: Subtract the previous observation from each observation.

series_diff = series.diff().dropna()

Log transformation: Stabilizes variance for multiplicative series.

series_log = np.log(series)

Seasonal differencing: Remove seasonal component.

series_seasonal_diff = series.diff(periods=12).dropna()

Autocorrelation and Partial Autocorrelation

ACF and PACF plots are essential tools for identifying time series model parameters.

  • ACF (Autocorrelation Function): Shows correlation between the series and its lagged values
  • PACF (Partial Autocorrelation Function): Shows correlation after removing intermediate lag effects
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf

plot_acf(series, lags=40)
plot_pacf(series, lags=40)

ACF that decays gradually and PACF that cuts off after a few lags suggests an autoregressive (AR) process. PACF that decays gradually and ACF that cuts off suggests a moving average (MA) process.

Forecasting Models

ARIMA (AutoRegressive Integrated Moving Average)

ARIMA combines three components: AR (autoregressive) captures dependence on past values, I (integrated) handles differencing for stationarity, MA (moving average) captures dependence on past forecast errors.

from statsmodels.tsa.arima.model import ARIMA

model = ARIMA(series, order=(p, d, q))
# p = AR order, d = differences, q = MA order
results = model.fit()
forecast = results.forecast(steps=12)

Choosing p, d, q: Use ACF/PACF plots for p and q. Use ADF test for d. Automate with auto_arima:

from pmdarima import auto_arima

model = auto_arima(series, seasonal=False, trace=True)

Seasonal ARIMA (SARIMA)

Extends ARIMA to handle seasonality by adding seasonal parameters:

model = SARIMAX(series, order=(1, 1, 1), seasonal_order=(1, 1, 1, 12))
results = model.fit()

Facebook Prophet

Prophet handles missing data, outliers, and holidays automatically. It uses an additive model with non-linear trends:

from prophet import Prophet

df = pd.DataFrame({'ds': dates, 'y': values})
model = Prophet(yearly_seasonality=True, weekly_seasonality=True)
model.fit(df)
future = model.make_future_dataframe(periods=365)
forecast = model.predict(future)

Exponential Smoothing (Holt-Winters)

Weighted averages of past observations, with exponentially decreasing weights:

from statsmodels.tsa.holtwinters import ExponentialSmoothing

model = ExponentialSmoothing(series, trend='add', seasonal='add', seasonal_periods=12)
fitted = model.fit()
forecast = fitted.forecast(12)

Forecasting Best Practices

Train-Test Split for Time Series

Time series requires sequential splitting — never random:

train = series[:-12]  # All but last 12 months
test = series[-12:]   # Last 12 months

Evaluation Metrics

| Metric | Formula | Interpretation | |

Advanced Topics

Multivariate Time Series

Incorporate external regressors — variables that influence the target but are not themselves forecasted. Examples include weather data for energy demand forecasting or promotional calendars for retail sales.

Deep Learning Approaches

LSTM and Transformer models can capture complex temporal dependencies without explicit feature engineering. They are particularly effective for long-horizon forecasting with multiple related time series.

Anomaly Detection

Time series anomalies can indicate system failures, fraud, or data quality issues. Statistical methods (moving Z-score, STL decomposition) and machine learning approaches (isolation forest, autoencoders) can detect unusual patterns.

Conclusion

Time series analysis requires understanding the unique properties of temporal data — trend, seasonality, stationarity, and autocorrelation. Start with visual exploration and decomposition. Test for stationarity and apply transformations as needed. Choose a modeling approach that matches your data characteristics and forecasting horizon. Always validate on out-of-sample data and compare against simple baselines. With practice, time series forecasting becomes a powerful tool for making data-driven decisions about the future.

Time Series Components and Decomposition

Secular Trend, Seasonality, and Cycles

Every time series can be decomposed into three core components plus residual noise. The secular trend represents the long-term direction — overall growth or decline over years. Seasonality captures regular patterns that repeat at fixed intervals — daily website traffic peaks, weekly sales patterns, or annual seasonal effects. Cycles differ from seasonality in their irregular period — economic business cycles last 5-10 years, not a fixed monthly or quarterly pattern. The classical multiplicative decomposition divides the series by its trend-cycle and seasonal components: Y = T × S × R. The additive decomposition subtracts components: Y = T + S + R. Choosing additive vs. multiplicative depends on whether the seasonal amplitude grows with the trend level — multiplicative is appropriate when seasonal fluctuations scale with the overall magnitude.

Stationarity and Differencing

Most time series models require stationarity — constant mean, variance, and autocorrelation over time. Non-stationary series violate model assumptions and produce spurious regression results. The Augmented Dickey-Fuller (ADF) test formally tests for stationarity: a p-value below 0.05 rejects the null hypothesis that a unit root exists. KPSS test inverts the hypotheses — significant p-value indicates non-stationarity. Differencing transforms a non-stationary series into a stationary one by subtracting consecutive observations. First-order differencing removes a linear trend; second-order differencing removes quadratic trends. Seasonal differencing (subtracting the value from one seasonal cycle back) removes seasonal non-stationarity. Automated algorithms like auto-ARIMA test multiple differencing orders and select the minimal differencing needed to achieve stationarity.

Forecasting with ARIMA and Beyond

The ARIMA (AutoRegressive Integrated Moving Average) model combines autoregression (AR — using past values as predictors), differencing (I — making the series stationary), and moving average (MA — modeling forecast errors as a weighted sum of past errors). Seasonal ARIMA (SARIMA) adds seasonal autoregression, differencing, and moving average terms. Model selection uses AIC and BIC to balance fit with parsimony. Prophet, developed by Facebook, provides an alternative approach designed for business forecasting with strong seasonality, holiday effects, and changepoints. For long-horizon forecasts, neural network approaches like N-BEATS, DeepAR, and Temporal Fusion Transformers capture complex patterns but require more data and computational resources than statistical methods.

Frequently Asked Questions

What is the difference between white noise and a random walk? White noise is stationary with zero autocorrelation — each observation is independent and identically distributed with constant mean and variance. A random walk is non-stationary — each observation equals the previous observation plus white noise. Random walks drift over time and have increasing variance. Stock prices and exchange rates often follow random walks or close approximations.

How do I handle missing values in time series? Forward fill (last observation carried forward) is appropriate for slowly changing series. Linear interpolation works for short gaps in smooth series. Seasonal adjustment uses the value from the same point in the previous season. For longer gaps, models like ARIMA can estimate missing values as latent variables or use state space methods with Kalman filtering for optimal imputation.

What is autocorrelation and why does it matter? Autocorrelation measures the correlation between a time series and its lagged versions. Positive autocorrelation at lag 1 means high values tend to follow high values — common in economic and financial data. Autocorrelation matters because standard statistical tests assume independent observations. Models must explicitly account for autocorrelation structure through AR terms or Generalized Least Squares.

How many data points do I need for time series forecasting? For simple exponential smoothing, 2-3 full seasonal cycles are a minimum — if modeling weekly seasonality, at least 14-21 observations. For ARIMA, 50+ observations provide reasonable estimates. For neural network models (DeepAR, N-BEATS), hundreds to thousands of time steps are typically needed. More data always improves forecast accuracy, especially for capturing seasonal patterns and long-term dependencies.

For a comprehensive overview, read our article on Bayesian Statistics Guide.

For a comprehensive overview, read our article on Big Data Tools Guide.

Section: Data Science 1485 words 7 min read Beginner 756 articles in section Report inaccuracy Back to top