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.
Metadata
Release files for panelxai 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| panelxai-0.1.0.tar.gz | 22.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| panelxai-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 45.7 kB
Release files / panelxai-0.1.0.tar.gz
| Download URL | panelxai-0.1.0.tar.gz |
|---|---|
| Size | 22.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
74338ad3ac5411a3da8215f5c6e104b581c695d017be394a74ad14cd5149833f
|
|
BLAKE2b-256 checksum How to use checksums |
4f7e3b54b99f400d2e03143bd947cf56cb64bbcb7fd933862b8013ca8283e44c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.0
|
Release files / panelxai-0.1.0-py3-none-any.whl
| Download URL | panelxai-0.1.0-py3-none-any.whl |
|---|---|
| Size | 23.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cf4b569f41a4326d14b206ad6fc65ebc349bc64ad7b338444589381f35553257
|
|
BLAKE2b-256 checksum How to use checksums |
67abacd847e4e8cc410670f833b298343cd9b9e2b8967bc18c9e8c13c79e1783
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.0
|
Security for panelxai 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.