Skip to main content

TsSHAP-style feature-based explainability for univariate forecasting: backtesting, interpretable features, tree surrogate (sklearn/XGBoost/LightGBM/CatBoost), SHAP, notebook-driven TSICE comparison.

Project description

TsSHAP: Feature-based Explainability for Time Series Forecasting

Implementation and evaluation of the TsSHAP algorithm from:

Raykar et al. (2023). TsSHAP: Robust model agnostic feature-based explainability for univariate time series forecasting. IBM Research.


Project Structure

final_project/
├── tsshap/                    # Core library
│   ├── features/              # Interpretable feature extractors
│   │   ├── stl_features.py    # STL decomposition (trend, seasonal, residual)
│   │   ├── lag_features.py    # Lag & seasonal lag features
│   │   ├── window_features.py # Rolling & expanding window statistics
│   │   ├── trend_features.py  # Polynomial trend features
│   │   └── date_features.py   # Calendar date/time encodings
│   ├── forecasters/           # Forecaster wrappers (fit/predict interface)
│   │   ├── naive.py           # Naive, SeasonalNaive, MovingAverage
│   │   ├── statistical.py     # ExponentialSmoothing (statsmodels)
│   │   └── ml.py              # XGBoost, Prophet
│   ├── backtesting.py         # Expanding window backtesting
│   ├── surrogate.py           # Tree surrogate (XGBoost / LightGBM / CatBoost / sklearn GB)
│   ├── explainer.py           # TsSHAPExplainer (main API)
│   └── explanations.py        # Plotting helpers: importance + forecast comparison
├── evaluation/
│   ├── synthetic_data.py      # Synthetic trend+seasonal generator + component metadata
│   ├── metrics.py             # surrogate_accuracy (MAPE vs black-box forecasts)
│   └── tsice_comparison.py    # TSICE wrapper + TsSHAP vs TSICE comparison
├── notebooks/
│   ├── 01_features_demo.ipynb          # Feature extraction showcase
│   ├── 02_synthetic_evaluation.ipynb   # End-to-end TsSHAP on synthetic data
│   └── 03_tsice_comparison.ipynb       # TsSHAP vs TSICE side-by-side
├── pyproject.toml           # Packaging metadata (pip / PyPI)
├── LICENSE
└── requirements.txt         # Full stack for local notebooks (optional extras below)

Quick Start

Install as a library (PyPI)

Distribution name on PyPI: tsshap-timeseries (import name remains tsshap).

pip install tsshap-timeseries          # core deps + sklearn surrogate fallback
pip install "tsshap-timeseries[all]"   # XGBoost, LightGBM, CatBoost, Prophet, seaborn

After you publish: python -m build then twine upload dist/* from this directory.

Install from source (editable)

cd final_project
pip install -e ".[all]"

Requirements file (notebooks / evaluation)

pip install -r requirements.txt
from tsshap import TsSHAPExplainer
from tsshap.features import STLFeatures, LagFeatures, SeasonalLagFeatures
from tsshap.forecasters import SeasonalNaiveForecaster
from tsshap.explanations import plot_importance

forecaster = SeasonalNaiveForecaster(period=12)

explainer = TsSHAPExplainer(
    forecaster=forecaster,
    feature_extractors=[
        STLFeatures(period=12),
        LagFeatures(lags=3),
        SeasonalLagFeatures(lags=2, period=12),
    ],
    horizon=12,
)

explainer.fit(y_train)

global_result   = explainer.explain(scope='global')
local_result    = explainer.explain(scope='local', t=-1)
semilocal_result = explainer.explain(scope='semi-local', t_start=-12, t_end=None)

plot_importance(global_result)
print(global_result.top_features())

Algorithm Overview

TsSHAP works in three stages:

1. Backtesting      black-box forecaster  →  backtested forecast series
2. Feature extraction  original series    →  interpretable feature matrix X
3. Surrogate + SHAP    Tree ensemble on (X, backtested forecasts)  →  SHAP values

Feature families (in order of complexity):

Family Description
STLFeatures STL decomposition: trend, seasonal, residual (expanding window, no look-ahead)
LagFeatures y(t-1), y(t-2), ...
SeasonalLagFeatures y(t-m), y(t-2m), ...
RollingWindowFeatures mean/max/min/std over last k observations
ExpandingWindowFeatures mean/max/min/std over all past observations
TrendFeatures Polynomial time index t, t², ...
DateFeatures Month, quarter, season, weekday, etc. (requires DatetimeIndex)

Explanation scopes:

  • Local — SHAP values at a single time step
  • Semi-local — mean |SHAP| over a time interval
  • Global — mean |SHAP| over the entire series

Bundled black-box forecasters (tsshap.forecasters):

Class Notes
NaiveForecaster Repeat last value
SeasonalNaiveForecaster Repeat value from one seasonal period ago
MovingAverageForecaster Mean of trailing window
ExponentialSmoothingForecaster Simple exponential smoothing (statsmodels)
XGBoostForecaster Recursive lag regression; optional deps / OpenMP on some platforms
ProphetForecaster Prophet model; DatetimeIndex required on y

Evaluation (evaluation/metrics.py):

  • surrogate_accuracy — MAPE between surrogate predictions and black-box backtested forecasts (how well the surrogate mimics the forecaster outputs).

Notebooks

Notebook Content
01_features_demo.ipynb Visualise every feature family on the CO₂ series (STL, lag, seasonal lag, rolling / expanding windows, trend, calendar).
02_synthetic_evaluation.ipynb Six experiments: baseline TsSHAP, local / semi-local scopes, surrogate fidelity across all six forecasters (MAPE table + two-panel plots with forecaster suptitles), STL vs ground-truth variance shares, efficiency (surrogate backends + TreeSHAP vs KernelSHAP).
03_tsice_comparison.ipynb TsSHAP vs TSICE on synthetic data: side-by-side lag-importance bars (with forecaster suptitle on the Naive walkthrough), then the same comparison looped over all six forecasters.

Environment tip: XGBoost and Prophet need working installs (see requirements.txt or pip install -e ".[all]"). If tsicebox is installed, TSICE’s native path builds Series without the original time index; forecasters that require a DatetimeIndex (e.g. Prophet) are safer with the library’s manual ICE fallback (triggered when tsicebox is not importable) or after aligning that behavior in code.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tsshap_timeseries-0.1.1.tar.gz (21.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tsshap_timeseries-0.1.1-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file tsshap_timeseries-0.1.1.tar.gz.

File metadata

  • Download URL: tsshap_timeseries-0.1.1.tar.gz
  • Upload date:
  • Size: 21.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for tsshap_timeseries-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6981a92112a858494e95009a6d100244f0738ecd8ede9c20c43baf753b824d25
MD5 b600a3dae01d3eb9005b26077541d370
BLAKE2b-256 f3e818cb8ebea945d9d6086cc994ed676ea89d593d2113e2a63f957fec5bab1c

See more details on using hashes here.

File details

Details for the file tsshap_timeseries-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for tsshap_timeseries-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d104350ed87f6148cf03906334919e0d2d81b466afaace4e099019d2a1a47711
MD5 7809c573e80831d8f5994ae65330f7f1
BLAKE2b-256 6413fc89dd31e4bad7a6e231b61a592bbb9f3b8dcdecc97543c28334ed9b1537

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page