16: Cost by Income#
Another approach to the inclusion of trip maker or context characteristics is through interactions with mode attributes. The most common example of this approach is to take account of the expectation that low-income travelers will be more sensitive to travel cost than high-income travelers by using cost divided by income in place of cost as an explanatory variable. Such a specification implies that the importance of cost in mode choice diminishes with increasing household income.
Model 16 drops travel cost to include travel cost divided by income. (pp. 125)
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
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 [2, 3]:
m.utility_co[a] = +P("hhinc#2,3") * X("hhinc")
for a in [4, 5, 6]:
m.utility_co[a] = +P(f"hhinc#{a}") * X("hhinc")
Sometimes we may want to define a part of the utility function that is common across all (or almost all) of the alternatives. We can access a dictionary (more generically called a “mapping”) of alternative codes to alternative names, which can be found via the Dataset.dc.alts_mapping attribute:
d.dc.alts_mapping
{np.int64(1): np.str_('DA'),
np.int64(2): np.str_('SR2'),
np.int64(3): np.str_('SR3+'),
np.int64(4): np.str_('Transit'),
np.int64(5): np.str_('Bike'),
np.int64(6): np.str_('Walk')}
Using this like a standard Python dictionary, we can iterate over all the alternatives, skipping 1, and setting alternative specific constants (ASC’s) for the rest.
for a, name in d.dc.alts_mapping.items():
if a == 1:
continue
m.utility_co[a] += (
+P("ASC_" + name)
+ P("vehbywrk_" + name) * X("vehbywrk")
+ P("wkcbd_" + name) * X("wkccbd + wknccbd")
+ P("wkempden_" + name) * X("wkempden")
)
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 = (
+P("nonmotorized_time") * X("(altid> 4) * tottime")
+ P("motorized_time") * X("(altid <= 4) * ivtt")
+ (P("motorized_time") + (P("motorized_ovtbydist") / X("dist")))
* X("(altid <= 4) * ovtt")
+ P("costbyinc") * X("totcost/hhinc")
)
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 16, Cost by Income"
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 | 3637 | 4755 |
2 | SR2 | 517 | 5029 |
3 | SR3+ | 161 | 5029 |
4 | Transit | 498 | 4003 |
5 | Bike | 50 | 1738 |
6 | Walk | 166 | 1479 |
< Total All Alternatives > | 5029 | <NA> |
We’ll set a parameter cap (bound) at +/- 20, which helps improve the numerical stability of the optimization algorithm used in estimation.
m.set_cap(20)
Having created this model, we can then estimate it:
assert m.compute_engine == "numba"
result = m.maximize_loglike(stderr=True, method="bhhh")
m.calculate_parameter_covariance()
m.loglike()
Iteration 010 [Optimization terminated successfully]
Best LL = -3442.33405704741
value | best | initvalue | minimum | maximum | nullvalue | holdfast | |
---|---|---|---|---|---|---|---|
param_name | |||||||
ASC_Bike | -1.621777 | -1.621777 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
ASC_SR2 | -1.729799 | -1.729799 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
ASC_SR3+ | -3.656256 | -3.656256 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
ASC_Transit | -0.691704 | -0.691704 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
ASC_Walk | 0.075215 | 0.075215 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
costbyinc | -0.051774 | -0.051774 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
hhinc#2,3 | 0.000037 | 0.000037 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
hhinc#4 | -0.005336 | -0.005336 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
hhinc#5 | -0.008672 | -0.008672 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
hhinc#6 | -0.006017 | -0.006017 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
motorized_ovtbydist | -0.132722 | -0.132722 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
motorized_time | -0.020158 | -0.020158 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
nonmotorized_time | -0.045439 | -0.045439 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
vehbywrk_Bike | -0.704065 | -0.704065 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
vehbywrk_SR2 | -0.381617 | -0.381617 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
vehbywrk_SR3+ | -0.138805 | -0.138805 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
vehbywrk_Transit | -0.937505 | -0.937505 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
vehbywrk_Walk | -0.723853 | -0.723853 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
wkcbd_Bike | 0.486324 | 0.486324 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
wkcbd_SR2 | 0.247142 | 0.247142 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
wkcbd_SR3+ | 1.094359 | 1.094359 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
wkcbd_Transit | 1.305616 | 1.305616 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
wkcbd_Walk | 0.097248 | 0.097248 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
wkempden_Bike | 0.001922 | 0.001922 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
wkempden_SR2 | 0.001596 | 0.001596 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
wkempden_SR3+ | 0.002204 | 0.002204 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
wkempden_Transit | 0.003132 | 0.003132 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
wkempden_Walk | 0.002881 | 0.002881 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
np.float64(-3442.33405704741)
m.parameter_summary()
Value | Std Err | t Stat | Signif | Null Value | |
---|---|---|---|---|---|
Parameter | |||||
ASC_Bike | -1.62 | 0.429 | -3.78 | *** | 0.00 |
ASC_SR2 | -1.73 | 0.139 | -12.48 | *** | 0.00 |
ASC_SR3+ | -3.66 | 0.206 | -17.74 | *** | 0.00 |
ASC_Transit | -0.692 | 0.249 | -2.77 | ** | 0.00 |
ASC_Walk | 0.0752 | 0.349 | 0.22 | 0.00 | |
costbyinc | -0.0518 | 0.0107 | -4.84 | *** | 0.00 |
hhinc#2,3 | 3.69e-05 | 0.00141 | 0.03 | 0.00 | |
hhinc#4 | -0.00534 | 0.00205 | -2.61 | ** | 0.00 |
hhinc#5 | -0.00867 | 0.00518 | -1.68 | 0.00 | |
hhinc#6 | -0.00602 | 0.00318 | -1.89 | 0.00 | |
motorized_ovtbydist | -0.133 | 0.0196 | -6.76 | *** | 0.00 |
motorized_time | -0.0202 | 0.00381 | -5.28 | *** | 0.00 |
nonmotorized_time | -0.0454 | 0.00577 | -7.88 | *** | 0.00 |
vehbywrk_Bike | -0.704 | 0.259 | -2.72 | ** | 0.00 |
vehbywrk_SR2 | -0.382 | 0.0766 | -4.98 | *** | 0.00 |
vehbywrk_SR3+ | -0.139 | 0.109 | -1.27 | 0.00 | |
vehbywrk_Transit | -0.938 | 0.118 | -7.91 | *** | 0.00 |
vehbywrk_Walk | -0.724 | 0.170 | -4.27 | *** | 0.00 |
wkcbd_Bike | 0.486 | 0.361 | 1.35 | 0.00 | |
wkcbd_SR2 | 0.247 | 0.124 | 1.99 | * | 0.00 |
wkcbd_SR3+ | 1.09 | 0.191 | 5.73 | *** | 0.00 |
wkcbd_Transit | 1.31 | 0.166 | 7.88 | *** | 0.00 |
wkcbd_Walk | 0.0972 | 0.252 | 0.39 | 0.00 | |
wkempden_Bike | 0.00192 | 0.00122 | 1.58 | 0.00 | |
wkempden_SR2 | 0.00160 | 0.000394 | 4.05 | *** | 0.00 |
wkempden_SR3+ | 0.00220 | 0.000455 | 4.84 | *** | 0.00 |
wkempden_Transit | 0.00313 | 0.000364 | 8.62 | *** | 0.00 |
wkempden_Walk | 0.00288 | 0.000743 | 3.88 | *** | 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.*",
),
(
"Income",
"hhinc.*",
),
(
"Ownership",
"vehbywrk.*",
),
(
"Zonal",
"wkcbd.*",
"wkempden.*",
),
(
"ASCs",
"ASC.*",
),
)
m.parameter_summary()
Value | Std Err | t Stat | Signif | Null Value | ||
---|---|---|---|---|---|---|
Category | Parameter | |||||
LOS | costbyinc | -0.0518 | 0.0107 | -4.84 | *** | 0.00 |
motorized_time | -0.0202 | 0.00381 | -5.28 | *** | 0.00 | |
nonmotorized_time | -0.0454 | 0.00577 | -7.88 | *** | 0.00 | |
motorized_ovtbydist | -0.133 | 0.0196 | -6.76 | *** | 0.00 | |
Income | hhinc#2,3 | 3.69e-05 | 0.00141 | 0.03 | 0.00 | |
hhinc#4 | -0.00534 | 0.00205 | -2.61 | ** | 0.00 | |
hhinc#5 | -0.00867 | 0.00518 | -1.68 | 0.00 | ||
hhinc#6 | -0.00602 | 0.00318 | -1.89 | 0.00 | ||
Ownership | vehbywrk_Bike | -0.704 | 0.259 | -2.72 | ** | 0.00 |
vehbywrk_SR2 | -0.382 | 0.0766 | -4.98 | *** | 0.00 | |
vehbywrk_SR3+ | -0.139 | 0.109 | -1.27 | 0.00 | ||
vehbywrk_Transit | -0.938 | 0.118 | -7.91 | *** | 0.00 | |
vehbywrk_Walk | -0.724 | 0.170 | -4.27 | *** | 0.00 | |
Zonal | wkcbd_Bike | 0.486 | 0.361 | 1.35 | 0.00 | |
wkcbd_SR2 | 0.247 | 0.124 | 1.99 | * | 0.00 | |
wkcbd_SR3+ | 1.09 | 0.191 | 5.73 | *** | 0.00 | |
wkcbd_Transit | 1.31 | 0.166 | 7.88 | *** | 0.00 | |
wkcbd_Walk | 0.0972 | 0.252 | 0.39 | 0.00 | ||
wkempden_Bike | 0.00192 | 0.00122 | 1.58 | 0.00 | ||
wkempden_SR2 | 0.00160 | 0.000394 | 4.05 | *** | 0.00 | |
wkempden_SR3+ | 0.00220 | 0.000455 | 4.84 | *** | 0.00 | |
wkempden_Transit | 0.00313 | 0.000364 | 8.62 | *** | 0.00 | |
wkempden_Walk | 0.00288 | 0.000743 | 3.88 | *** | 0.00 | |
ASCs | ASC_Bike | -1.62 | 0.429 | -3.78 | *** | 0.00 |
ASC_SR2 | -1.73 | 0.139 | -12.48 | *** | 0.00 | |
ASC_SR3+ | -3.66 | 0.206 | -17.74 | *** | 0.00 | |
ASC_Transit | -0.692 | 0.249 | -2.77 | ** | 0.00 | |
ASC_Walk | 0.0752 | 0.349 | 0.22 | 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()
Statistic | Aggregate | Per Case |
---|---|---|
Number of Cases | 5029 | |
Log Likelihood at Convergence | -3442.33 | -0.68 |
Log Likelihood at Null Parameters | -7309.60 | -1.45 |
Rho Squared w.r.t. Null Parameters | 0.529 |