TorchDCM
PyTorch-first discrete choice model estimation and econometric inference.
Installation | Quick Start | Examples | Model Zoo | Development
TorchDCM is the importable Python package for discrete choice model estimation.
This repository is intentionally package-first: it keeps the reusable
torchdcm implementation, unit tests, examples, and packaging metadata in one
small repo that users can install and import directly.
The paper's reproducibility repository is separate and contains benchmark runners, aligned datasets, validation utilities, and committed outputs:
torchdcm-evaluation-benchmark: reproduce the validation and benchmark results
Why TorchDCM
| Goal | What TorchDCM Provides |
|---|---|
| PyTorch-native estimation | Vectorized likelihoods written around tensors and automatic differentiation. |
| Econometric outputs | Classic, robust, and cluster covariance; WTP and elasticity helpers. |
| Model coverage | MNL, NL, CNL, mixed logit, WTP-space, latent class, scaled, ordered, and hybrid choice. |
| Reusable package | Clean import surface, executed model notebooks, and package-level tests. |
| Benchmark companion | Full comparisons live in torchdcm-evaluation-benchmark and import this package. |
Installation
python -m pip install torchdcm
For local development:
python -m venv .venv
source .venv/bin/activate
python -m pip install -U pip
python -m pip install -e ".[dev]"
pytest
Quick Start
import torch
from torchdcm import Beta, ChoiceDataset, MultinomialLogit, UtilitySpec
from torchdcm.datasets import make_swissmetro_like
df = make_swissmetro_like(n_obs=300, seed=7)
data = ChoiceDataset.from_wide(
df,
alternatives=["TRAIN", "SM", "CAR"],
choice="choice",
variables={
"time": {"TRAIN": "time_train", "SM": "time_sm", "CAR": "time_car"},
"cost": {"TRAIN": "cost_train", "SM": "cost_sm", "CAR": "cost_car"},
},
availability={
"TRAIN": "avail_train",
"SM": "avail_sm",
"CAR": "avail_car",
},
individual_id="person_id",
)
spec = UtilitySpec()
spec.utility(
"TRAIN",
Beta("ASC_TRAIN")
+ Beta("B_TIME", init=-0.01) * "time"
+ Beta("B_COST", init=-0.1) * "cost",
)
spec.utility(
"SM",
Beta("B_TIME", init=-0.01) * "time"
+ Beta("B_COST", init=-0.1) * "cost",
)
spec.utility(
"CAR",
Beta("ASC_CAR")
+ Beta("B_TIME", init=-0.01) * "time"
+ Beta("B_COST", init=-0.1) * "cost",
)
device = "cuda" if torch.cuda.is_available() else "cpu"
result = MultinomialLogit(spec, device=device).fit(data, cov_type="cluster", groups="person_id")
print(result.summary())
summary() renders an organized console report covering the model and data,
convergence diagnostics, fit statistics, inference, alternative shares, and
parameter estimates. The same structured report can be inspected as tables or
saved as a reproducible artifact directory:
report = result.report(cov_type="cluster", confidence_level=0.95)
parameter_table = report.parameters
result.save_report(
"outputs/swissmetro_mnl",
formats=["html", "json", "csv", "latex", "text"],
)
The output directory contains a readable HTML report, a machine-readable JSON
record, parameter/covariance/correlation CSV files, a LaTeX fragment, and a
plain-text summary. The convergence section records the exact L-BFGS stopping
condition, the internal-parameter gradient infinity norm, and that norm divided
by the number of observations. Function- or step-tolerance stops are reported
as converged when the normalized gradient does not exceed 1e-5; iteration
limits, non-finite values, and larger normalized gradients remain warnings.
All estimators accept a standard PyTorch-style device argument. Passing
device="cuda" moves estimation, prediction, simulated likelihoods, and
covariance calculations for that model to CUDA when your PyTorch installation
has GPU support.
Executed Examples
The examples/ directory contains self-contained Jupyter
notebooks for every public model family. Each notebook presents the model's
mathematical formulation, builds a nontrivial specification, runs full
estimation, and retains its rendered HTML report. They were executed on an AMD
Ryzen 9 9950X3D CPU (16 cores), 64 GB RAM, and an NVIDIA GeForce RTX 5090 GPU
(32 GB VRAM), running Ubuntu 24.04.4 with PyTorch 2.12.1 and CUDA 13.0. The
examples automatically select CUDA when it is available and can be changed to
CPU by setting device = "cpu".
| Model | Executed notebook |
|---|---|
| Multinomial logit | 01_multinomial_logit.ipynb |
| Nested logit | 02_nested_logit.ipynb |
| Cross-nested logit | 03_cross_nested_logit.ipynb |
| Mixed logit | 04_mixed_logit.ipynb |
| WTP-space mixed logit | 05_wtp_space_mixed_logit.ipynb |
| Alternative-scaled MNL | 06_scaled_multinomial_logit.ipynb |
| Covariate-scaled MNL | 07_covariate_scaled_multinomial_logit.ipynb |
| Ordered logit | 08_ordered_logit.ipynb |
| Ordered probit | 09_ordered_probit.ipynb |
| Latent-class logit | 10_latent_class_logit.ipynb |
| Error-components logit | 11_error_components_logit.ipynb |
| Hybrid choice | 12_hybrid_choice.ipynb |
| Panel mixed logit | 13_panel_mixed_logit.ipynb |
| Panel multinomial logit with cluster covariance | 14_panel_multinomial_logit.ipynb |
Model Zoo
The current package includes:
- multinomial logit / conditional logit;
- nested logit and cross-nested logit;
- mixed logit, WTP-space mixed logit, and error-components logit;
- latent-class logit;
- scaled and covariate-scaled multinomial logit;
- ordered logit and ordered probit;
- hybrid choice with latent variables and Gaussian measurement indicators;
- classic, robust, and cluster covariance estimates;
- WTP and elasticity helpers.
Repository Layout
| Path | Purpose |
|---|---|
torchdcm/ |
Importable package implementation. |
tests/ |
Package-level unit tests. |
examples/ |
Executed Jupyter notebooks covering every public model family. |
docs/assets/ |
GitHub README logo and cover assets. |
pyproject.toml |
Packaging metadata and dependencies. |
License
TorchDCM is released under the MIT License.
Development
python -m pip install -e ".[dev]"
pytest
Benchmark and validation work should happen in the companion repository:
git clone https://github.com/mbc96325/torchdcm-evaluation-benchmark.git
Metadata
Release files for torchdcm 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| torchdcm-0.1.2.tar.gz | 74.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| torchdcm-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 161.8 kB
Release files / torchdcm-0.1.2.tar.gz
| Download URL | torchdcm-0.1.2.tar.gz |
|---|---|
| Size | 74.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6a048ee35cbd668cfaa1883c99405ca41525a2c1404279e5e3688de4c352983c
|
|
BLAKE2b-256 checksum How to use checksums |
95984e6081048951c808a2ed0ebb7e46436de20f5e7e93b31c9f891fb6e55bb1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Jul 30, 2026.
Transparency logRelease files / torchdcm-0.1.2-py3-none-any.whl
| Download URL | torchdcm-0.1.2-py3-none-any.whl |
|---|---|
| Size | 87.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d6695d65de44b85297cf9c554df92b756cc7ca8e2c4b7b5b12241abbba08099d
|
|
BLAKE2b-256 checksum How to use checksums |
c88b9150a0401fd481eb0ffc9669cc174962518750df4df22c9fdc9d22881542
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Jul 30, 2026.
Transparency log