Skip to main content

xlogit: A Python Package for GPU-Accelerated Estimation of Mixed Logit Models.

https://raw.githubusercontent.com/arteagac/xlogit/master/docs/xlogit_logo_1000px.png

Build Coverage Community Documentation Status PyPi License

Examples | Docs | Installation | API Reference | Contributing | Contact

Quick start

The following example uses xlogit to estimate a mixed logit model for choices of electricity supplier (See the data here). The parameters are:

  • X: 2-D array of input data (in long format) with choice situations as rows, and variables as columns

  • y: 1-D array of choices (in long format)

  • varnames: List of variable names that matches the number and order of the columns in X

  • alts: 1-D array of alternative indexes or an alternatives list

  • ids: 1-D array of the ids of the choice situations

  • panels: 1-D array of ids for panel formation

  • randvars: dictionary of variables and their mixing distributions ("n" normal, "ln" lognormal, "t" triangular, "u" uniform, "tn" truncated normal)

The current version of xlogit only supports input data in long format.

# Read data from CSV file
import pandas as pd
df = pd.read_csv("examples/data/electricity_long.csv")

# Fit the model with xlogit
from xlogit import MixedLogit

varnames = ['pf', 'cl', 'loc', 'wk', 'tod', 'seas']
model = MixedLogit()
model.fit(X=df[varnames],
          y=df['choice'],
          varnames=varnames,
          ids=df['chid'],
          panels=df['id'],
          alts=df['alt'],
          n_draws=600,
          randvars={'pf': 'n', 'cl': 'n', 'loc': 'n',
                    'wk': 'n', 'tod': 'n', 'seas': 'n'})
model.summary()
GPU processing enabled.
Optimization terminated successfully.
         Current function value: 3888.413414
         Iterations: 46
         Function evaluations: 51
         Gradient evaluations: 51
Estimation time= 2.6 seconds
----------------------------------------------------------------------
Coefficient         Estimate      Std.Err.         z-val         P>|z|
----------------------------------------------------------------------
pf                -0.9996286     0.0331488   -30.1557541     9.98e-100 ***
cl                -0.2355334     0.0220401   -10.6865870      1.97e-22 ***
loc                2.2307891     0.1164263    19.1605300      5.64e-56 ***
wk                 1.6251657     0.0918755    17.6887855      6.85e-50 ***
tod               -9.6067367     0.3112721   -30.8628296     2.36e-102 ***
seas              -9.7892800     0.2913063   -33.6047603     2.81e-112 ***
sd.pf              0.2357813     0.0181892    12.9627201      7.25e-31 ***
sd.cl              0.4025377     0.0220183    18.2819903      2.43e-52 ***
sd.loc             1.9262893     0.1187850    16.2166103      7.67e-44 ***
sd.wk             -1.2192931     0.0944581   -12.9083017      1.17e-30 ***
sd.tod             2.3354462     0.1741859    13.4077786      1.37e-32 ***
sd.seas           -1.4200913     0.2095869    -6.7756668       3.1e-10 ***
----------------------------------------------------------------------
Significance:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Log-Likelihood= -3888.413
AIC= 7800.827
BIC= 7847.493

For more examples of xlogit see this Jupyter Notebook in Google Colab. Google Colab provides GPU resources for free, which will significantly speed up your model estimation using xlogit.

Quick install

Install xlogit using pip as follows:

pip install xlogit

For additional installation details check xlogit installation instructions at: https://xlogit.readthedocs.io/en/latest/install.html

No GPU? No problem

