# driftlesslabs/larch architecture

> Architecture diagram of the GitHub repository driftlesslabs/larch, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/driftlesslabs/larch
- Repository: https://github.com/driftlesslabs/larch

## Overview

#### 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: [`src/larch/model/basemodel.py`](https://github.com/driftlesslabs/larch/blob/main/src/larch/model/basemodel.py) (Base model), [`src/larch/dataset/construct.py`](https://github.com/driftlesslabs/larch/blob/main/src/larch/dataset/construct.py) (Dataset construction), [`src/larch/__init__.py`](https://github.com/driftlesslabs/larch/blob/main/src/larch/__init__.py) (Larch package), [`src/larch/model/optimization.py`](https://github.com/driftlesslabs/larch/blob/main/src/larch/model/optimization.py) (Optimization)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data Layer"]
  node_dataset_api["Dataset API<br/>data API<br/>[__init__.py]"]
  node_data_construction["Dataset construction<br/>data preparation<br/>[construct.py]"]
  node_data_flow["Data flow transforms<br/>data transformation<br/>[flow.py]"]
  node_choice_reporting["Choice &amp; availability reports<br/>data diagnostics"]
  node_warehouse[("Bundled data warehouse<br/>example datasets")]
end

subgraph group_model["Modeling Core"]
  node_model_api["Model API<br/>[__init__.py]"]
  node_model_core["Base model<br/>model abstraction<br/>[basemodel.py]"]
  node_parameters["Parameter core<br/>parameter management<br/>[param_core.py]"]
  node_spec_builder["Specification builder<br/>model specification<br/>[from_spec.py]"]
  node_constraints["Constraints<br/>[constraints.py]"]
  node_structured_models["Structured choice models<br/>model variants<br/>[cascading.py]"]
end

subgraph group_runtime["Execution &amp; Estimation"]
  node_numba_backend{{"Numba backend<br/>compiled model runtime<br/>[numbamodel.py]"}}
  node_jax_backend{{"JAX backend<br/>optional model runtime<br/>[jaxmodel.py]"}}
  node_compiled_integration["Compiled integration<br/>runtime integration<br/>[compiled.py]"]
  node_optimization["Optimization<br/>estimation orchestration<br/>[optimization.py]"]
  node_numba_optimization["Numba optimization<br/>backend optimization"]
  node_model_saving["Model saving<br/>model persistence<br/>[saving.py]"]
end

subgraph group_delivery["Formats &amp; Delivery"]
  node_omx["OMX reader<br/>external format adapter<br/>[omx.py]"]
  node_examples["Examples &amp; notebooks<br/>documentation workflows"]
  node_ci["CI pipeline<br/>automation<br/>[ci.yml]"]
  node_release["Release automation<br/>[release.yml]"]
end

node_package["Larch package<br/>Python package entry<br/>[__init__.py]"]

node_package -->|"exports"| node_dataset_api
node_package -->|"exports"| node_model_api
node_dataset_api -->|"constructs"| node_data_construction
node_data_construction -->|"transforms"| node_data_flow
node_data_construction -->|"reports"| node_choice_reporting
node_warehouse -->|"supplies data"| node_data_construction
node_omx -->|"imports matrices"| node_data_construction
node_model_api -->|"exposes"| node_model_core
node_spec_builder -->|"builds"| node_model_core
node_parameters -->|"configures"| node_model_core
node_structured_models -->|"extends"| node_model_core
node_data_construction -->|"provides choice data"| node_model_core
node_constraints -->|"applied during estimation"| node_optimization
node_model_core -->|"shared interface"| node_numba_backend
node_model_core -->|"shared interface"| node_jax_backend
node_compiled_integration -->|"integrates"| node_numba_backend
node_model_core -->|"evaluates likelihood"| node_optimization
node_numba_backend -->|"executes"| node_numba_optimization
node_numba_optimization -->|"backend path"| node_optimization
node_optimization -->|"saves fitted state"| node_model_saving
node_warehouse -->|"supports workflows"| node_examples
node_ci -.->|"validates"| node_package
node_release -.->|"publishes"| node_package

