22: MTC Motorized and Non-Motorized Nested Mode Choice#

import larch as lx

lx.__version__
'6.0.42'
m = lx.example(17)
m.compute_engine = "numba"

For this example, we’re going to re-create model 22 from the Self Instructing Manual. (pp. 179)

motorized = m.graph.new_node(
    parameter="mu_motor", children=[1, 2, 3, 4], name="Motorized"
)
nonmotorized = m.graph.new_node(
    parameter="mu_nonmotor", children=[5, 6], name="Nonmotorized"
)
m.ordering = (
    (
        "CostbyInc",
        "costbyincome",
    ),
    (
        "TravelTime",
        ".*time.*",
        ".*dist.*",
    ),
    (
        "Household",
        "hhinc.*",
        "vehbywrk.*",
    ),
    (
        "Zonal",
        "wkcbd.*",
        "wkempden.*",
    ),
    (
        "ASCs",
        "ASC.*",
    ),
)
m.graph
Tree cluster_elemental Elemental Alternatives 1 DA (1) 2 SR2 (2) 3 SR3+ (3) 4 Transit (4) 5 Bike (5) 6 Walk (6) 0 Root 7 Motorized (7) 0->7 8 Nonmotorized (8) 0->8 7->1 7->2 7->3 7->4 8->5 8->6
mj = m.copy()
mj.compute_engine = "jax"

The numba and jax compute engines give us the same result, but they have different performance characteristics:

numba

  • compile time is quite short and required only once per session,

  • run time is pretty good

jax

  • compile time is relatively long and required for every change in the model structure

  • run time is blazing fast

For small models with relatively small data sets and simple structures, the numba engine will typically be preferred. As data size or model complexity grows, the optimizations achievable in jax compilation become more valuable.

result = m.maximize_loglike(method="bhhh")

Iteration 013 [Optimization terminated successfully]

Best LL = -3441.672530467278

