Panelformer
Panelformer is a transformer-based deep learning model designed for accurate and scalable panel time series forecasting. Built on top of the Temporal Fusion Transformer (TFT), Panelformer introduces several innovations to address the limitations of existing models when applied to heterogeneous panel datasets.
🔑 Keywords
Panel time series, Temporal Fusion Transformer, Transformer architecture, Forecasting, Panelformer
🔍 Overview
Panel time series involve multiple entities (e.g., countries, products) observed over time — requiring models to handle both temporal and cross-sectional complexity. Panelformer enhances forecasting accuracy across diverse panel structures by integrating the following features:
- Segment-wise Attention: Reduces complexity and captures local patterns in long sequences.
- Multi-Scale Series Decomposition: Separates trend and seasonal components using learnable moving averages.
- Cross-Entity Attention: Models dependencies between different panel entities.
- Parallel Trend-Seasonal Paths: Dedicated processing for distinct temporal dynamics.
- Adaptive Component Weighting: Learns to prioritize seasonal vs. trend features dynamically.
🚀 Key Results
Evaluated on 11 real-world datasets spanning economics, energy, climate, and health domains, Panelformer achieved:
- 7.99% average MAPE improvement over baseline TFT
- Robust performance on balanced/unbalanced, short/long, micro/macro panels
- Significant accuracy gains on high-frequency datasets like foreign exchange, surface temperature, and electricity
📦 Installation
pip install panelformer
🛠️ How to Use
Using Panelformer involves four main steps:
🧬 Step 1: Data Preparation
Prepare your panel dataset with columns for entity IDs, timestamps, and target values. Convert your date column to datetime format and create a sequential time index for modeling.
🧩 Step 2: Dataset Creation
Use the pytorch_forecasting library’s TimeSeriesDataSet class to define training, validation, and test datasets. This includes specifying encoder and decoder lengths, grouping by entity, and applying normalization.
🏋️ Step 3: Train the Model
Instantiate the Panelformer model using the prepared dataset, configure hyperparameters (like learning rate, hidden sizes, attention heads), and train with a PyTorch Lightning Trainer for efficient training management.
from Panelformer import Panelformer
import lightning.pytorch as pl
from lightning.pytorch.callbacks import EarlyStopping, LearningRateMonitor
from lightning.pytorch.loggers import TensorBoardLogger
from pytorch_forecasting.metrics import QuantileLoss
early_stop_callback = EarlyStopping(monitor="val_loss", min_delta=1e-10, patience=5, verbose=False, mode="min")
lr_logger = LearningRateMonitor()
logger = TensorBoardLogger("lightning_logs")
trainer = pl.Trainer(
max_epochs = 50,
enable_model_summary=True,
gradient_clip_val=0.1,
callbacks=[lr_logger, early_stop_callback],
logger=logger,
)
model = Panelformer.from_dataset(
training,
learning_rate=0.001,
hidden_size=16,
attention_head_size=2,
dropout=0.1,
segment_size=4,
decomposition_kernel_sizes=[3, 7, 15, 31],
trend_processing_layers=2,
use_cross_series_attention=True,
adaptive_trend_weight=True,
hidden_continuous_size=8,
loss=QuantileLoss()
)
trainer.fit(model, train_dataloaders=train_dataloader, val_dataloaders=val_dataloader)
📈 Step 4: Make Predictions
Use the trained model to predict on new data by passing a test dataloader, retrieving forecasts for evaluation or downstream use.
predictions = model.predict(test_dataloader, mode="raw", return_x=True)
🧪 Step 5: Evaluation Utilities
Panelformer comes with a built-in utility module to simplify model evaluation and visualization. This includes standard regression metrics and diagnostic plots to help assess performance.
✅ Features:
- Residual analysis (
Residuals vs Predictions) - Actual vs Predicted scatter plot
- Error distribution histogram
- Standard metrics: R², MAPE, MSE, RMSE, MAE
- Correlation metrics: Pearson, Spearman
- Standardized error metrics (optional)
🔧 Example Usage:
from Panelformer import utils
# Evaluate on validation set
print("Validation Metrics:")
val_r2, val_mape, val_mse, val_rmse, val_mae, val_error_std, val_pearson_corr, val_spearman_corr = utils.calculate_metrics(val_dataloader, model)
# Evaluate on test set
print("\nTest Metrics:")
test_r2, test_mape, test_mse, test_rmse, test_mae, test_error_std, test_pearson_corr, test_spearman_corr = utils.calculate_metrics(test_dataloader, model)
🎯 Output:
-
Metrics printed to console
-
Diagnostic plots shown:
- Residuals vs Predictions
- Actual vs Predicted
- Error Distribution
This helps visually diagnose model behavior, including overfitting, heteroscedasticity, or bias in predictions.
🛠 License
Released under the MIT License. Built on top of PyTorch Forecasting.
Metadata
Release files for Panelformer 0.2.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| panelformer-0.2.4.tar.gz | 12.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| panelformer-0.2.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 28.3 kB
Release files / panelformer-0.2.4.tar.gz
| Download URL | panelformer-0.2.4.tar.gz |
|---|---|
| Size | 12.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7294534f28ffae73db475acc63d8d9f5c7860f55a43a00fe597c384614d08c46
|
|
BLAKE2b-256 checksum How to use checksums |
526a506047f8e6a95c4c72962c50077c29608cba4227032bea7ef72e5590254d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 4, 2025.
Transparency logRelease files / panelformer-0.2.4-py3-none-any.whl
| Download URL | panelformer-0.2.4-py3-none-any.whl |
|---|---|
| Size | 15.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f26f7426ba2f43ea62c02e6fd296b4526ce4bd1e45b2f81e898797e7338280a4
|
|
BLAKE2b-256 checksum How to use checksums |
4eabee185ed5432156cd1499291b1cc97f0bca242b0c89e04a5dfd6ed2c539a4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 4, 2025.
Transparency log