Skip to main content

TorchDCM

TorchDCM logo

PyTorch-first discrete choice model estimation and econometric inference.

Quick Start | Model Zoo | Installation | Benchmarks | Development

PyPI Python PyTorch Models Benchmarks Status

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 software-paper repository is separate and contains public benchmark data, validation wrappers, plots, comparison tables, generated results, and LaTeX:

torchdcm-paper: validation, benchmarks, datasets, and manuscript

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 with examples and package-level tests.
Benchmark companion Full comparisons live in torchdcm-paper 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.

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.

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/ Minimal runnable usage examples.
docs/assets/ GitHub README logo and cover assets.
pyproject.toml Packaging metadata and dependencies.

Development

python -m pip install -e ".[dev]"
pytest

Benchmark and manuscript work should happen in the companion repository:

git clone https://github.com/mbc96325/torchdcm-paper.git

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torchdcm-0.1.0.tar.gz (59.2 kB view details)

Uploaded Source

Built Distribution

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

torchdcm-0.1.0-py3-none-any.whl (75.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: torchdcm-0.1.0.tar.gz
  • Upload date:
  • Size: 59.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for torchdcm-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b0a54b11d04071c7c85ca82ac1c5ed1565b398c93f87ebb3e2cc02898dec66fd
MD5 c7561c3129248f65fb654606d0bc6b87
BLAKE2b-256 00845b859a636169888babc110bbc1ac59d8e348f5df7613255020c0e8cc54ea

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchdcm-0.1.0.tar.gz:

Publisher: publish.yml on mbc96325/torchdcm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: torchdcm-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 75.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for torchdcm-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d956aa21a245f471a33786660f2136c12425fc7f42d8b906f91b37c35d95d0c1
MD5 976407bea64717ac4bc86961188d726a
BLAKE2b-256 02208810ed53721af887efa2a5487d33944cc508a714dcf628ec065601cc5fbd

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchdcm-0.1.0-py3-none-any.whl:

Publisher: publish.yml on mbc96325/torchdcm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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