6: Motorized IVTT and OVTT#
Model 6 relaxes the travel time constraint further than Model 5 by disaggregating the travel time for motorized modes into distinct components for in-vehicle travel time (IVT) and out-of-vehicle travel time (OVT). This specification allows the two components of travel time for motorized travel to have different effects on utility with the expectation that travelers are more sensitive to out-of-vehicle time than in-vehicle time. (pp. 111)
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
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 PX, P, X
m.utility_co[2] = P("ASC_SR2") + P("hhinc#2,3") * X("hhinc")
m.utility_co[3] = P("ASC_SR3P") + P("hhinc#2,3") * X("hhinc")
m.utility_co[4] = P("ASC_TRAN") + P("hhinc#4") * X("hhinc")
m.utility_co[5] = P("ASC_BIKE") + P("hhinc#5") * X("hhinc")
m.utility_co[6] = P("ASC_WALK") + P("hhinc#6") * X("hhinc")
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.
People may value time differently depending on what mode they’re in, so we’ll decompose tottime into ovtt and ivtt for the motorized alternatives.
m.utility_ca = (
+P("nonmotorized_time") * X("(altid>4) * tottime")
+ P("motorized_ovtt") * X("(altid <= 4) * ovtt")
+ P("motorized_ivtt") * X("(altid <= 4) * ivtt")
+ PX("totcost")
)
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 6, Motorized Times"
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 = -3588.038619702086
value | best | initvalue | minimum | maximum | nullvalue | holdfast | |
---|---|---|---|---|---|---|---|
param_name | |||||||
ASC_BIKE | -1.719259 | -1.719259 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
ASC_SR2 | -2.429982 | -2.429982 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
ASC_SR3P | -3.883419 | -3.883419 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
ASC_TRAN | -0.489858 | -0.489858 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
ASC_WALK | 0.409083 | 0.409083 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
hhinc#2,3 | -0.001584 | -0.001584 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
hhinc#4 | -0.005693 | -0.005693 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
hhinc#5 | -0.012217 | -0.012217 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
hhinc#6 | -0.009302 | -0.009302 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
motorized_ivtt | -0.002540 | -0.002540 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
motorized_ovtt | -0.075942 | -0.075942 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
nonmotorized_time | -0.063194 | -0.063194 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
totcost | -0.004798 | -0.004798 | 0.0 | -20.0 | 20.0 | 0.0 | 0 |
np.float64(-3588.038619702086)
m.parameter_summary()
Value | Std Err | t Stat | Signif | Null Value | |
---|---|---|---|---|---|
Parameter | |||||
ASC_BIKE | -1.72 | 0.323 | -5.32 | *** | 0.00 |
ASC_SR2 | -2.43 | 0.101 | -24.14 | *** | 0.00 |
ASC_SR3P | -3.88 | 0.127 | -30.54 | *** | 0.00 |
ASC_TRAN | -0.490 | 0.149 | -3.29 | ** | 0.00 |
ASC_WALK | 0.409 | 0.253 | 1.62 | 0.00 | |
hhinc#2,3 | -0.00158 | 0.00139 | -1.14 | 0.00 | |
hhinc#4 | -0.00569 | 0.00185 | -3.07 | ** | 0.00 |
hhinc#5 | -0.0122 | 0.00524 | -2.33 | * | 0.00 |
hhinc#6 | -0.00930 | 0.00305 | -3.05 | ** | 0.00 |
motorized_ivtt | -0.00254 | 0.00620 | -0.41 | 0.00 | |
motorized_ovtt | -0.0759 | 0.00586 | -12.96 | *** | 0.00 |
nonmotorized_time | -0.0632 | 0.00536 | -11.79 | *** | 0.00 |
totcost | -0.00480 | 0.000237 | -20.21 | *** | 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",
"totcost",
"nonmotorized_time",
"motorized_ivtt",
"motorized_ovtt",
),
(
"Income",
"hhinc.*",
),
(
"ASCs",
"ASC.*",
),
)
m.parameter_summary()
Value | Std Err | t Stat | Signif | Null Value | ||
---|---|---|---|---|---|---|
Category | Parameter | |||||
LOS | totcost | -0.00480 | 0.000237 | -20.21 | *** | 0.00 |
nonmotorized_time | -0.0632 | 0.00536 | -11.79 | *** | 0.00 | |
motorized_ivtt | -0.00254 | 0.00620 | -0.41 | 0.00 | ||
motorized_ovtt | -0.0759 | 0.00586 | -12.96 | *** | 0.00 | |
Income | hhinc#2,3 | -0.00158 | 0.00139 | -1.14 | 0.00 | |
hhinc#4 | -0.00569 | 0.00185 | -3.07 | ** | 0.00 | |
hhinc#5 | -0.0122 | 0.00524 | -2.33 | * | 0.00 | |
hhinc#6 | -0.00930 | 0.00305 | -3.05 | ** | 0.00 | |
ASCs | ASC_BIKE | -1.72 | 0.323 | -5.32 | *** | 0.00 |
ASC_SR2 | -2.43 | 0.101 | -24.14 | *** | 0.00 | |
ASC_SR3P | -3.88 | 0.127 | -30.54 | *** | 0.00 | |
ASC_TRAN | -0.490 | 0.149 | -3.29 | ** | 0.00 | |
ASC_WALK | 0.409 | 0.253 | 1.62 | 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 | -3588.04 | -0.71 |
Log Likelihood at Null Parameters | -7309.60 | -1.45 |
Rho Squared w.r.t. Null Parameters | 0.509 |