Skip to main content

PyCMTensor

PyCMTensor

Conda Forge Python Version Conda-Forge|downloads Codecov Licence DOI

Advanced Tensor-based Choice Modelling Python package

Introduction | Install | Key Features


Introduction

PyCMTensor is a Python library that use tensor-based computations for discrete choice modelling and estimation. It specializes in the estimation of hybrid neural networks, Logit models, and Mixed Logit models. The models in PyCMTensor are built on computational graphs and are estimated using generalized backpropagation algorithms.

This library provides the tools to fully specify and estimate Multinomial Logit and Mixed Logit models. It also enables the generation of statistical test results for comprehensive econometric analysis. With PyCMTensor, you can perform model estimation using computational graphs, making it a powerful tool for advanced statistical modelling.

Installation

PyCMTensor is available on Conda-forge:

conda install -c conda-forge pycmtensor

Quick start

A simple Multinomial logit model. Dataset: Swissmetro

import pandas as pd
from pycmtensor import train
from pycmtensor.dataset import Dataset
from pycmtensor.expressions import Beta
from pycmtensor.models import MNL
import pycmtensor.optimizers as optim

def mnl_model(filename='http://transp-or.epfl.ch/data/swissmetro.dat'):
    # Load the CSV file into a DataFrame
    df = pd.read_csv(filename, sep='\t')

    # Load the DataFrame into a Dataset object
    ds = Dataset(df, choice="CHOICE")
    ds.scale_variable("TRAIN_TT", 100)
    ds.scale_variable("SM_TT", 100)
    ds.scale_variable("CAR_TT", 100)
    ds.scale_variable("TRAIN_CO", 100)
    ds.scale_variable("SM_CO", 100)
    ds.scale_variable("CAR_CO", 100)
    ds.split(0.8)

    # Define the alternative specific constants (ASCs) for each mode of transport
    ASC_TRAIN = Beta("ASC_TRAIN", 0., None, None, 0)
    ASC_SM = Beta("ASC_SM", 0., None, None, 0)
    ASC_CAR = Beta("ASC_CAR", 0., None, None, 1)
    B_COST = Beta("B_COST", -1., None, None, 1)
    B_TIME_TRAIN = Beta("B_TIME_TRAIN", 0., None, None, 0)
    B_TIME_SM = Beta("B_TIME_SM", 0., None, None, 0)
    B_TIME_CAR = Beta("B_TIME_CAR", 0., None, None, 0)
    B_SEAT = Beta("B_SEAT", 0., None, None, 0)

    # Define the utility functions for each mode of transport
    V_TRAIN = ASC_TRAIN + B_TIME_TRAIN * ds["TRAIN_TT"] + B_COST * ds["TRAIN_CO"]
    V_SM = ASC_SM + B_TIME_SM * ds["SM_TT"] + B_COST * ds["SM_CO"] + B_SEAT * ds["SM_SEATS"]
    V_CAR = ASC_CAR + B_TIME_CAR * ds["CAR_TT"] + B_COST * ds["CAR_CO"]

    # Define the model
    U = [V_TRAIN, V_SM, V_CAR]
    AV = [ds["TRAIN_AV"], ds["SM_AV"], ds["CAR_AV"]]
    model = MNL(ds, locals(), utility=U, av=AV)
    
    return model, ds

# Train model
model, ds = mnl_model()
train(model, ds, optimizer=optim.Adam, max_epochs=2000, base_learning_rate=0.1, convergence_threshold=1e-3)

# Print model results
print(model.results.beta_statistics())
print(model.results.model_statistics())
print(model.results.benchmark())

Key Features

  • Interpretable and customizable utility specification syntaxes: Easily define your models with an intuitive syntax.
  • Neural network specification: Specify neural networks with weight and bias parameters inside utility functions (e.g., TasteNet).
  • Comprehensive analysis tools: Perform specification testing, analyze covariances, and compute standard errors for taste parameters.
  • Fast model estimation: Quickly estimate models, including simulation-based methods like Mixed Logit models, using a computational graph approach.
  • Flexible optimization methods: Tune the model estimation with 1st order methods (e.g., Adam, Stochastic Gradient Descent) or 1.5th order methods (e.g., Stochastic BFGS).

While other choice modelling estimation software in Python are available, such as Biogeme, xlogit, and PyLogit, PyCMTensor sets itself apart by fully implementing deep learning-based methods with a simplified syntax for utility equation specification.

Documentation

For more information on how to use PyCMTensor, please refer to our documentation.

Metadata

Release files for pycmtensor 1.14.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pycmtensor 1.14.2
File Size Uploaded
pycmtensor-1.14.2.tar.gz 41.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pycmtensor 1.14.2
File Interpreter ABI Platform
pycmtensor-1.14.2-py3-none-any.whl Python 3 none any Details

Total release size: 90.6 kB

Release files / pycmtensor-1.14.2.tar.gz

Download URL pycmtensor-1.14.2.tar.gz
Size 41.0 kB
Tags Source
SHA-256 checksum
How to use checksums
90b3e3b868e517cb17a9816721d7f4f2b316f5b5238efc3146d976dc57ff26e6
BLAKE2b-256 checksum
How to use checksums
d8491f93843bd1cf67c99a0a586d9e0a340e504dd1d81e3296c2042691aae2f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.12 Linux/6.11.0-1014-azure

Release files / pycmtensor-1.14.2-py3-none-any.whl

Download URL pycmtensor-1.14.2-py3-none-any.whl
Size 49.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e2d5d41a8e5dc2b874df6a7beac97e3c515f869926cbcda07a108c409068d2d8
BLAKE2b-256 checksum
How to use checksums
87de288a7250e5cb3cf33651f63bb9ac16b9d1522d6ef04422b0344bd483cdc4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.12 Linux/6.11.0-1014-azure

Release history Release notifications | RSS feed

This release

1.14.2 This release

2 release files

1.14.1

2 release files

1.14.0

2 release files

1.13.3

2 release files

1.13.1

2 release files

1.13.0

2 release files

1.11.1

2 release files

1.11.0

2 release files

1.10.0

2 release files

1.9.2

2 release files

1.9.1

2 release files

1.9.0

2 release files

1.8.1

2 release files

1.8.0

2 release files

1.7.1

2 release files

1.7.0

2 release files

1.6.4

2 release files

1.6.3

2 release files

1.6.2

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.7

2 release files

1.0.3

2 release files

1.0.2

2 release files

0.7.0

2 release files

0.6.4

2 release files

0.6.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page