Extensions for a Python package for GPU-accelerated estimation of mixed logit models.
Project description
Quick start
The following example uses the base searchlogit 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)
transvars: List of variables to apply the specified transformation
transformation: String specifying the transformation to be applied (default is the Box-Cox transformation)
correlation: Either a list of variables names that are correlated or a boolean to set all randvars as correlated
The current version of searchlogit only supports input data in long format.
# Read data from CSV file
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/arteagac/xlogit/master/examples/data/electricity_long.csv")
# reverse sign for randvars with a lognormal distribution
df['tod'] = -df['tod']
df['seas'] = -df['seas']
X = df[varnames]
y = df['choice']
# Fit the model with xlogit
from searchlogit import MixedLogit
model = MixedLogit()
model.fit(X, y,
varnames,
alts=df['alt'],
ids=df['chid'],
panels=df['id'],
randvars={'cl':'n','loc':'n','wk':'u','tod':'ln','seas':'n'},
correlation=['loc', 'wk', 'tod', 'seas'],
transvars=['cl'],
n_draws=600
)
model.summary()
Optimization terminated successfully. Current function value: 3942.930393 Iterations: 66 Function evaluations: 77 Gradient evaluations: 77 Estimation time= 51.5 seconds Frequencies of alternatives: observed choice [0.22576177 0.26246537 0.23684211 0.26939058] Frequencies of alternatives: predicted choice [0.22930773 0.26222102 0.22989339 0.2730377 ] --------------------------------------------------------------------------- Coefficient Estimate Std.Err. z-val P>|z| --------------------------------------------------------------------------- pf -0.8844627763 0.0295175526 -29.9639603432 0 *** loc 2.2458727521 0.1032222566 21.7576405168 0 *** wk 0.2276055259 0.0685607922 3.3197621949 0.000901 *** tod 2.1355682749 0.0376005793 56.7961535512 0 *** seas 8.7966341596 0.2824599096 31.1429475855 0 *** chol.loc.loc 1.3698552163 0.1301206261 10.5275793490 0 *** chol.wk.loc 0.5137795414 0.0664560103 7.7311222681 1.07e-14 *** chol.wk.wk -0.5269078200 0.0465699165 -11.3143389438 0 *** chol.tod.loc -0.0092950782 0.0277084890 -0.3354595849 0.737 chol.tod.wk 0.0694307288 0.0316747405 2.1919904546 0.0284 * chol.tod.tod 0.3998943075 0.0283441100 14.1085504981 0 *** chol.seas.loc 0.2636579056 0.1466998262 1.7972611991 0.0723 . chol.seas.wk -0.0387176619 0.1862974259 -0.2078271435 0.835 chol.seas.tod 1.0931383738 0.1523794076 7.1737933030 7.29e-13 *** chol.seas.seas -2.9951794692 0.1818545992 -16.4701881742 0 *** cl -0.1571373624 0.0234064621 -6.7134179193 1.9e-11 *** sd.cl 0.2767984154 0.0345402217 8.0137996096 1.11e-15 *** lambda.cl 1.1525365710 0.1287181869 1.1850428803 0.236 --------------------------------------------------------------------------- Significance: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Log-Likelihood= -3942.930 AIC= 7921.861 BIC= 7991.861
Quick install
Install searchlogit using pip as follows:
pip install searchlogit
No GPU? No problem
searchlogit 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 for learning purposes with no setup required, as the service can be accessed using a web browser. Using searchlogit in Google Colab is very easy as it runs out of the box without needing to install CUDA or CuPy, which are installed by default.
For examples of xlogit running in Google Colab see this link.
The Google Cloud platform offers GPU processing starting at $0.45 USD per hour for a NVIDIA Tesla K80 GPU with 4,992 CUDA cores.
Amazon Sagemaker offers virtual machine instances with the same TESLA K80 GPU at less than $1 USD per hour.
Benchmark
searchlogit extends xlogit. 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.
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
Mixed Logit with correlated random variables
Mixed Logit with Box-Cox transformed fixed and random variables
Multinomial Logit models with Box-Cox transformed fixed and random variables
Conditional logit models
Latent class logit models
Latent class mixed logit models
Weighed regression for all of the logit-based 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
Contact
If you have any questions, ideas to improve searchlogit, or want to report a bug, just open a new issue in xlogit’s GitHub repository .
Citing searchlogit
Please cite searchlogit as follows:
Beeramoole, P. B., Alexander, P., Kelly, R., Arteaga, C.,. (2022). Searchlogit [Computer software]. https://pypi.org/project/searchlogit/
Or using BibTex as follows:
@misc{searchlogit, author = {Beeramoole, P and Paz, A and Kelly, R and Arteaga, C}, title = "{Searchlogit [Computer software]}, url = {https://pypi.org/project/searchlogit/}, year = {2022} }
Project details
Release history Release notifications | RSS feed
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
Hashes for searchlogit-0.2.178-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 301abf83e14230d96c6f103b9079f24c6686bc669c3ed0539ae3bcba969d12f1 |
|
MD5 | 373ac59b2b12975b3bbcee93d4d276a3 |
|
BLAKE2b-256 | 74932636a14eedada92dd45c433204786b18747cee7e9f3e1b486e37f59c1533 |