17B: Alternative Segmentation#

Model 17B segments the market by automobile ownership for households that have more than one car. (pp. 133)5)

import larch
larch.__version__
'6.0.37.dev32+gdab7641'

This example is a mode choice model built using the MTC example dataset. First we create the DB and Model objects:

d = larch.examples.MTC(format="dataset")
d
<xarray.Dataset> Size: 2MB
Dimensions:    (caseid: 5029, altid: 6)
Coordinates:
  * caseid     (caseid) int64 40kB 1 2 3 4 5 6 ... 5024 5025 5026 5027 5028 5029
  * altid      (altid) int64 48B 1 2 3 4 5 6
    alt_names  (altid) <U7 168B 'DA' 'SR2' 'SR3+' 'Transit' 'Bike' 'Walk'
Data variables: (12/38)
    chose      (caseid, altid) float32 121kB 1.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
    ivtt       (caseid, altid) float64 241kB 13.38 18.38 20.38 ... 1.59 6.55 0.0
    ovtt       (caseid, altid) float64 241kB 2.0 2.0 2.0 15.2 ... 16.0 4.5 0.0
    tottime    (caseid, altid) float64 241kB 15.38 20.38 22.38 ... 11.05 19.1
    totcost    (caseid, altid) float64 241kB 70.63 35.32 20.18 ... 75.0 0.0 0.0
    hhid       (caseid) int64 40kB 2 3 5 6 8 8 ... 9429 9430 9433 9434 9436 9438
    ...         ...
    corredis   (caseid) int64 40kB 0 1 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
    vehbywrk   (caseid) float64 40kB 4.0 1.0 0.33 1.0 0.0 ... 2.0 2.0 3.0 3.0
    vocc       (caseid) int64 40kB 1 0 1 0 2 0 1 1 1 1 0 ... 1 2 1 1 0 1 2 1 1 1
    wgt        (caseid) int64 40kB 1 1 1 1 1 1 1 1 1 1 1 ... 1 1 1 1 1 1 1 1 1 1
    _avail_    (caseid, altid) int8 30kB 1 1 1 1 1 0 1 1 1 ... 1 0 1 1 1 1 1 1 1
    avail      (caseid, altid) int8 30kB 1 1 1 1 1 0 1 1 1 ... 1 0 1 1 1 1 1 1 1
Attributes:
    _caseid_:  caseid
    _altid_:   altid

Include only the cases where number of vehicles is 2 or more

d = d.sel(caseid=d.numveh >= 2)
m = larch.Model(d, compute_engine="numba")

Then we can build up the utility function. We’ll use some :ref:idco data first, using the Model.utility.co attribute. This attribute is a dict-like object, to which we can assign :class:LinearFunction objects for each alternative code.

from larch import P, X

for a in [4, 5, 6]:
    m.utility_co[a] += X("hhinc") * P(f"hhinc#{a}")

Since the model we want to create groups together DA, SR2 and SR3+ jointly as reference alternatives with respect to income, we can simply omit all of these alternatives from the block that applies to hhinc.

For vehicles per worker, the preferred model include a joint parameter on SR2 and SR3+, but not including DA and not fixed at zero. Here we might use a shadow_parameter (also called an alias in some places), which allows us to specify one or more parameters that are simply a fixed proportion of another parameter. For example, we can say that vehbywrk_SR2 will be equal to vehbywrk_SR.

for i in d["alt_names"][1:3]:
    name = str(i.values)
    a = int(i.altid)
    m.utility_co[a] += (
        +X("vehbywrk") * P("vehbywrk_SR")
        + X("wkccbd+wknccbd") * P("wkcbd_" + name)
        + X("wkempden") * P("wkempden_" + name)
        + P("ASC_" + name)
    )

for i in d["alt_names"][3:]:
    name = str(i.values)
    a = int(i.altid)
    m.utility_co[a] += (
        +X("vehbywrk") * P("vehbywrk_" + name)
        + X("wkccbd+wknccbd") * P("wkcbd_" + name)
        + X("wkempden") * P("wkempden_" + name)
        + P("ASC_" + name)
    )

Next we’ll use some idca data, with the utility_ca attribute. This attribute is only a single :class:LinearFunction that is applied across all alternatives using :ref:idca data. Because the data is structured to vary across alternatives, the parameters (and thus the structure of the :class:LinearFunction) does not need to vary across alternatives.

m.utility_ca = (
    +X("totcost/hhinc") * P("costbyincome")
    + X("tottime * (altid <= 4)") * P("motorized_time")
    + X("tottime * (altid >= 5)") * P("nonmotorized_time")
    + X("ovtt/dist * (altid <= 4)") * P("motorized_ovtbydist")
)

The “totcost/hhinc” data is computed once as a new variable when loading the model data. The same applies for tottime filtered by motorized modes (we harness the convenient fact that all the motorized modes have identifying numbers 4 or less), and “ovtt/dist”.

Lastly, we need to identify idca Format data that gives the availability for each alternative, as well as the number of times each alternative is chosen. (In traditional discrete choice analysis, this is often 0 or 1, but it need not be binary, or even integral.)