value best initvalue minimum maximum nullvalue holdfast
param_name
ASC_Bike -1.201 -1.201 0.0 -25.00 25.0 0.0 0
ASC_SR2 -1.325 -1.325 0.0 -25.00 25.0 0.0 0
ASC_SR3+ -2.506 -2.506 0.0 -25.00 25.0 0.0 0
ASC_Transit -0.404 -0.404 0.0 -25.00 25.0 0.0 0
ASC_Walk 0.345 0.345 0.0 -25.00 25.0 0.0 0
costbyincome -0.039 -0.039 0.0 -25.00 25.0 0.0 0
hhinc#4 -0.004 -0.004 0.0 -25.00 25.0 0.0 0
hhinc#5 -0.010 -0.010 0.0 -25.00 25.0 0.0 0
hhinc#6 -0.006 -0.006 0.0 -25.00 25.0 0.0 0
motorized_ovtbydist -0.114 -0.114 0.0 -25.00 25.0 0.0 0
motorized_time -0.015 -0.015 0.0 -25.00 25.0 0.0 0
mu_motor 0.726 0.726 1.0 0.01 1.0 1.0 0
mu_nonmotor 0.769 0.769 1.0 0.01 1.0 1.0 0
nonmotorized_time -0.046 -0.046 0.0 -25.00 25.0 0.0 0
vehbywrk_Bike -0.735 -0.735 0.0 -25.00 25.0 0.0 0
vehbywrk_SR -0.226 -0.226 0.0 -25.00 25.0 0.0 0
vehbywrk_Transit -0.707 -0.707 0.0 -25.00 25.0 0.0 0
vehbywrk_Walk -0.764 -0.764 0.0 -25.00 25.0 0.0 0
wkcbd_Bike 0.408 0.408 0.0 -25.00 25.0 0.0 0
wkcbd_SR2 0.193 0.193 0.0 -25.00 25.0 0.0 0
wkcbd_SR3+ 0.781 0.781 0.0 -25.00 25.0 0.0 0
wkcbd_Transit 0.921 0.921 0.0 -25.00 25.0 0.0 0
wkcbd_Walk 0.114 0.114 0.0 -25.00 25.0 0.0 0
wkempden_Bike 0.002 0.002 0.0 -25.00 25.0 0.0 0
wkempden_SR2 0.001 0.001 0.0 -25.00 25.0 0.0 0
wkempden_SR3+ 0.002 0.002 0.0 -25.00 25.0 0.0 0
wkempden_Transit 0.002 0.002 0.0 -25.00 25.0 0.0 0
wkempden_Walk 0.002 0.002 0.0 -25.00 25.0 0.0 0
/opt/hostedtoolcache/Python/3.10.18/x64/lib/python3.10/site-packages/larch/model/numbamodel.py:1423: RuntimeWarning: invalid value encountered in _numba_master
  *_numba_master_vectorized(
/opt/hostedtoolcache/Python/3.10.18/x64/lib/python3.10/site-packages/larch/model/numbamodel.py:1423: RuntimeWarning: invalid value encountered in _numba_master
  *_numba_master_vectorized(
/opt/hostedtoolcache/Python/3.10.18/x64/lib/python3.10/site-packages/larch/model/numbamodel.py:1423: RuntimeWarning: invalid value encountered in _numba_master
  *_numba_master_vectorized(
/opt/hostedtoolcache/Python/3.10.18/x64/lib/python3.10/site-packages/larch/model/numbamodel.py:1423: RuntimeWarning: invalid value encountered in _numba_master
  *_numba_master_vectorized(
/opt/hostedtoolcache/Python/3.10.18/x64/lib/python3.10/site-packages/larch/model/numbamodel.py:1423: RuntimeWarning: invalid value encountered in _numba_master
  *_numba_master_vectorized(
/opt/hostedtoolcache/Python/3.10.18/x64/lib/python3.10/site-packages/larch/model/numbamodel.py:1423: RuntimeWarning: invalid value encountered in _numba_master
  *_numba_master_vectorized(
/opt/hostedtoolcache/Python/3.10.18/x64/lib/python3.10/site-packages/larch/model/numbamodel.py:1423: RuntimeWarning: invalid value encountered in _numba_master
  *_numba_master_vectorized(
/opt/hostedtoolcache/Python/3.10.18/x64/lib/python3.10/site-packages/larch/model/numbamodel.py:1423: RuntimeWarning: invalid value encountered in _numba_master
  *_numba_master_vectorized(
/opt/hostedtoolcache/Python/3.10.18/x64/lib/python3.10/site-packages/larch/model/numbamodel.py:1423: RuntimeWarning: invalid value encountered in _numba_master
  *_numba_master_vectorized(
/opt/hostedtoolcache/Python/3.10.18/x64/lib/python3.10/site-packages/larch/model/numbamodel.py:1423: RuntimeWarning: invalid value encountered in _numba_master
  *_numba_master_vectorized(
/opt/hostedtoolcache/Python/3.10.18/x64/lib/python3.10/site-packages/larch/model/numbamodel.py:1423: RuntimeWarning: invalid value encountered in _numba_master
  *_numba_master_vectorized(
/opt/hostedtoolcache/Python/3.10.18/x64/lib/python3.10/site-packages/larch/model/numbamodel.py:1423: RuntimeWarning: invalid value encountered in _numba_master
  *_numba_master_vectorized(
/opt/hostedtoolcache/Python/3.10.18/x64/lib/python3.10/site-packages/larch/model/numbamodel.py:1423: RuntimeWarning: invalid value encountered in _numba_master
  *_numba_master_vectorized(
m.calculate_parameter_covariance()
m.parameter_summary()
/opt/hostedtoolcache/Python/3.10.18/x64/lib/python3.10/site-packages/larch/model/numbamodel.py:1423: RuntimeWarning: invalid value encountered in _numba_master
  *_numba_master_vectorized(
    Value Std Err t Stat Signif Null Value
Category Parameter          
CostbyInc costbyincome -0.0386  0.0104 -3.72 *** 0.00
TravelTime motorized_time -0.0145  0.00387 -3.76 *** 0.00
nonmotorized_time -0.0462  0.00540 -8.56 *** 0.00
motorized_ovtbydist -0.114  0.0211 -5.39 *** 0.00
Household hhinc#4 -0.00393  0.00161 -2.44 * 0.00
hhinc#5 -0.0100  0.00465 -2.16 * 0.00
hhinc#6 -0.00621  0.00302 -2.05 * 0.00
vehbywrk_Bike -0.735  0.229 -3.21 ** 0.00
vehbywrk_SR -0.226  0.0651 -3.47 *** 0.00
vehbywrk_Transit -0.707  0.150 -4.72 *** 0.00
vehbywrk_Walk -0.764  0.163 -4.68 *** 0.00
Zonal wkcbd_Bike  0.408  0.328  1.24 0.00
wkcbd_SR2  0.193  0.0962  2.01 * 0.00
wkcbd_SR3+  0.781  0.200  3.91 *** 0.00
wkcbd_Transit  0.921  0.222  4.15 *** 0.00
wkcbd_Walk  0.114  0.236  0.48 0.00
wkempden_Bike  0.00167  0.00109  1.54 0.00
wkempden_SR2  0.00115  0.000354  3.24 ** 0.00
wkempden_SR3+  0.00164  0.000449  3.65 *** 0.00
wkempden_Transit  0.00224  0.000507  4.41 *** 0.00
wkempden_Walk  0.00217  0.000762  2.85 ** 0.00
ASCs ASC_Bike -1.20  0.417 -2.88 ** 0.00
ASC_SR2 -1.33  0.255 -5.21 *** 0.00
ASC_SR3+ -2.51  0.475 -5.28 *** 0.00
ASC_Transit -0.404  0.221 -1.82 0.00
ASC_Walk  0.345  0.358  0.96 0.00
Other mu_motor  0.726  0.135 -2.03 * 1.00
mu_nonmotor  0.769  0.178 -1.29 1.00
resultj = mj.maximize_loglike(stderr=True)
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Larch Model Dashboard                                                                        ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ optimization complete                                                                        │
│ Log Likelihood Current =      -3441.672363 Best =      -3441.672363                          │
│ ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━┓ │
│ ┃ Parameter                       Estimate        Std. Error      t-Stat      Null Val ┃ │
│ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━┩ │
│ │ ASC_Bike                       │ -1.2015381     │  0.41682762    │ -2.883     │  0       │ │
│ │ ASC_SR2                        │ -1.3258556     │  0.25459284    │ -5.208     │  0       │ │
│ │ ASC_SR3+                       │ -2.5073382     │  0.4749814     │ -5.279     │  0       │ │
│ │ ASC_Transit                    │ -0.40413391    │  0.22135752    │ -1.826     │  0       │ │
│ │ ASC_Walk                       │  0.34519587    │  0.35783356    │  0.9647    │  0       │ │
│ │ costbyincome                   │ -0.03865569    │  0.010375324   │ -3.726     │  0       │ │
│ │ hhinc#4                        │ -0.0039278801  │  0.0016121723  │ -2.436     │  0       │ │
│ │ hhinc#5                        │ -0.010047257   │  0.0046502198  │ -2.161     │  0       │ │
│ │ hhinc#6                        │ -0.0062204647  │  0.003022644   │ -2.058     │  0       │ │
│ │ motorized_ovtbydist            │ -0.11385884    │  0.021103296   │ -5.395     │  0       │ │
│ │ motorized_time                 │ -0.014533574   │  0.0038675808  │ -3.758     │  0       │ │
│ │ mu_motor                       │  0.72627407    │  0.13492283    │ -2.029     │  1       │ │
│ │ mu_nonmotor                    │  0.76899446    │  0.17858686    │ -1.294     │  1       │ │
│ │ nonmotorized_time              │ -0.046207871   │  0.0053972616  │ -8.561     │  0       │ │
│ │ vehbywrk_Bike                  │ -0.7346048     │  0.22874789    │ -3.211     │  0       │ │
│ │ vehbywrk_SR                    │ -0.22590668    │  0.065107793   │ -3.47      │  0       │ │
│ │ vehbywrk_Transit               │ -0.70757457    │  0.14988515    │ -4.721     │  0       │ │
│ │ vehbywrk_Walk                  │ -0.76380217    │  0.16340096    │ -4.674     │  0       │ │
│ │ wkcbd_Bike                     │  0.40717442    │  0.32762614    │  1.243     │  0       │ │
│ │ wkcbd_SR2                      │  0.19333801    │  0.096259162   │  2.009     │  0       │ │
│ │ wkcbd_SR3+                     │  0.78183963    │  0.20000461    │  3.909     │  0       │ │
│ │ wkcbd_Transit                  │  0.92199207    │  0.22192422    │  4.155     │  0       │ │
│ │ wkcbd_Walk                     │  0.1138266     │  0.23647358    │  0.4814    │  0       │ │
│ │ wkempden_Bike                  │  0.0016787439  │  0.0010864197  │  1.545     │  0       │ │
│ │ wkempden_SR2                   │  0.0011495038  │  0.00035437627 │  3.244     │  0       │ │
│ │ wkempden_SR3+                  │  0.001638102   │  0.00044877059 │  3.65      │  0       │ │
│ │ wkempden_Transit               │  0.0022378609  │  0.00050735514 │  4.411     │  0       │ │
│ │ wkempden_Walk                  │  0.0021704948  │  0.00076248142 │  2.847     │  0       │ │
│ └────────────────────────────────┴────────────────┴────────────────┴────────────┴──────────┘ │
└──────────────────────────────────────────────────────────────────────────────────────────────┘