301: Itinerary Choice using MNL

301: Itinerary Choice using MNL

import larch.numba as lx
/home/runner/work/larch/larch/larch/larch/numba/model.py:23: UserWarning: 

### larch.numba is experimental, and not feature-complete ###
 the first time you import on a new system, this package will
 compile optimized binaries for your machine, which may take 
 a little while, please be patient 

  warnings.warn( ### EXPERIMENTAL ### )

This example is an itinerary choice model built using the example itinerary choice dataset included with Larch. We’ll begin by loading that example data.

d = lx.Dataset.from_idce(
    pd.read_csv(lx.example_file("arc"), index_col=['id_case','id_alt']),
)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [2], in <module>
      1 d = lx.Dataset.from_idce(
----> 2     pd.read_csv(lx.example_file("arc"), index_col=['id_case','id_alt']),
      3 )

NameError: name 'pd' is not defined

Now let’s make our model. We’ll use a few variables to define our linear-in-parameters utility function.

m = lx.Model(datatree=d)

v = [
    "timeperiod==2",
    "timeperiod==3",
    "timeperiod==4",
    "timeperiod==5",
    "timeperiod==6",
    "timeperiod==7",
    "timeperiod==8",
    "timeperiod==9",
    "carrier==2",
    "carrier==3",
    "carrier==4",
    "carrier==5",
    "equipment==2",
    "fare_hy",    
    "fare_ly",    
    "elapsed_time",  
    "nb_cnxs",       
]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [4], in <module>
----> 1 m = lx.Model(datatree=d)
      3 v = [
      4     "timeperiod==2",
      5     "timeperiod==3",
   (...)
     20     "nb_cnxs",       
     21 ]

NameError: name 'd' is not defined

The larch.roles module defines a few convenient classes for declaring data and parameter. One we will use here is PX which creates a linear-in-parameter term that represents one data element (a column from our data, or an expression that can be evaluated on the data alone) multiplied by a parameter with the same name.

from larch.roles import PX
m.utility_ca = sum(PX(i) for i in v)
m.choice_ca_var = 'choice'
m.availability_var = 1
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [5], in <module>
      1 from larch.roles import PX
----> 2 m.utility_ca = sum(PX(i) for i in v)
      3 m.choice_ca_var = 'choice'
      4 m.availability_var = 1

NameError: name 'v' is not defined

Since we are estimating just an MNL model in this example, this is all we need to do to build our model, and we’re ready to go. To estimate the likelihood maximizing parameters, we give:

m.maximize_loglike()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [7], in <module>
----> 1 m.maximize_loglike()

NameError: name 'm' is not defined