m.availability_ca_var = "avail"
m.choice_ca_var = "chose"

And let’s give our model a descriptive title.

m.title = "MTC Example 17B, Segmented for 2 or more cars"

We can view a summary of the choices and alternative availabilities to make sure the model is set up correctly.

m.choice_avail_summary()
name chosen available
1 DA 3022 3757
2 SR2 365 3808
3 SR3+ 117 3808
4 Transit 221 2941
5 Bike 24 1393
6 Walk 59 987
< Total All Alternatives > 3808 <NA>

We’ll set a parameter cap (bound) at +/- 25, which helps improve the numerical stability of the optimization algorithm used in estimation.

m.set_cap(25)

Having created this model, we can then estimate it:

assert m.compute_engine == "numba"
result = m.maximize_loglike(stderr=True, options={"maxiter": 1000, "ftol": 1e-10})
m.calculate_parameter_covariance()
m.loglike()

Iteration 097 [Optimization terminated successfully]

Best LL = -2296.7365749439946

value best initvalue minimum maximum nullvalue holdfast
param_name
ASC_Bike -3.131354 -3.131354 0.0 -25.0 25.0 0.0 0
ASC_SR2 -1.982918 -1.982918 0.0 -25.0 25.0 0.0 0
ASC_SR3+ -3.726011 -3.726011 0.0 -25.0 25.0 0.0 0
ASC_Transit -2.156692 -2.156692 0.0 -25.0 25.0 0.0 0
ASC_Walk -1.583599 -1.583599 0.0 -25.0 25.0 0.0 0
costbyincome -0.097861 -0.097861 0.0 -25.0 25.0 0.0 0
hhinc#4 0.000361 0.000361 0.0 -25.0 25.0 0.0 0
hhinc#5 -0.002651 -0.002651 0.0 -25.0 25.0 0.0 0
hhinc#6 0.000910 0.000910 0.0 -25.0 25.0 0.0 0
motorized_ovtbydist -0.193760 -0.193760 0.0 -25.0 25.0 0.0 0
motorized_time -0.018755 -0.018755 0.0 -25.0 25.0 0.0 0
nonmotorized_time -0.045210 -0.045210 0.0 -25.0 25.0 0.0 0
vehbywrk_Bike -0.217195 -0.217194 0.0 -25.0 25.0 0.0 0
vehbywrk_SR -0.238238 -0.238238 0.0 -25.0 25.0 0.0 0
vehbywrk_Transit -0.236197 -0.236197 0.0 -25.0 25.0 0.0 0
vehbywrk_Walk -0.079981 -0.079982 0.0 -25.0 25.0 0.0 0
wkcbd_Bike 0.565196 0.565196 0.0 -25.0 25.0 0.0 0
wkcbd_SR2 0.163576 0.163576 0.0 -25.0 25.0 0.0 0
wkcbd_SR3+ 1.332899 1.332899 0.0 -25.0 25.0 0.0 0
wkcbd_Transit 1.261398 1.261398 0.0 -25.0 25.0 0.0 0
wkcbd_Walk 0.215944 0.215944 0.0 -25.0 25.0 0.0 0
wkempden_Bike 0.001436 0.001436 0.0 -25.0 25.0 0.0 0
wkempden_SR2 0.001074 0.001074 0.0 -25.0 25.0 0.0 0
wkempden_SR3+ 0.001345 0.001345 0.0 -25.0 25.0 0.0 0
wkempden_Transit 0.002893 0.002893 0.0 -25.0 25.0 0.0 0
wkempden_Walk -0.001061 -0.001061 0.0 -25.0 25.0 0.0 0
np.float64(-2296.7365749681976)
m.parameter_summary()
  Value Std Err t Stat Signif Null Value
Parameter          
ASC_Bike -3.13  0.734 -4.27 *** 0.00
ASC_SR2 -1.98  0.129 -15.42 *** 0.00
ASC_SR3+ -3.73  0.186 -20.05 *** 0.00
ASC_Transit -2.16  0.383 -5.63 *** 0.00
ASC_Walk -1.58  0.574 -2.76 ** 0.00
costbyincome -0.0979  0.0162 -6.05 *** 0.00
hhinc#4  0.000361  0.00263  0.14 0.00
hhinc#5 -0.00265  0.00654 -0.41 0.00
hhinc#6  0.000910  0.00406  0.22 0.00
motorized_ovtbydist -0.194  0.0330 -5.87 *** 0.00
motorized_time -0.0188  0.00513 -3.65 *** 0.00
nonmotorized_time -0.0452  0.00873 -5.18 *** 0.00
vehbywrk_Bike -0.217  0.332 -0.65 0.00
vehbywrk_SR -0.238  0.0736 -3.24 ** 0.00
vehbywrk_Transit -0.236  0.136 -1.74 0.00
vehbywrk_Walk -0.0800  0.209 -0.38 0.00
wkcbd_Bike  0.565  0.494  1.14 0.00
wkcbd_SR2  0.164  0.149  1.10 0.00
wkcbd_SR3+  1.33  0.221  6.03 *** 0.00
wkcbd_Transit  1.26  0.244  5.17 *** 0.00
wkcbd_Walk  0.216  0.380  0.57 0.00
wkempden_Bike  0.00144  0.00170  0.85 0.00
wkempden_SR2  0.00107  0.000483  2.22 * 0.00
wkempden_SR3+  0.00135  0.000547  2.46 * 0.00
wkempden_Transit  0.00289  0.000448  6.45 *** 0.00
wkempden_Walk -0.00106  0.00214 -0.49 0.00

