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
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 torchdcm-0.1.2.tar.gz.
File metadata
- Download URL: torchdcm-0.1.2.tar.gz
- Upload date:
- Size: 74.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a048ee35cbd668cfaa1883c99405ca41525a2c1404279e5e3688de4c352983c
|
|
| MD5 |
a2ee2c9c6e8285673af149150ed52458
|
|
| BLAKE2b-256 |
95984e6081048951c808a2ed0ebb7e46436de20f5e7e93b31c9f891fb6e55bb1
|
Provenance
The following attestation bundles were made for torchdcm-0.1.2.tar.gz:
Publisher:
publish.yml on mbc96325/torchdcm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchdcm-0.1.2.tar.gz -
Subject digest:
6a048ee35cbd668cfaa1883c99405ca41525a2c1404279e5e3688de4c352983c - Sigstore transparency entry: 2288587781
- Sigstore integration time:
-
Permalink:
mbc96325/torchdcm@86bd009795b86f4589403c34c988161d02eb94cc -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/mbc96325
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@86bd009795b86f4589403c34c988161d02eb94cc -
Trigger Event:
release
-
Statement type:
File details
Details for the file torchdcm-0.1.2-py3-none-any.whl.
File metadata
- Download URL: torchdcm-0.1.2-py3-none-any.whl
- Upload date:
- Size: 87.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d6695d65de44b85297cf9c554df92b756cc7ca8e2c4b7b5b12241abbba08099d
|
|
| MD5 |
bacfae15ba438cddf93c8c0cb42621ab
|
|
| BLAKE2b-256 |
c88b9150a0401fd481eb0ffc9669cc174962518750df4df22c9fdc9d22881542
|
Provenance
The following attestation bundles were made for torchdcm-0.1.2-py3-none-any.whl:
Publisher:
publish.yml on mbc96325/torchdcm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchdcm-0.1.2-py3-none-any.whl -
Subject digest:
d6695d65de44b85297cf9c554df92b756cc7ca8e2c4b7b5b12241abbba08099d - Sigstore transparency entry: 2288587811
- Sigstore integration time:
-
Permalink:
mbc96325/torchdcm@86bd009795b86f4589403c34c988161d02eb94cc -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/mbc96325
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@86bd009795b86f4589403c34c988161d02eb94cc -
Trigger Event:
release
-
Statement type: