Skip to content

Eexploration of deterministic processes in time series forecasting using statsmodels, featuring trend modeling, seasonality, Fourier terms, custom components, and integration with AutoReg and SARIMAX models.

License

Notifications You must be signed in to change notification settings

esosetrov/deterministic_process_for_ts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

deterministic_process_for_ts

A comprehensive exploration of deterministic processes in time series forecasting using statsmodels, featuring trend modeling, seasonality, Fourier terms, custom components, and integration with AutoReg and SARIMAX models.

Overview

This project provides an in-depth modeling deterministic components in time series data, including trends, seasonality, and custom deterministic patterns. The implementation leverages statsmodels.tsa.deterministic module to create reproducible forecasting workflows with both in-sample fitting and out-of-sample prediction capabilities.

Features

  • Flexible Deterministic Modeling: Create deterministic processes with constants, polynomial trends, seasonal dummies, and Fourier terms
  • Date Index Support: Seamless integration with DateTimeIndex and PeriodIndex for time-aware forecasting
  • Custom Component Development: Extend base classes to create specialized deterministic terms for domain-specific patterns
  • Model Integration: Direct support for AutoReg models and manual integration with SARIMAX and other exogenous-aware models
  • Advanced Patterns: Implement broken trends, polynomial expansions, and interaction terms
  • Diagnostic Tools: Collinearity checks and visualization utilities for deterministic components

Key Concepts

Deterministic Components

Deterministic terms represent predictable, non-random patterns in time series data:

  • Constant: Baseline level
  • Time Trend: Linear, quadratic, or higher-order polynomial trends
  • Seasonality: Periodic patterns captured via dummy variables or Fourier series
  • Custom Terms: User-defined deterministic structures

Mathematical Foundation

A time series $y_t$ can be decomposed as: $$ y_t = \mu_t + \epsilon_t $$ where $\mu_t$ represents deterministic components and $\epsilon_t$ represents stochastic components.

The deterministic component can be expressed as: $$ \mu_t = \beta_0 + \beta_1 t + \sum_{j=1}^m \left[ \alpha_j \sin\left(\frac{2\pi j t}{P}\right) + \gamma_j \cos\left(\frac{2\pi j t}{P}\right) \right] + \sum_{k=1}^K \delta_k S_k(t) $$

Installation

pip install statsmodels pandas numpy matplotlib

Usage Examples

Basic Deterministic Process

from statsmodels.tsa.deterministic import DeterministicProcess
import pandas as pd

index = pd.RangeIndex(0, 100)
det_proc = DeterministicProcess(
    index,
    constant=True,
    order=1,          # linear trend
    seasonal=True,
    period=5
)
in_sample = det_proc.in_sample()
forecast = det_proc.out_of_sample(steps=20)

Custom Broken Time Trend

from statsmodels.tsa.deterministic import DeterministicTerm
import numpy as np

class BrokenTimeTrend(DeterministicTerm):
    def __init__(self, break_period: int):
        self._break_period = break_period
    
    def in_sample(self, index: pd.Index):
        nobs = index.shape[0]
        terms = np.zeros((nobs, 2))
        terms[self._break_period:, 0] = 1
        terms[self._break_period:, 1] = np.arange(self._break_period + 1, nobs + 1)
        return pd.DataFrame(terms, columns=["const_break", "trend_break"], index=index)

Integration with AutoReg

from statsmodels.tsa.api import AutoReg

mod = AutoReg(y, lags=1, trend="n", deterministic=det_proc)
res = mod.fit()
forecast = res.predict(start=200, end=211)

Advanced Topics Covered

  1. Multi-Component Processes: Combining trends, multiple seasonal patterns, and Fourier terms
  2. Date-Based Indexing: Working with temporal indices and date ranges
  3. Custom Term Development: Extending DeterministicTerm for specialized patterns
  4. Model Integration Strategies: Both direct (AutoReg) and manual (SARIMAX) approaches
  5. Diagnostic Validation: Condition number analysis for multicollinearity detection
  6. Visualization: Component-wise plotting for interpretability
  7. Alternative Approaches: Polynomial trends, interaction terms, and exogenous data wrapping

Project Structure

  • Basic Usage: Foundational examples of deterministic process creation
  • Date Index Handling: Temporal indexing and forecasting techniques
  • Advanced Construction: Complex deterministic patterns and custom components
  • Model Integration: Statistical model fitting and forecasting workflows
  • Alternative Approaches: Extended methodologies and custom implementations
  • Validation Tools: Diagnostic and visualization utilities

Applications

  • Economic Forecasting: Modeling business cycles, trends, and seasonal effects
  • Climate Time Series: Capturing annual and multi-year periodic patterns
  • Retail Analytics: Weekly, monthly, and holiday seasonality modeling
  • Energy Demand Forecasting: Daily and seasonal load pattern decomposition
  • Financial Time Series: Trend analysis and periodic component extraction

Dependencies

  • statsmodels >= 0.13.0
  • pandas >= 1.3.0
  • numpy >= 1.20.0
  • matplotlib >= 3.4.0

References

  1. StatsModels Documentation: Deterministic Terms in Time Series Models
  2. Hyndman, R.J., & Athanasopoulos, G. (2021). Forecasting: Principles and Practice
  3. Box, G.E.P., Jenkins, G.M., Reinsel, G.C., & Ljung, G.M. (2015). Time Series Analysis: Forecasting and Control

License

This project is licensed under the MIT License - see the LICENSE file for details.

Note

This notebook is designed for educational and research purposes. Real-world applications may require additional considerations and validation.

About

Eexploration of deterministic processes in time series forecasting using statsmodels, featuring trend modeling, seasonality, Fourier terms, custom components, and integration with AutoReg and SARIMAX models.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published