mnlite

Discrete choice modelling for travel mode choice in numpy: multinomial and nested logit by maximum likelihood, robust standard errors, elasticities, value of travel time and scenario simulation.

Version 0.1.2 · Python >=3.9 · MIT licence
pip install mnlite
PyPISourceIssuesChangelogCite (DOI)

mnlite

PyPI version Python versions pipeline status License: MIT DOI Downloads

Lightweight discrete choice modelling for travel mode choice, in numpy.

mnlite estimates multinomial logit (MNL) and one-level nested logit (NL) models by maximum likelihood and gives you what a mode choice study needs afterwards: standard errors (classical and robust), rho-squared, likelihood ratio tests, elasticities, the value of travel time with a delta-method standard error, and market-share scenarios. It depends on numpy only; scipy and pandas are optional.

It is meant for teaching, quick studies and pipelines where a full econometrics package is too heavy. For mixed logit, latent classes, panel data or large production models use a fuller package: Biogeme, PyLogit, xlogit (GPU mixed logit) or Apollo in R.

pip install mnlite              # numpy only
pip install "mnlite[pandas]"    # + DataFrame export
pip install "mnlite[scipy]"     # + scipy optimiser backend

Requires Python 3.9 or newer.

Quickstart

import mnlite
from mnlite import Spec, estimate

# Ben-Akiva & Lerman (1985, p. 88): 21 travellers, car vs transit.
data = mnlite.ben_akiva_lerman()
res = estimate({"car": "b_time*time", "transit": "asc_transit + b_time*time"}, data)
print(res.summary())
# asc_transit 0.2376 (s.e. 0.7505), b_time -0.0531 (s.e. 0.0206),
# LL -6.166, rho2 0.576, adjusted rho2 0.439  -> identical to the published values

# Three modes, availability, generic and alternative-specific coefficients.
modes = mnlite.synthetic_mode_choice(n=3000)          # known true parameters
spec = Spec({
    "car":  "b_time*time + b_cost*cost + b_inc_car*income",
    "bus":  "asc_bus  + b_time*time + b_cost*cost",
    "rail": "asc_rail + b_time*time + b_cost*cost",
})
mnl = estimate(spec, modes)
print(mnl.vtt("b_time", "b_cost", scale=60))           # currency per hour, delta-method s.e.
print(mnl.elasticities(modes, "cost", "car"))          # direct (car) and cross elasticities
print(mnl.simulate(modes, {"cost_bus": "*1.2", "time_rail": "-5"}).table())

# Nested logit: bus and rail in a public transport nest (FIML).
nl = estimate(Spec(spec.utilities, nests={"pt": ["bus", "rail"]}), modes)
print(mnlite.lr_test(mnl, nl))
print(mnlite.compare([mnl, nl], ["MNL", "NL"]))

See examples/quickstart.py for a complete script.

Data

ChoiceData holds one row per choice observation (wide form). Alternative-specific attributes are columns named {var}_{alt} (time_car, time_bus); other columns (income) are individual-specific. The utility term b_time*time for alternative bus uses column time_bus if it exists, otherwise time.

d = mnlite.read_csv("trips.csv", alternatives=["car", "bus", "rail"],
                    choice="mode", avail="{alt}_av", weights="w")           # wide
d = mnlite.read_csv("trips_long.csv", form="long", id_col="id", alt_col="alt",
                    choice_col="chosen", avail_col="av")                    # long
d = mnlite.ChoiceData.from_wide(df, ["car", "bus", "rail"])                 # pandas
d.to_long(); d.to_wide(); d.write_csv("out.csv", "long"); d.to_pandas()

In long form, missing rows mean "unavailable"; a variable that is constant across an observation's alternatives is also kept as an individual-specific column.

Specification mini-language

Written Meaning
asc_bus alternative-specific constant
b_time*time parameter times variable; the first name is always the parameter
b_inc_rail*income individual-specific variable (needs an alternative-specific parameter)
b*time*income interaction (product of variables)
b_cost*cost*0.01 numeric scaling
- b*x negative sign
0 or "" utility fixed at zero (reference alternative)

A parameter name used in several utilities is generic; used once it is alternative-specific. Spec(..., fixed={"asc_rail": 0}) holds a parameter fixed; nests={"pt": ["bus", "rail"]} adds the nest parameter lambda_pt. Spec.to_dict() / Spec.from_dict() round-trip through JSON.

API overview

Object Purpose
Spec(utilities, nests=None, fixed=None, column_pattern="{var}_{alt}") model specification
ChoiceData(columns, alternatives, choice, avail=None, weights=None) data; .from_wide, .from_long, .to_long, .to_wide, .write_csv, .to_pandas
read_csv(path, form="wide" or "long", **kw) CSV reader (stdlib csv)
estimate(spec, data, start=None, method="auto") MLE; returns EstimationResults
res.params, .std_errors(robust=), .tstats(), .pvalues(), .conf_int() inference
res.loglik, .loglik_null, .loglik_constants, .rho2, .rho2_bar, .aic, .bic fit
res.lr_test_null(), lr_test(restricted, unrestricted), res.wald_test(name, value) tests
res.ratio(num, den, scale), res.vtt(time, cost, scale=60) ratios, VTT with delta-method s.e.
res.elasticities(data, var, alt, aggregate=True) direct / cross point elasticities
res.predict(data), .market_shares(data), .simulate(data, changes, avail) prediction and scenarios
res.summary(), .to_dict(), .to_json(), compare([...]) reporting
ben_akiva_lerman(), synthetic_mode_choice(n, seed, nested) validation data

Command line

mnlite example                                    # reproduces Ben-Akiva & Lerman (1985)
mnlite synth trips.csv --n 2000 [--nested] [--long]
mnlite estimate trips.csv --avail "{alt}_av" \
    -u "car=b_time*time + b_cost*cost + b_inc_car*income" \
    -u "bus=asc_bus + b_time*time + b_cost*cost" \
    -u "rail=asc_rail + b_time*time + b_cost*cost" \
    --nest pt:bus,rail --ratio "b_time/b_cost*60" \
    --elasticity cost:car --scenario "cost_bus*1.2" --json results.json
mnlite estimate trips.csv --spec spec.json        # {"utilities": {...}, "nests": {...}, "fixed": {...}}
mnlite convert trips_long.csv trips_wide.csv --long --avail avail --to wide

Scenario changes: COL*1.2, COL+5, COL-5, COL/2, COL=0.

Method and sources

Validation

References

Limitations

Academic citation

@software{eminoglu_mnlite,
  author  = {Eminoğlu, Yusuf},
  title   = {mnlite},
  year    = {2026},
  version = {0.1.2},
  doi     = {10.5281/zenodo.24235586},
  url     = {https://gitlab.com/geophilo1/mnlite_sdk}
}

Related Geophilo SDKs

License

MIT License, Copyright (c) 2026 Yusuf Eminoğlu. See LICENSE.

Other Geophilo SDKs