Explainable AI for panel time-series econometrics: structured (variable x lag x unit x time) SHAP, factor / cross-sectional-dependence-aware attribution, regime-aware explanation drift, constrained counterfactuals, bootstrap uncertainty, and hybrid econometric-core + ML-residual models.
Project description
panelxai
Explainable AI for panel time-series econometrics.
Most XAI work either explains time series (TimeSHAP, WindowSHAP, TFT) or applies SHAP to panel-shaped data — but rarely respects the econometric structure of a dynamic panel: distributed lags, cross-sectional dependence, common factors, unit heterogeneity, regime change, fixed effects.
panelxai closes that gap. A raw SHAP value is indexed only by
(observation, feature). Because the design builder encodes
variable, lag, and kind (own regressor vs cross-sectional-average
factor proxy) into every feature name, panelxai re-indexes attributions onto
the structure that matters:
variable × lag × unit × time + own-vs-factor (CSD) decomposition
Author: Dr Merwan Roudane · MIT License · pip install -e .
What it provides
| Capability | Function / class | Idea |
|---|---|---|
| Panel TS simulator | simulate_panel_ts |
DGP with true lag drivers, common factors (CSD), regime switch, non-linearity |
| Lag + CCE design | build_design |
within-FE design with own lags and cross-sectional averages |
| Models | GBPanel, HybridPanel |
XGBoost on the design; or linear econometric core + ML residual |
| Structured SHAP | StructuredExplainer |
importance by variable, by lag, variable×lag matrix, per-unit |
| Factor-aware XAI | .own_vs_factor() |
share of explanation from own dynamics vs common factors / CSD |
| Regime-aware XAI | regime_importance, regime_effect_sign, explanation_drift |
importance & effect-sign flips across regimes; rolling drift |
| Counterfactual XAI | counterfactual |
minimal, box-constrained change to move a prediction |
| Uncertainty-aware XAI | bootstrap_importance |
unit cluster-bootstrap CIs + stability flag for importances |
| Plots / tables / report | plot_*, tables, Report |
publication-ready outputs |
Quick start
import panelxai as px
df = px.simulate_panel_ts(n_units=30, n_periods=40, n_features=4,
n_lags=2, n_factors=1, seed=7)
model = px.GBPanel(lags=2, csa=True).fit(df) # explains the full model
ex = px.StructuredExplainer(model)
ex.variable_importance() # which variable matters
ex.lag_importance() # at which lag (temporal profile)
ex.variable_lag_matrix("own") # variable × lag heatmap data
ex.own_vs_factor() # own dynamics vs common-factor / CSD share
ex.unit_importance() # cross-sectional heterogeneity of explanations
px.plot_variable_lag_heatmap(ex, save="varlag.png")
On the built-in DGP (x1 drives y at lag 0, x2 at lag 1, x3 at lag 2) the
structured SHAP recovers exactly that — each variable's mass concentrates at
its true lag.
Hybrid econometric core + ML
m = px.HybridPanel(lags=2).fit(df) # y = linear dynamic-panel core + ML residual
print(px.Report(m).text()) # SHAP here explains only the NON-LINEAR part
Regime-aware (sign-flip detection)
df = px.simulate_panel_ts(regime_break=0.5, seed=11) # x1 effect flips at midpoint
ex = px.StructuredExplainer(px.GBPanel(lags=2).fit(df))
px.regime_effect_sign(ex, n_regimes=2) # x1: +corr -> -corr => sign_flip = True
Uncertainty
boot = px.bootstrap_importance(lambda: px.GBPanel(lags=2),
df, n_boot=30, level=0.90)
boot # mean, lo, hi, stable (CI excludes zero)
Examples
Runnable scripts in examples/:
01_structured_shap.py— variable × lag × own/factor decomposition02_regime_drift.py— regime sign-flip & explanation drift03_hybrid_counterfactual.py— hybrid model + constrained counterfactual04_uncertainty.py— bootstrap importance CIs
Design notes
- Fixed effects are absorbed by the
fe="within"transform, so SHAP explains within-unit deviations (the dynamic signal), not level differences. - Cross-sectional dependence is handled CCE-style: period-
tcross-sectional averages enter the design as proxies for unobserved common factors; their SHAP share is reported separately from own dynamics. - The hybrid model keeps a transparent linear econometric core and lets the ML learner — and SHAP — speak only to the residual non-linearity, the honest target of post-hoc XAI in an econometric setting.
Status & scope
v0.1.0 implements the structured / temporal, factor-aware, regime-aware, counterfactual, uncertainty, and hybrid families on a tree-ensemble backbone. Planned extensions: causal/interventional SHAP, deep-sequence models (LSTM/TFT) with Integrated Gradients, and graph XAI for network panels.
Requirements
Python ≥ 3.9; numpy, pandas, scipy, scikit-learn, statsmodels, matplotlib, shap, xgboost, linearmodels, tabulate.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file panelxai-0.1.0.tar.gz.
File metadata
- Download URL: panelxai-0.1.0.tar.gz
- Upload date:
- Size: 22.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
74338ad3ac5411a3da8215f5c6e104b581c695d017be394a74ad14cd5149833f
|
|
| MD5 |
a140cfab610c1ced45434bbb3c3c7fa5
|
|
| BLAKE2b-256 |
4f7e3b54b99f400d2e03143bd947cf56cb64bbcb7fd933862b8013ca8283e44c
|
File details
Details for the file panelxai-0.1.0-py3-none-any.whl.
File metadata
- Download URL: panelxai-0.1.0-py3-none-any.whl
- Upload date:
- Size: 23.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf4b569f41a4326d14b206ad6fc65ebc349bc64ad7b338444589381f35553257
|
|
| MD5 |
b889c40e3f8e93682acb42300a485861
|
|
| BLAKE2b-256 |
67abacd847e4e8cc410670f833b298343cd9b9e2b8967bc18c9e8c13c79e1783
|