click node_package "https://github.com/driftlesslabs/larch/blob/main/src/larch/__init__.py"
click node_dataset_api "https://github.com/driftlesslabs/larch/blob/main/src/larch/dataset/__init__.py"
click node_data_construction "https://github.com/driftlesslabs/larch/blob/main/src/larch/dataset/construct.py"
click node_data_flow "https://github.com/driftlesslabs/larch/blob/main/src/larch/dataset/flow.py"
click node_choice_reporting "https://github.com/driftlesslabs/larch/blob/main/src/larch/dataset/choice_avail_reporting.py"
click node_model_api "https://github.com/driftlesslabs/larch/blob/main/src/larch/model/__init__.py"
click node_model_core "https://github.com/driftlesslabs/larch/blob/main/src/larch/model/basemodel.py"
click node_parameters "https://github.com/driftlesslabs/larch/blob/main/src/larch/model/param_core.py"
click node_spec_builder "https://github.com/driftlesslabs/larch/blob/main/src/larch/model/from_spec.py"
click node_constraints "https://github.com/driftlesslabs/larch/blob/main/src/larch/model/constraints.py"
click node_structured_models "https://github.com/driftlesslabs/larch/blob/main/src/larch/model/cascading.py"
click node_numba_backend "https://github.com/driftlesslabs/larch/blob/main/src/larch/model/numbamodel.py"
click node_jax_backend "https://github.com/driftlesslabs/larch/blob/main/src/larch/model/jaxmodel.py"
click node_compiled_integration "https://github.com/driftlesslabs/larch/blob/main/src/larch/compiled.py"
click node_optimization "https://github.com/driftlesslabs/larch/blob/main/src/larch/model/optimization.py"
click node_numba_optimization "https://github.com/driftlesslabs/larch/blob/main/src/larch/model/numba_optimization.py"
click node_model_saving "https://github.com/driftlesslabs/larch/blob/main/src/larch/model/saving.py"
click node_omx "https://github.com/driftlesslabs/larch/blob/main/src/larch/omx.py"
click node_ci "https://github.com/driftlesslabs/larch/blob/main/.github/workflows/ci.yml"
click node_release "https://github.com/driftlesslabs/larch/blob/main/.github/workflows/release.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_dataset_api,node_data_construction,node_data_flow,node_choice_reporting,node_warehouse toneBlue
class node_model_api,node_model_core,node_parameters,node_spec_builder,node_constraints,node_structured_models toneAmber
class node_numba_backend,node_jax_backend,node_compiled_integration,node_optimization,node_numba_optimization,node_model_saving toneMint
class node_omx,node_examples,node_ci,node_release toneRose
class node_package toneNeutral
```

## Components

### Data Layer

- [Dataset API](https://github.com/driftlesslabs/larch/blob/main/src/larch/dataset/__init__.py): data API · `src/larch/dataset/__init__.py`
- [Dataset construction](https://github.com/driftlesslabs/larch/blob/main/src/larch/dataset/construct.py): data preparation · Constructs and transforms choice datasets. · `src/larch/dataset/construct.py`
- [Data flow transforms](https://github.com/driftlesslabs/larch/blob/main/src/larch/dataset/flow.py): data transformation · `src/larch/dataset/flow.py`
- [Choice & availability reports](https://github.com/driftlesslabs/larch/blob/main/src/larch/dataset/choice_avail_reporting.py): data diagnostics · `src/larch/dataset/choice_avail_reporting.py`
- **Bundled data warehouse**: example datasets · CSV, Parquet, and OMX workflow data.

### Modeling Core

- [Model API](https://github.com/driftlesslabs/larch/blob/main/src/larch/model/__init__.py): model API · `src/larch/model/__init__.py`
- [Base model](https://github.com/driftlesslabs/larch/blob/main/src/larch/model/basemodel.py): model abstraction · Owns model state, utilities, diagnostics, and persistence flags. · `src/larch/model/basemodel.py`
- [Parameter core](https://github.com/driftlesslabs/larch/blob/main/src/larch/model/param_core.py): parameter management · `src/larch/model/param_core.py`
- [Specification builder](https://github.com/driftlesslabs/larch/blob/main/src/larch/model/from_spec.py): model specification · Builds models from tabular specifications. · `src/larch/model/from_spec.py`
- [Constraints](https://github.com/driftlesslabs/larch/blob/main/src/larch/model/constraints.py): estimation constraints · `src/larch/model/constraints.py`
- [Structured choice models](https://github.com/driftlesslabs/larch/blob/main/src/larch/model/cascading.py): model variants · Cascading, tree, and latent-class structures extend the core. · `src/larch/model/cascading.py`

### Execution & Estimation

- [Numba backend](https://github.com/driftlesslabs/larch/blob/main/src/larch/model/numbamodel.py): compiled model runtime · `src/larch/model/numbamodel.py`
- [JAX backend](https://github.com/driftlesslabs/larch/blob/main/src/larch/model/jaxmodel.py): optional model runtime · `src/larch/model/jaxmodel.py`
- [Compiled integration](https://github.com/driftlesslabs/larch/blob/main/src/larch/compiled.py): runtime integration · `src/larch/compiled.py`
- [Optimization](https://github.com/driftlesslabs/larch/blob/main/src/larch/model/optimization.py): estimation orchestration · Coordinates likelihood and gradient optimization. · `src/larch/model/optimization.py`
- [Numba optimization](https://github.com/driftlesslabs/larch/blob/main/src/larch/model/numba_optimization.py): backend optimization · `src/larch/model/numba_optimization.py`
- [Model saving](https://github.com/driftlesslabs/larch/blob/main/src/larch/model/saving.py): model persistence · `src/larch/model/saving.py`

### Formats & Delivery

- [OMX reader](https://github.com/driftlesslabs/larch/blob/main/src/larch/omx.py): external format adapter · `src/larch/omx.py`
- **Examples & notebooks**: documentation workflows
- [CI pipeline](https://github.com/driftlesslabs/larch/blob/main/.github/workflows/ci.yml): automation · `.github/workflows/ci.yml`
- [Release automation](https://github.com/driftlesslabs/larch/blob/main/.github/workflows/release.yml): automation · `.github/workflows/release.yml`

### Other components

- [Larch package](https://github.com/driftlesslabs/larch/blob/main/src/larch/__init__.py): Python package entry · Public programmatic API. · `src/larch/__init__.py`

## Connections

- 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
