17D: Gender Segmentation (Female)#

Model 17D segments the market by gender for females. (pp. 135)

import larch
import larch

larch.__version__
'6.0.42'

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
d = d.sel(caseid=d.femdum == 1)
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 17D, Segmented for females"

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 1512 2026
2 SR2 237 2187
3 SR3+ 62 2187
4 Transit 264 1850
5 Bike 16 828
6 Walk 96 760
< Total All Alternatives > 2187 <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 091 [Optimization terminated successfully]

Best LL = -1511.338640296568

value best initvalue minimum maximum nullvalue holdfast
param_name
ASC_Bike -1.041278 -1.041278 0.0 -25.0 25.0 0.0 0
ASC_SR2 -1.553981 -1.553981 0.0 -25.0 25.0 0.0 0
ASC_SR3+ -3.185431 -3.185431 0.0 -25.0 25.0 0.0 0
ASC_Transit -0.469768 -0.469768 0.0 -25.0 25.0 0.0 0
ASC_Walk 1.368870 1.368870 0.0 -25.0 25.0 0.0 0
costbyincome -0.043530 -0.043530 0.0 -25.0 25.0 0.0 0
hhinc#4 -0.008930 -0.008930 0.0 -25.0 25.0 0.0 0
hhinc#5 -0.038410 -0.038410 0.0 -25.0 25.0 0.0 0
hhinc#6 -0.005082 -0.005082 0.0 -25.0 25.0 0.0 0
motorized_ovtbydist -0.088797 -0.088797 0.0 -25.0 25.0 0.0 0
motorized_time -0.019135 -0.019135 0.0 -25.0 25.0 0.0 0
nonmotorized_time -0.071258 -0.071258 0.0 -25.0 25.0 0.0 0
vehbywrk_Bike -0.103433 -0.103433 0.0 -25.0 25.0 0.0 0
vehbywrk_SR -0.613552 -0.613552 0.0 -25.0 25.0 0.0 0
vehbywrk_Transit -1.179432 -1.179432 0.0 -25.0 25.0 0.0 0
vehbywrk_Walk -0.917891 -0.917891 0.0 -25.0 25.0 0.0 0
wkcbd_Bike 1.053409 1.053409 0.0 -25.0 25.0 0.0 0
wkcbd_SR2 0.451942 0.451942 0.0 -25.0 25.0 0.0 0
wkcbd_SR3+ 0.368589 0.368589 0.0 -25.0 25.0 0.0 0
wkcbd_Transit 1.381185 1.381185 0.0 -25.0 25.0 0.0 0
wkcbd_Walk -0.008517 -0.008517 0.0 -25.0 25.0 0.0 0
wkempden_Bike 0.004040 0.004040 0.0 -25.0 25.0 0.0 0
wkempden_SR2 0.002991 0.002991 0.0 -25.0 25.0 0.0 0
wkempden_SR3+ 0.005135 0.005135 0.0 -25.0 25.0 0.0 0
wkempden_Transit 0.004566 0.004566 0.0 -25.0 25.0 0.0 0
wkempden_Walk 0.005465 0.005465 0.0 -25.0 25.0 0.0 0
np.float64(-1511.338640296878)
m.parameter_summary()
  Value Std Err t Stat Signif Null Value
Parameter          
ASC_Bike -1.04  0.767 -1.36 0.00
ASC_SR2 -1.55  0.182 -8.54 *** 0.00
ASC_SR3+ -3.19  0.245 -13.00 *** 0.00
ASC_Transit -0.470  0.359 -1.31 0.00
ASC_Walk  1.37  0.507  2.70 ** 0.00
costbyincome -0.0435  0.0149 -2.93 ** 0.00
hhinc#4 -0.00893  0.00300 -2.98 ** 0.00
hhinc#5 -0.0384  0.0140 -2.75 ** 0.00
hhinc#6 -0.00508  0.00446 -1.14 0.00
motorized_ovtbydist -0.0888  0.0253 -3.51 *** 0.00
motorized_time -0.0191  0.00559 -3.42 *** 0.00
nonmotorized_time -0.0713  0.00943 -7.55 *** 0.00
vehbywrk_Bike -0.103  0.413 -0.25 0.00
vehbywrk_SR -0.614  0.136 -4.51 *** 0.00
vehbywrk_Transit -1.18  0.189 -6.23 *** 0.00
vehbywrk_Walk -0.918  0.271 -3.39 *** 0.00
wkcbd_Bike  1.05  0.591  1.78 0.00
wkcbd_SR2  0.452  0.181  2.50 * 0.00
wkcbd_SR3+  0.369  0.337  1.10 0.00
wkcbd_Transit  1.38  0.232  5.95 *** 0.00
wkcbd_Walk -0.00852  0.350 -0.02 0.00
wkempden_Bike  0.00404  0.00220  1.84 0.00
wkempden_SR2  0.00299  0.000661  4.53 *** 0.00
wkempden_SR3+  0.00513  0.000773  6.65 *** 0.00
wkempden_Transit  0.00457  0.000653  6.99 *** 0.00
wkempden_Walk  0.00547  0.00116  4.69 *** 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.0435  0.0149 -2.93 ** 0.00
motorized_time -0.0191  0.00559 -3.42 *** 0.00
nonmotorized_time -0.0713  0.00943 -7.55 *** 0.00
motorized_ovtbydist -0.0888  0.0253 -3.51 *** 0.00
Zonal wkcbd_Bike  1.05  0.591  1.78 0.00
wkcbd_SR2  0.452  0.181  2.50 * 0.00
wkcbd_SR3+  0.369  0.337  1.10 0.00
wkcbd_Transit  1.38  0.232  5.95 *** 0.00
wkcbd_Walk -0.00852  0.350 -0.02 0.00
wkempden_Bike  0.00404  0.00220  1.84 0.00
wkempden_SR2  0.00299  0.000661  4.53 *** 0.00
wkempden_SR3+  0.00513  0.000773  6.65 *** 0.00
wkempden_Transit  0.00457  0.000653  6.99 *** 0.00
wkempden_Walk  0.00547  0.00116  4.69 *** 0.00
Household hhinc#4 -0.00893  0.00300 -2.98 ** 0.00
hhinc#5 -0.0384  0.0140 -2.75 ** 0.00
hhinc#6 -0.00508  0.00446 -1.14 0.00
vehbywrk_Bike -0.103  0.413 -0.25 0.00
vehbywrk_SR -0.614  0.136 -4.51 *** 0.00
vehbywrk_Transit -1.18  0.189 -6.23 *** 0.00
vehbywrk_Walk -0.918  0.271 -3.39 *** 0.00
ASCs ASC_Bike -1.04  0.767 -1.36 0.00
ASC_SR2 -1.55  0.182 -8.54 *** 0.00
ASC_SR3+ -3.19  0.245 -13.00 *** 0.00
ASC_Transit -0.470  0.359 -1.31 0.00
ASC_Walk  1.37  0.507  2.70 ** 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 Cases2187
Log Likelihood at Convergence-1511.34-0.69
Log Likelihood at Null Parameters-3240.79-1.48
Rho Squared w.r.t. Null Parameters0.534