101: Swissmetro MNL Mode Choice

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

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

We can attach a title to the model. The title does not affect the calculations as all; it is merely used in various output report styles.

m.title = "swissmetro example 01 (simple logit)"

The swissmetro dataset, as with all Biogeme data, is only in co format.

from larch.roles import P,X
m.utility[1] = P("ASC_TRAIN")
m.utility[2] = 0
m.utility[3] = P("ASC_CAR")
m.utility[1] += X("TRAIN_TT") * P("B_TIME")
m.utility[2] += X("SM_TT") * P("B_TIME")
m.utility[3] += X("CAR_TT") * P("B_TIME")
m.utility[1] += X("TRAIN_CO*(GA==0)") * P("B_COST")
m.utility[2] += X("SM_CO*(GA==0)") * P("B_COST")
m.utility[3] += X("CAR_CO") * P("B_COST")

Larch will find all the parameters in the model, but we’d like to output them in a particular order, so we want to reorder the parameters. We can use the reorder method to fix this:

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

We can estimate the models and check the results match up with those given by Biogeme:

>>> m.maximize_loglike()
messages: Optimization terminated successfully ...
>>> m.loglike()
-5331.252...
>>> m['B_TIME'].value
-0.01277...
>>> m['B_COST'].value
-0.01083...
>>> m['ASC_TRAIN'].value
-0.701...
>>> m['ASC_CAR'].value
-0.1546...
>>> print(m.report('txt', sigfigs=3))
=========================================================================================...
swissmetro example 01 (simple logit)
=========================================================================================...
Model Parameter Estimates
-----------------------------------------------------------------------------------------...
Parameter       InitValue       FinalValue      StdError        t-Stat          NullValue
ASC_TRAIN        0.0            -0.701           0.0549         -12.8            0.0
ASC_CAR          0.0            -0.155           0.0432         -3.58            0.0
B_TIME           0.0            -0.0128          0.000569       -22.5            0.0
B_COST           0.0            -0.0108          0.000518       -20.9            0.0
=========================================================================================...
Model Estimation Statistics
-----------------------------------------------------------------------------------------...
Log Likelihood at Convergence           -5331.25
Log Likelihood at Null Parameters       -6964.66
-----------------------------------------------------------------------------------------...
Rho Squared w.r.t. Null Parameters      0.235
=========================================================================================...

Tip

If you want access to the model in this example without worrying about assembling all the code blocks together on your own, you can load a read-to-estimate copy like this:

m = larch.Model.Example(101)