xlogit can also be used without a GPU. However, if you need to speed up your model estimation, there are several low cost and even free options to access cloud GPU resources. For instance:

  • Google Colab offers free GPU resources with no setup required, as the service can be accessed using a web browser. Using xlogit in Google Colab is very easy as it runs out of the box without having to to install CUDA or CuPy, which are installed by default. For examples of xlogit running in Google Colab see this link.

  • Amazon Sagemaker Studio Lab offers Python runtime environments with free GPUs.

  • Google Cloud platform offers GPU processing at less than $1 USD per hour for NVIDIA Tesla K80 GPU with 4,992 CUDA cores.

  • Amazon Sagemaker offers virtual machine instances with the same TESLA K80 GPU at a similar price range of less than $1 USD per hour.

Benchmark

As shown in the plots below, xlogit is significantly faster than existing estimation packages. Also, xlogit provides convenient scaling when the number of random draws increases. These results were obtained using a modest and low-cost NVIDIA GTX 1060 graphics card. More sophisticated graphics cards are expected to provide even faster estimation times. For additional details about this benchmark and for replication instructions check https://xlogit.readthedocs.io/en/latest/benchmark.html.

https://raw.githubusercontent.com/arteagac/xlogit/master/examples/benchmark/results/time_benchmark_artificial.png https://raw.githubusercontent.com/arteagac/xlogit/master/examples/benchmark/results/time_benchmark_apollo_biogeme.png

Notes

The current version allows estimation of:

  • Mixed Logit with several types of mixing distributions (normal, lognormal, triangular, uniform, and truncated normal)

  • Mixed Logit with panel data

  • Mixed Logit with unbalanced panel data

  • Mixed Logit with Halton draws

  • Multinomial Logit models

  • Conditional logit models

  • Handling of unbalanced availability of choice alternatives for all of the supported models

  • Post-estimation tools for prediction and specification testing

  • Inclusion of sample weights for all of the supported models

Contributors

The following contributors have tremendously helped in the enhancement and expansion of xlogit’s features.

Contact

If you have any questions, ideas to improve xlogit, or want to report a bug, chat with us on gitter or open a new issue in xlogit’s GitHub repository.

Citing xlogit

Please cite xlogit as follows:

Arteaga, C., Park, J., Beeramoole, P. B., & Paz, A. (2022). xlogit: An open-source Python package for GPU-accelerated estimation of Mixed Logit models. Journal of Choice Modelling, 42, 100339. https://doi.org/10.1016/j.jocm.2021.100339

Or using BibTex as follows:

@article{xlogit,
    title = {xlogit: An open-source Python package for GPU-accelerated estimation of Mixed Logit models},
    author = {Cristian Arteaga and JeeWoong Park and Prithvi Bhat Beeramoole and Alexander Paz},
    journal = {Journal of Choice Modelling},
    volume = {42},
    pages = {100339},
    year = {2022},
    issn = {1755-5345},
    doi = {https://doi.org/10.1016/j.jocm.2021.100339},
}

Metadata

Release files for xlogit 0.2.7

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

Source distribution (sdist)

Source distribution for xlogit 0.2.7
File Size Uploaded
xlogit-0.2.7.tar.gz 39.4 kB Details

Built distribution (wheel)

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

Total release size: 75.5 kB

Release files / xlogit-0.2.7.tar.gz

Download URL xlogit-0.2.7.tar.gz
Size 39.4 kB
Tags Source
SHA-256 checksum
How to use checksums
60b3a657082cfe0b5b5a1ce1c49496da07ad14f51b2a4b6cda3e6089d5371608
BLAKE2b-256 checksum
How to use checksums
52cec2d15e5b5a98334b1d5093c24698a2a747d623a053c84d33d844d6b67491
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.12

Release files / xlogit-0.2.7-py3-none-any.whl

Download URL xlogit-0.2.7-py3-none-any.whl
Size 36.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7b091b5bb1e0b806892eccbc2b0f9b8b876bb80fcd994446faec80ac000fbc3f
BLAKE2b-256 checksum
How to use checksums
5bab5280d6920d59e739063effb59b54349a71ee66bdd11daa1664d08e68564a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.12

Release history Release notifications | RSS feed

This release

0.2.7 This release

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.1

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