How driftlesslabs/larch works
Purpose and Package Entry
Larch is a Python library for estimating and applying discrete-choice models. The public package begins at src/larch/__init__.py; model-facing APIs are organized under src/larch/model/__init__.py, and dataset APIs under src/larch/dataset/__init__.py. It is primarily a programmatic modeling library, not a service or web application.
Data Representation and Preparation
Choice data is represented through dataset construction, dimension naming, patching, and flow transformations. This layer converts tabular/array inputs into the structures consumed by models and provides choice/availability reporting.
• src/larch/dataset/construct.py
• src/larch/dataset/flow.py
• src/larch/dataset/choice_avail_reporting.py
Bundled warehouse files support examples and reproducible model workflows, including CSV/Parquet/OMX datasets in src/larch/data_warehouse/.
Model Definition and Parameter Core
The core model abstraction manages parameters, utility expressions, constraints, persistence flags, and diagnostics. Model specifications can be built directly or derived from tabular specifications; linear functions provide the principal utility-expression representation.
• src/larch/model/basemodel.py
• src/larch/model/param_core.py
• src/larch/model/from_spec.py
Constraints are a distinct boundary applied during estimation rather than being embedded in input data.
• src/larch/model/constraints.py
• src/larch/model/constrainedmodel.py
Choice-Model Variants and Structure
Specialized model classes extend the common model core for nested/cascading choice structures, latent classes, mixtures, random terms, and coordinated groups of models.
• src/larch/model/cascading.py
• src/larch/model/tree.py
• src/larch/model/latent_class.py
Related composition features are in src/larch/model/mixtures.py and src/larch/model/model_group.py. This separates behavioral/model structure from the compute backend.
Swappable Compute Engines
Version 6 supports equivalent model execution through Numba or JAX. Backend-specific model implementations and optimization/streaming paths sit behind the shared model interfaces.
• src/larch/model/numbamodel.py
• src/larch/model/jaxmodel.py
• src/larch/model/numba_optimization.py
JAX-specific execution is therefore an optional material runtime path; Numba is the alternate compiled path. src/larch/compiled.py is a package-level compiled-execution integration point.
Estimation, Optimization, and Outputs
Estimation flows from prepared data plus a configured model into likelihood/gradient evaluation, optimization, diagnostics, and saved model state. Generic optimization is centralized separately from backend implementations.
• src/larch/model/optimization.py
• src/larch/optimize.py
• src/larch/model/saving.py
Prediction analysis and model troubleshooting are supported around this boundary rather than constituting an external serving layer.
• src/larch/model/troubleshooting.py
• src/larch/model/gradient_check.py
External Formats, Documentation, and Delivery
The library reads OMX data through src/larch/omx.py; examples demonstrate workflows using supplied data and notebooks under docs/examples/. Packaging is Python/uv-based (pyproject.toml, uv.lock), with Docker support in Dockerfile. CI, docs publication, and releases are automated via .github/workflows/ci.yml, .github/workflows/docs.yml, and .github/workflows/release.yml.
Main entry points: basemodel.py Base model · construct.py Dataset construction · __init__.py Larch package · optimization.py Optimization
Components 22
Data Layer
- Dataset API
src/larch/dataset/__init__.py - Dataset construction · Constructs and transforms choice datasets.
src/larch/dataset/construct.py - Data flow transforms
src/larch/dataset/flow.py - Choice & availability reports
src/larch/dataset/choice_avail_reporting.py - Bundled data warehouse · CSV, Parquet, and OMX workflow data.
Modeling Core
- Model API
src/larch/model/__init__.py - Base model · Owns model state, utilities, diagnostics, and persistence flags.
src/larch/model/basemodel.py - Parameter core
src/larch/model/param_core.py - Specification builder · Builds models from tabular specifications.
src/larch/model/from_spec.py - Constraints
src/larch/model/constraints.py - Structured choice models · Cascading, tree, and latent-class structures extend the core.
src/larch/model/cascading.py
Execution & Estimation
- Numba backend
src/larch/model/numbamodel.py - JAX backend
src/larch/model/jaxmodel.py - Compiled integration
src/larch/compiled.py - Optimization · Coordinates likelihood and gradient optimization.
src/larch/model/optimization.py - Numba optimization
src/larch/model/numba_optimization.py - Model saving
src/larch/model/saving.py
Formats & Delivery
- OMX reader
src/larch/omx.py - Examples & notebooks
- CI pipeline
.github/workflows/ci.yml - Release automation
.github/workflows/release.yml
Other components
- Larch package · Public programmatic API.
src/larch/__init__.py
Connections 23
- Larch package → Dataset API: exports
- Larch package → Model API: exports
- Dataset API → Dataset construction: constructs
- Dataset construction → Data flow transforms: transforms
- Dataset construction → Choice & availability reports: reports
- Bundled data warehouse → Dataset construction: supplies data
- OMX reader → Dataset construction: imports matrices
- Model API → Base model: exposes
- Specification builder → Base model: builds
- Parameter core → Base model: configures
- Structured choice models → Base model: extends
- Dataset construction → Base model: provides choice data
- Constraints → Optimization: applied during estimation
- Base model → Numba backend: shared interface
- Base model → JAX backend: shared interface
- Compiled integration → Numba backend: integrates
- Base model → Optimization: evaluates likelihood
- Numba backend → Numba optimization: executes
- Numba optimization → Optimization: backend path
- Optimization → Model saving: saves fitted state
- Bundled data warehouse → Examples & notebooks: supports workflows
- CI pipeline → Larch package: validates
- Release automation → Larch package: publishes