Skip to main content

A robust and efficient Python package leveraging tensor computations for advanced discrete choice modelling.

Project description

PyCMTensor

PyCMTensor

Licence PyPI version codecov Downloads 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.dataset import Dataset
from pycmtensor.expressions import Beta
from pycmtensor.models import MNL, train
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.

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

pycmtensor-1.9.2.tar.gz (38.6 kB view details)

Uploaded Source

Built Distribution

pycmtensor-1.9.2-py3-none-any.whl (44.2 kB view details)

Uploaded Python 3

File details

Details for the file pycmtensor-1.9.2.tar.gz.

File metadata

  • Download URL: pycmtensor-1.9.2.tar.gz
  • Upload date:
  • Size: 38.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.7 Linux/6.2.0-1019-azure

File hashes

Hashes for pycmtensor-1.9.2.tar.gz
Algorithm Hash digest
SHA256 da23b357500ae0dfcdda1e1f18bd82172ced019765f688e87db3df290138cea7
MD5 388d8a4eb1dc71a427e7df40e36f5752
BLAKE2b-256 3902ba90b61848cbab2badd4258723792b66b6d4b2411abc2bd4ee9f48c7d32e

See more details on using hashes here.

Provenance

File details

Details for the file pycmtensor-1.9.2-py3-none-any.whl.

File metadata

  • Download URL: pycmtensor-1.9.2-py3-none-any.whl
  • Upload date:
  • Size: 44.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.7 Linux/6.2.0-1019-azure

File hashes

Hashes for pycmtensor-1.9.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6af8f55c70fd07d2ad28713b2ed40f858a795e221bfada900f34ca97df40b368
MD5 2ab63ee33f04d943269d571b9184b9a0
BLAKE2b-256 eb5a86afbcc4a0c5da470da3b6ac9e7297821579d5ef87b5f746000f40d6b07b

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page