1: MTC MNL Mode Choice

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

d = larch.DB.Example('MTC')
m = larch.Model(d)

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

from larch.roles import P, X, PX
m.utility.co[2] = P("ASC_SR2")  + P("hhinc#2") * X("hhinc")
m.utility.co[3] = P("ASC_SR3P") + P("hhinc#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 LinearFunction that is applied across all alternatives using idca Format data. Because the data is structured to vary across alternatives, the parameters (and thus the structure of the LinearFunction) does not need to vary across alternatives.

m.utility.ca = PX("tottime") + PX("totcost")

We can specify some model options too. And let’s give our model a descriptive title.

m.option.calc_std_errors = True
m.title = "MTC Example 1 (Simple MNL)"

Having created this model, we can then estimate it:

>>> result = m.maximize_loglike()
>>> result.message
'Optimization terminated successfully per computed tolerance. [bhhh]'
>>> m.loglike()
-3626.18...

>>> print(m.report('txt', sigfigs=3))
============================================================================================
MTC Example 1 (Simple MNL)
============================================================================================
Model Parameter Estimates
--------------------------------------------------------------------------------------------
Parameter       InitValue       FinalValue      StdError        t-Stat          NullValue
tottime          0.0            -0.0513          0.0031         -16.6            0.0
totcost          0.0            -0.00492         0.000239       -20.6            0.0
ASC_SR2          0.0            -2.18            0.105          -20.8            0.0
hhinc#2          0.0            -0.00217         0.00155        -1.4             0.0
ASC_SR3P         0.0            -3.73            0.178          -21.0            0.0
hhinc#3          0.0             0.000358        0.00254         0.141           0.0
ASC_TRAN         0.0            -0.671           0.133          -5.06            0.0
hhinc#4          0.0            -0.00529         0.00183        -2.89            0.0
ASC_BIKE         0.0            -2.38            0.305          -7.8             0.0
hhinc#5          0.0            -0.0128          0.00532        -2.41            0.0
ASC_WALK         0.0            -0.207           0.194          -1.07            0.0
hhinc#6          0.0            -0.00969         0.00303        -3.19            0.0
============================================================================================
Model Estimation Statistics
--------------------------------------------------------------------------------------------
Log Likelihood at Convergence           -3626.19
Log Likelihood at Null Parameters       -7309.60
--------------------------------------------------------------------------------------------
Rho Squared w.r.t. Null Parameters      0.504
============================================================================================
...

It is a little tough to read this report because the ASC and income parameters are interweaved. We can use the reorder method to fix this:

m.reorder_parameters("ASC", "hhinc")

Then the report will look more reasonable (although ultimately the content is the same):

>>> print(m.report('txt', sigfigs=3))
============================================================================================
MTC Example 1 (Simple MNL)
============================================================================================
Model Parameter Estimates
--------------------------------------------------------------------------------------------
Parameter       InitValue       FinalValue      StdError        t-Stat          NullValue
ASC_SR2          0.0            -2.18            0.105          -20.8            0.0
ASC_SR3P         0.0            -3.73            0.178          -21.0            0.0
ASC_TRAN         0.0            -0.671           0.133          -5.06            0.0
ASC_BIKE         0.0            -2.38            0.305          -7.8             0.0
ASC_WALK         0.0            -0.207           0.194          -1.07            0.0
hhinc#2          0.0            -0.00217         0.00155        -1.4             0.0
hhinc#3          0.0             0.000358        0.00254         0.141           0.0
hhinc#4          0.0            -0.00529         0.00183        -2.89            0.0
hhinc#5          0.0            -0.0128          0.00532        -2.41            0.0
hhinc#6          0.0            -0.00969         0.00303        -3.19            0.0
tottime          0.0            -0.0513          0.0031         -16.6            0.0
totcost          0.0            -0.00492         0.000239       -20.6            0.0
============================================================================================
Model Estimation Statistics
--------------------------------------------------------------------------------------------
Log Likelihood at Convergence           -3626.19
Log Likelihood at Null Parameters       -7309.60
--------------------------------------------------------------------------------------------
Rho Squared w.r.t. Null Parameters      0.504
============================================================================================
...

You can then access individual parameters from the model either by name or number (using zero-based indexing).

>>> m[0]
ModelParameter('ASC_SR2', value=-2.17...)

>>> m['totcost']
ModelParameter('totcost', value=-0.00492...)

The len() function retrieves the number of parameters.

>>> len(m)
12

You can get a list of the parameter names in order.

>>> m.parameter_names()
['ASC_SR2', 'ASC_SR3P', 'ASC_TRAN', 'ASC_BIKE', 'ASC_WALK', 'hhinc#2',
'hhinc#3', 'hhinc#4', 'hhinc#5', 'hhinc#6', 'tottime', 'totcost']