Skip to main content

Modular customer-base forecasting: multinomial LSTM / Transformer + Pareto/NBD benchmarks for per-customer transaction-count prediction.

Project description

panelclv

Modular LSTM and Transformer models for customer-base transaction-count forecasting, with Pareto/NBD benchmarks. The thesis target is the Valendin et al. workflow: the models are classifiers over transaction-count classes that forecast by autoregressive Monte Carlo simulation (sample a count per period, feed it back, average many paths) — not point regressors.

Install

From the repo root (use your PyTorch venv):

pip install -e .

The project uses a src-layout (the package lives in src/panelclv/), so installing it is what puts panelclv on the path — there are no sys.path hacks. It is split by concern into subpackages: panelclv.models, panelclv.training, panelclv.tuning, panelclv.evaluation, panelclv.benchmarks, panelclv.experiments, panelclv.data_preparation, panelclv.configs. Import from the relevant one, e.g. from panelclv.tuning import run_optuna_study. For the test runner, use pip install -e ".[dev]" and run pytest.

Quickstart

The whole flow is: build/load a panel → prepare tensors → tune (Optuna) → rebuild the winning model → Monte Carlo forecast → report. The three panelclv.experiments helpers (make_data_builder, build_inference_from_trial, and make_loaders) absorb the mechanical glue so the notebook stays in control of every modeling choice.

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split

from panelclv.configs.panel_config import PanelConfig
from panelclv.data_preparation import dynamic_panel_dataset
from panelclv.tuning import run_optuna_study
from panelclv.experiments import make_data_builder, build_inference_from_trial
from panelclv.models import mc_forecast, mc_compute_metrics

# 1. Panel -> model-ready tensors (calibration/holdout/samples/targets/seq_cols/...).
panel = pd.read_csv("Datasets/Dataset_clean/electronics_customer_week_panel.csv")
cfg = PanelConfig(id_col="Id", target_col="Transactions", frequency="weekly",
                  training_start="1999-01-01", training_end="2000-12-31",
                  holdout_start="2001-01-01", holdout_end="2001-12-31",
                  time_cols=("year", "week"), clip_target_upper=6)
data_full = dynamic_panel_dataset.prepare_dataset(panel, cfg)

# 2. Customer-wise split (rows are customers).
train_idx, val_idx = train_test_split(np.arange(data_full["N"]), test_size=0.1,
                                       random_state=42)

# 3. Tune. make_data_builder gives run_optuna_study the per-trial data closure; every
#    other knob (selection_metric, removable_features, loss config, rollout_*) stays
#    yours to set here.
study = run_optuna_study(
    model_type="lstm",
    data_builder=make_data_builder(data_full, train_idx, val_idx),
    data_info={"n_epochs": 150, "patience": 7,
               "checkpoint_dir": "./checkpoints/lstm_optuna", "loss_type": "cross_entropy"},
    n_trials=30,
)

# 4. Rebuild the winning model + load its checkpoint. Returns the model AND data_best
#    (data sliced to the winning feature subset) -- always forecast with data_best.
inference_model, data_best = build_inference_from_trial(study, data_full, "lstm")

# 5. Autoregressive Monte Carlo forecast + metrics.
forecast = mc_forecast(inference_model, data_best, n_simulations=600, seed=42)
print(mc_compute_metrics(forecast["actual"], forecast["prediction_mean"]))

Swap model_type="lstm" / "transformer" (and mc_forecast / mc_forecast_transformer) to run the other family on the same contract.

Notebooks

All notebooks live in notebooks/. notebooks/Data_integration_LSTM_v2.ipynb and notebooks/Data_integration_TRANSFORMER_v2.ipynb are the runnable, annotated walkthroughs of the flow above (built on the helpers); the un-suffixed Data_integration_{LSTM,TRANSFORMER}.ipynb are kept for reference, and dataset_building.ipynb builds the clean panels from raw data. Each notebook opens with a small bootstrap cell that locates the repo root and makes panelclv importable, so they run whether or not the package is pip-installed.

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

panelclv-0.1.0.tar.gz (75.0 kB view details)

Uploaded Source

Built Distribution

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

panelclv-0.1.0-py3-none-any.whl (85.2 kB view details)

Uploaded Python 3

File details

Details for the file panelclv-0.1.0.tar.gz.

File metadata

  • Download URL: panelclv-0.1.0.tar.gz
  • Upload date:
  • Size: 75.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for panelclv-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7d634fe2719b43382d83e71d65836e707c8322a33d971217ea40c1535f4da6ac
MD5 6b1ab03f40f402a962aebe3915b0a95e
BLAKE2b-256 d6b817c77ec9329032e4b0edad47165671ddefc43aa1fd924c59b8cff28ccf90

See more details on using hashes here.

File details

Details for the file panelclv-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: panelclv-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 85.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for panelclv-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4988cc5f7ec3ca2e5ed6b4fcc6be5d8a393c1f318d2de7f466911b6e57a35ca7
MD5 c405352383ec185e68473f55bdf7d2f8
BLAKE2b-256 29ed6967bbfcdc806636150941a85c40c450ace03f35cd339dd5dff3377d4859

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