It is a little tough to read this report because the parameters show up in alphabetical order. We can use the reorder method to fix this and group them systematically:

m.ordering = (
    (
        "LOS",
        ".*cost.*",
        ".*time.*",
        ".*dist.*",
    ),
    (
        "Zonal",
        "wkcbd.*",
        "wkempden.*",
    ),
    (
        "Household",
        "hhinc.*",
        "vehbywrk.*",
    ),
    (
        "ASCs",
        "ASC.*",
    ),
)
m.parameter_summary()
    Value Std Err t Stat Signif Null Value
Category Parameter          
LOS costbyincome -0.0979  0.0162 -6.05 *** 0.00
motorized_time -0.0188  0.00513 -3.65 *** 0.00
nonmotorized_time -0.0452  0.00873 -5.18 *** 0.00
motorized_ovtbydist -0.194  0.0330 -5.87 *** 0.00
Zonal wkcbd_Bike  0.565  0.494  1.14 0.00
wkcbd_SR2  0.164  0.149  1.10 0.00
wkcbd_SR3+  1.33  0.221  6.03 *** 0.00
wkcbd_Transit  1.26  0.244  5.17 *** 0.00
wkcbd_Walk  0.216  0.380  0.57 0.00
wkempden_Bike  0.00144  0.00170  0.85 0.00
wkempden_SR2  0.00107  0.000483  2.22 * 0.00
wkempden_SR3+  0.00135  0.000547  2.46 * 0.00
wkempden_Transit  0.00289  0.000448  6.45 *** 0.00
wkempden_Walk -0.00106  0.00214 -0.49 0.00
Household hhinc#4  0.000361  0.00263  0.14 0.00
hhinc#5 -0.00265  0.00654 -0.41 0.00
hhinc#6  0.000910  0.00406  0.22 0.00
vehbywrk_Bike -0.217  0.332 -0.65 0.00
vehbywrk_SR -0.238  0.0736 -3.24 ** 0.00
vehbywrk_Transit -0.236  0.136 -1.74 0.00
vehbywrk_Walk -0.0800  0.209 -0.38 0.00
ASCs ASC_Bike -3.13  0.734 -4.27 *** 0.00
ASC_SR2 -1.98  0.129 -15.42 *** 0.00
ASC_SR3+ -3.73  0.186 -20.05 *** 0.00
ASC_Transit -2.16  0.383 -5.63 *** 0.00
ASC_Walk -1.58  0.574 -2.76 ** 0.00

Finally, let’s print model statistics. Note that if you want LL at constants you need to run a separate model.

m.estimation_statistics()
StatisticAggregatePer Case
Number of Cases3808
Log Likelihood at Convergence-2296.74-0.60
Log Likelihood at Null Parameters-5534.18-1.45
Rho Squared w.r.t. Null Parameters0.585
revealed_x
{np.str_('ASC_Bike'): -3.131353684453165,
 np.str_('ASC_SR2'): -1.9829181097087132,
 np.str_('ASC_SR3+'): -3.726010818083811,
 np.str_('ASC_Transit'): -2.1566920213466667,
 np.str_('ASC_Walk'): -1.5835989189277773,
 np.str_('costbyincome'): -0.09786071700049935,
 np.str_('hhinc#4'): 0.0003613865368073872,
 np.str_('hhinc#5'): -0.0026508441167924276,
 np.str_('hhinc#6'): 0.0009101590517921534,
 np.str_('motorized_ovtbydist'): -0.19376038716370456,
 np.str_('motorized_time'): -0.018754569843430064,
 np.str_('nonmotorized_time'): -0.04521035056817283,
 np.str_('vehbywrk_Bike'): -0.21719450776214844,
 np.str_('vehbywrk_SR'): -0.23823848663322233,
 np.str_('vehbywrk_Transit'): -0.2361974332581352,
 np.str_('vehbywrk_Walk'): -0.07998140457805629,
 np.str_('wkcbd_Bike'): 0.565196230101984,
 np.str_('wkcbd_SR2'): 0.16357641468529174,
 np.str_('wkcbd_SR3+'): 1.3328985605250807,
 np.str_('wkcbd_Transit'): 1.2613977817335789,
 np.str_('wkcbd_Walk'): 0.21594406473794295,
 np.str_('wkempden_Bike'): 0.0014358656965849888,
 np.str_('wkempden_SR2'): 0.0010739771398806139,
 np.str_('wkempden_SR3+'): 0.0013450278303126391,
 np.str_('wkempden_Transit'): 0.0028932086005130407,
 np.str_('wkempden_Walk'): -0.0010607308832534465}