Notebook: Exampville Mode Choice

Some of the very cool output features of Larch are only available, or best utilized, in a jupyter notebook. This page shows some of the output available for the mode choice model.

In [1]:
import larch

Larch 3.3.24.7031114d

In [2]:
from larch.roles import P,X
d, nZones, omx = larch.examples.reproduce(200, ['d', 'nZones', 'omx'])

DT files can have embedded data descriptions and dictionaries. These can be viewed in a summary table by invoking the info method.

In [3]:
d.info(3)
Out[3]:

<larch.DT> tmpox1ses8b.h5f

VariabledtypeShapeOriginal SourceDescriptionDictionary
AGEint64(6123,)tmp3emoc44z​/exampville_person.h5None
AUTO_TIMEfloat64(6123,)tmp3emoc44z​/exampville.omx
BIKETIMEfloat64(6123,)= DIST ​/ 12 * 60 * (DIST<=15)
CARCOSTfloat64(6123,)= DIST * 0.20
DISTfloat64(6123,)tmp3emoc44z​/exampville.omx
DTAZint64(6123,)tmp3emoc44z​/exampville_tours.h5None
DTAZiint64(6123,)= DTAZ-1
HHIDint64(6123,)tmp3emoc44z​/exampville_tours.h5None
HHSIZEint64(6123,)tmp3emoc44z​/exampville_hh.h5None
HOMETAZint64(6123,)tmp3emoc44z​/exampville_hh.h5None
HOMETAZiint64(6123,)= HOMETAZ-1
INCOMEint64(6123,)tmp3emoc44z​/exampville_hh.h5None
N_OTHERTOURSint64(6123,)tmp3emoc44z​/exampville_person.h5None
N_TOTALTOURSint64(6123,)tmp3emoc44z​/exampville_person.h5None
N_WORKTOURSint64(6123,)tmp3emoc44z​/exampville_person.h5None
PERSONIDint64(6123,)tmp3emoc44z​/exampville_tours.h5None
RAIL_FAREfloat64(6123,)tmp3emoc44z​/exampville.omx
RAIL_TIMEfloat64(6123,)tmp3emoc44z​/exampville.omx
TOURMODEint64(6123,)tmp3emoc44z​/exampville_tours.h5
1DA
2SR
3Walk
4Bike
5Transit
TOURPURPint64(6123,)tmp3emoc44z​/exampville_tours.h5
1Work Tour
2Non-Work Tour
WALKTIMEfloat64(6123,)= DIST ​/ 2.5 * 60 * (DIST<=3)
WORKSint64(6123,)tmp3emoc44z​/exampville_person.h5
0No
1Yes
caseidint64(6123,)tmp3emoc44z​/exampville_tours.h5
_avail_<stack>¿group?.
_choice_<stack>¿group?.
In [4]:
m = larch.Model.Example(201, d=d)

The maximize_loglike method displays an live updated table of parameters during estimation inside a notebook (although this is not visible in the static version here). Upon competion, well formatted tables describing the results are displayed.

In [5]:
m.maximize_loglike()

Model Parameter Estimates

ParameterEstimated ValueStd Errort-StatNull Value
Level of Service
InVehTime-0.1127 0.01356 -8.31 0
NonMotorTime-0.2255 = InVehTime * 2
Cost-0.3407 0.1812 -1.88 0
Alternative Specific Constants
ASC_SR-1.394 0.9541 -1.46 0
ASC_Walk 0.7318 0.3015 2.43 0
ASC_Bike-1.667 0.2739 -6.09 0
ASC_Transit-1.677 0.4977 -3.37 0
Income
HighIncSR-1.756 1.29 -1.36 0
Walk-0.7958 0.4189 -1.90 0
Bike-1.053 0.4631 -2.27 0
Transit-1.241 0.4508 -2.75 0
Logsum Parameters
NestCar 0.7417 0.5384 -0.48 1
NonMotor 0.8697 0.1835 -0.71 1
Motorized 0.8444 0.2487 -0.63 1

Model Estimation Statistics

StatisticAggregatePer Case
Number of Cases1897
Log Likelihood at Convergence-963.84-0.51
Log Likelihood at Null Parameters-2592.70-1.37
Rho Squared w.r.t. Null Parameters0.628

Latest Estimation Run Statistics

Estimation DateSaturday, March 11 2017, 03:00:21 PM
Resultssuccess
MessageOptimization terminated successfully. [SLSQP]
Optimization MethodSLSQP
Number of Iterations42
Running TimeTotal2.437 seconds
setup0:00.12
null_likelihood0:00
weight choice rebalance0:00
weight autorescale0:00
optimize:SLSQP0:02.09
parameter covariance0:00.23
cleanup0:00
Notesdid not automatically rescale weights
Computerserenity.thunderdome
ProcessorIntel(R) Core(TM) i5-6267U CPU @ 2.90GHz
Number of CPU Cores4
Number of Threads Used4
Installed Memory16.0 GiB
Peak Memory Usage151.0625 MiB
Out[5]:
messages: Optimization terminated successfully. [SLSQP]:
                  |     SLSQP:Optimization terminated successfully.
              ctol: 1.719309355075853e-10
               fun: 963.8426415309935
  installed_memory: '16.0 GiB'
               jac: array([ -3.64166842e-04,  -2.93506916e-04,  -2.03839830e-05,
                  |         -3.39775863e-05,   1.13043135e-05,  -1.04367650e-04,
                  |         -4.77640173e-05,  -1.19343496e-06,   3.87701861e-06,
                  |         -1.88856856e-05,  -5.70198558e-06,  -4.50457556e-06,
                  |         -9.44207318e-06,   0.00000000e+00])
           loglike: -963.8426415309935
      loglike_null: -2592.698004024908
           message: 'Optimization terminated successfully. [SLSQP]'
               nit: 42
             niter: [('SLSQP', 42)]
 peak_memory_usage: '151.0625 MiB'
             stats: <larch.core.runstats, success in 0:02.44>
            status: 0
           success: True
                 x: array([-0.11274266,  0.74170259,  0.86969656,  0.84439806, -0.3407393 ,
                  |        -1.39431543, -1.7562362 ,  0.73177238, -0.79580814, -1.6667516 ,
                  |        -1.0525934 , -1.67706804, -1.24100481])

You can pull out a single one of these sections to display…

In [6]:
m.xhtml('ll')
Out[6]:

Model Estimation Statistics

StatisticAggregatePer Case
Number of Cases1897
Log Likelihood at Convergence-963.84-0.51
Log Likelihood at Null Parameters-2592.70-1.37
Rho Squared w.r.t. Null Parameters0.628

…or lump several together into a neat report. Valid sections that happen to not have content are silently skipped. For example, there are no notes on the current example model so nothing appears before the parameters. Invalid section names, on the other hand, give a warning.

In [7]:
sections = [
    'notes',
    'params',
    'll',
    'nesting_tree',
    'latest',
    'utilityspec',
    'probabilityspec',
    'ch_av',
    'excludedcases',
    'options',
    'datasummary',
    'unimplemented_name',
]
m.xhtml(*sections)
/Users/jpn/anaconda/lib/python3.5/site-packages/larch/model_reporter/xhtml.py:489: UserWarning: xml builder for 'unimplemented_name' not found
  warnings.warn("xml builder for '{}' not found".format(key.casefold()))
Out[7]:

Model Parameter Estimates

ParameterEstimated ValueStd Errort-StatNull Value
Level of Service
InVehTime-0.1127 0.01356 -8.31 0
NonMotorTime-0.2255 = InVehTime * 2
Cost-0.3407 0.1812 -1.88 0
Alternative Specific Constants
ASC_SR-1.394 0.9541 -1.46 0
ASC_Walk 0.7318 0.3015 2.43 0
ASC_Bike-1.667 0.2739 -6.09 0
ASC_Transit-1.677 0.4977 -3.37 0
Income
HighIncSR-1.756 1.29 -1.36 0
Walk-0.7958 0.4189 -1.90 0
Bike-1.053 0.4631 -2.27 0
Transit-1.241 0.4508 -2.75 0
Logsum Parameters
NestCar 0.7417 0.5384 -0.48 1
NonMotor 0.8697 0.1835 -0.71 1
Motorized 0.8444 0.2487 -0.63 1

Model Estimation Statistics

StatisticAggregatePer Case
Number of Cases1897
Log Likelihood at Convergence-963.84-0.51
Log Likelihood at Null Parameters-2592.70-1.37
Rho Squared w.r.t. Null Parameters0.628

Nesting Structure

Tree cluster_elemental Elemental Alternatives 1 DA (1) 2 SR (2) 3 Walk (3) 4 Bike (4) 5 Transit (5) 0 Root 7 Nest:NonMotor (7) 0->7 8 Nest:Motorized (8) 0->8 7->3 7->4 8->5 6 Nest:Car (6) 8->6 6->1 6->2

Latest Estimation Run Statistics

Estimation DateSaturday, March 11 2017, 03:00:21 PM
Resultssuccess
MessageOptimization terminated successfully. [SLSQP]
Optimization MethodSLSQP
Number of Iterations42
Running TimeTotal2.437 seconds
setup0:00.12
null_likelihood0:00
weight choice rebalance0:00
weight autorescale0:00
optimize:SLSQP0:02.09
parameter covariance0:00.23
cleanup0:00
Notesdid not automatically rescale weights
Computerserenity.thunderdome
ProcessorIntel(R) Core(TM) i5-6267U CPU @ 2.90GHz
Number of CPU Cores4
Number of Threads Used4
Installed Memory16.0 GiB
Peak Memory Usage151.0625 MiB

Utility Specification

Resolved Utility

CodeAlternativeResolved Utility
1DA- 0.1127*AUTO_TIME
- 0.3407*CARCOST
2SR- 1.394
- 0.1127*AUTO_TIME
- 0.3407*CARCOST*(0.5)
- 1.756*INCOME>75000
3Walk  0.7318
- 0.2255*WALKTIME
- 0.7958*INCOME>75000
4Bike- 1.667
- 0.2255*BIKETIME
- 1.053*INCOME>75000
5Transit- 1.677
- 0.1127*RAIL_TIME
- 0.3407*RAIL_FARE
- 1.241*INCOME>75000
CodeNestResolved Utility
6Nest:Car0.7417 * log( exp(Utility[DA]/0.7417) + exp(Utility[SR]/0.7417) )
7Nest:NonMotor0.8697 * log( exp(Utility[Walk]/0.8697) + exp(Utility[Bike]/0.8697) )
8Nest:Motorized0.8444 * log( exp(Utility[Transit]/0.8444) + exp(Utility[Nest:Car]/0.8444) )
0ROOTlog( exp(Utility[Nest:Motorized]) + exp(Utility[Nest:NonMotor]) )

Formulaic Utility

CodeAlternativeFormulaic Utility
1DA  InVehTime*AUTO_TIME
+ Cost*CARCOST
2SR  ASC_SR
+ InVehTime*AUTO_TIME
+ Cost*CARCOST*(0.5)
+ HighInc:SR*INCOME>75000
3Walk  ASC_Walk
+ NonMotorTime*WALKTIME
+ HighInc:Walk*INCOME>75000
4Bike  ASC_Bike
+ NonMotorTime*BIKETIME
+ HighInc:Bike*INCOME>75000
5Transit  ASC_Transit
+ InVehTime*RAIL_TIME
+ Cost*RAIL_FARE
+ HighInc:Transit*INCOME>75000
CodeNestFormulaic Utility
6Nest:CarNest:Car * log( exp(Utility[DA]/Nest:Car) + exp(Utility[SR]/Nest:Car) )
7Nest:NonMotorNest:NonMotor * log( exp(Utility[Walk]/Nest:NonMotor) + exp(Utility[Bike]/Nest:NonMotor) )
8Nest:MotorizedNest:Motorized * log( exp(Utility[Transit]/Nest:Motorized) + exp(Utility[Nest:Car]/Nest:Motorized) )
0ROOTlog( exp(Utility[Nest:Motorized]) + exp(Utility[Nest:NonMotor]) )

Probability Specification

Resolved Probability

CodeAlternativeResolved Probability
1DAexp(Utility[DA]/0.7417)/exp(Utility[Nest:Car]/0.7417) * exp(Utility[Nest:Car]/0.8444)/exp(Utility[Nest:Motorized]/0.8444) * exp(Utility[Nest:Motorized])/exp(Utility[ROOT])
2SRexp(Utility[SR]/0.7417)/exp(Utility[Nest:Car]/0.7417) * exp(Utility[Nest:Car]/0.8444)/exp(Utility[Nest:Motorized]/0.8444) * exp(Utility[Nest:Motorized])/exp(Utility[ROOT])
3Walkexp(Utility[Walk]/0.8697)/exp(Utility[Nest:NonMotor]/0.8697) * exp(Utility[Nest:NonMotor])/exp(Utility[ROOT])
4Bikeexp(Utility[Bike]/0.8697)/exp(Utility[Nest:NonMotor]/0.8697) * exp(Utility[Nest:NonMotor])/exp(Utility[ROOT])
5Transitexp(Utility[Transit]/0.8444)/exp(Utility[Nest:Motorized]/0.8444) * exp(Utility[Nest:Motorized])/exp(Utility[ROOT])

Formulaic Probability

CodeAlternativeFormulaic Probability
1DAexp(Utility[DA]/Nest:Car)/exp(Utility[Nest:Car]/Nest:Car) * exp(Utility[Nest:Car]/Nest:Motorized)/exp(Utility[Nest:Motorized]/Nest:Motorized) * exp(Utility[Nest:Motorized])/exp(Utility[ROOT])
2SRexp(Utility[SR]/Nest:Car)/exp(Utility[Nest:Car]/Nest:Car) * exp(Utility[Nest:Car]/Nest:Motorized)/exp(Utility[Nest:Motorized]/Nest:Motorized) * exp(Utility[Nest:Motorized])/exp(Utility[ROOT])
3Walkexp(Utility[Walk]/Nest:NonMotor)/exp(Utility[Nest:NonMotor]/Nest:NonMotor) * exp(Utility[Nest:NonMotor])/exp(Utility[ROOT])
4Bikeexp(Utility[Bike]/Nest:NonMotor)/exp(Utility[Nest:NonMotor]/Nest:NonMotor) * exp(Utility[Nest:NonMotor])/exp(Utility[ROOT])
5Transitexp(Utility[Transit]/Nest:Motorized)/exp(Utility[Nest:Motorized]/Nest:Motorized) * exp(Utility[Nest:Motorized])/exp(Utility[ROOT])

Choice and Availability

CodeAlternative# Avail# ChosenAvailability Condition
1DA 1897 1573 (AGE>=16)
2SR 1897 171 1
3Walk 662 47 DIST<=3
4Bike 1882 28 DIST<=15
5Transit 1245 78 RAIL_TIME>0

Excluded Cases

CriteriaData Source# Cases Excluded# Cases Remaining
0TOURPURP != 1idco42261897

Options

authorjpn
autocreate_parametersTrue
calc_null_likelihoodTrue
calc_std_errorsTrue
enforce_boundsTrue
enforce_constraintsTrue
enforce_network_constraintsFalse
force_finite_diff_gradFalse
force_recalculateFalse
gradient_diagnostic0
hessian_diagnostic0
idca_avail_ratio_floor0.1
ignore_bad_constraintsFalse
log_turnsFalse
mute_nan_warningsTrue
null_disregards_holdfastTrue
save_db_hashFalse
suspend_xylem_rebuildFalse
teardown_after_estimateTrue
threads4
weight_autorescaleTrue
weight_choice_rebalanceTrue

Various Data Statistics

Utility idCO Data

DataMeanStd.Dev.MinimumMaximumZerosMean(NonZero)PositivesDistribution
AUTO_TIME7.71985.637249.67807.71981897
CARCOST0.977960.719280.00404373.62100.977961897
11011011897
CARCOST*(0.5)0.488980.359640.00202181.810500.488981897
INCOME>750000.426990.494640110871810
WALKTIME10.38617.018071.245123529.763662
BIKETIME23.81917.419073.8711524.0091882
RAIL_TIME4.14265.4032028.8216526.3121245
RAIL_FARE0.984450.7124101.56521.51245
Graphs are represented as pie charts if the data element has 4 or fewer distinct values.
Histograms are green if the displayed range truncates some extreme outliers.
Histograms are orange if the zeros are numerous and have been excluded.

Utility idCO Data by Alternative

AlternativeDataFilterMeanStd.Dev.MinimumMaximumMean (Nonzeros)# Zeros# PositivesDistribution
DAAUTO_TIMEChosen7.83475.6072.049.678258182948027.834701573
Unchosen7.16215.74782.033.842809900765487.16210324
CARCOSTChosen1.00370.706750.0040436794880651443.62103154879923971.003701573
Unchosen0.852770.7650.0040436794880651443.027323952332080.852770324
SRAUTO_TIMEChosen8.41955.30672.031.6927577250698438.41950171
Unchosen7.65055.6642.049.678258182948027.650501726
CARCOST*(0.5)Chosen0.554140.35060.0118274425868933231.47742213143196490.554140171
Unchosen0.482530.359880.0020218397440325721.81051577439961990.4825301726
INCOME>75000Chosen0.0818710.274170.01.0115714
Unchosen0.461180.498490.01.01930796
WalkINCOME>75000Chosen0.27660.447310.01.013413
Unchosen0.403250.490550.01.01367248
WALKTIMEChosen3.85684.46280.485241538567817223.4868402135863373.8568047
Unchosen31.74314.690.485241538567817271.2449115760592431.7430615
BikeINCOME>75000Chosen0.214290.410330.01.01226
Unchosen0.430420.495140.01.011056798
BIKETIMEChosen7.22676.81860.101091987201628625.1126805956974877.2267028
Unchosen24.26317.3440.101091987201628673.8711065715982324.26301854
TransitINCOME>75000Chosen0.179490.383760.01.016414
Unchosen0.437020.496020.01.01657510
RAIL_TIMEChosen7.16335.86760.1716016662716811628.8207959846633137.1633078
Unchosen6.25515.52230.1716016662716811628.8207959846633136.255101167
RAIL_FAREChosen1.501.51.51.5078
Unchosen1.501.51.51.501167
Graphs are represented as pie charts if the data element has 4 or fewer distinct values.
Histograms are green if the displayed range truncates some extreme outliers.
Histograms are orange if the zeros are numerous and have been excluded.
xml builder for 'unimplemented_name' not found

Put "help" to get a more helpful exception, which lists all the available sections.

In [8]:
m.xml('help')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-b62186433806> in <module>()
----> 1 m.xml('help')

/Users/jpn/anaconda/lib/python3.5/site-packages/larch/model_reporter/xhtml.py in __call__(self, force_Elem, filename, view_on_exit, return_html, *args, **kwarg)
    547                 def __call__(self, *args, force_Elem=False, filename=None, view_on_exit=False, return_html=False, **kwarg):
    548                         div = Elem('div')
--> 549                         for arg in self._model.iter_cats(args):
    550                                 if isinstance(arg, Elem):
    551                                         div << arg

/Users/jpn/anaconda/lib/python3.5/site-packages/larch/model_reporter/xhtml.py in iter_cats(self, cats)
     79                                                 poss += [k[4:]]
     80                                                 poss_incl.add(k[4:])
---> 81                                 raise TypeError('Possibilities...\n'+'\n'.join(poss))
     82
     83                         elif i=='formal':

TypeError: Possibilities...
!           ( title, possible_overspecification, params, LL, latest )
$           ( utilityspec, probabilityspec )
*           ( everything (almost) )
&           ( all registered special sections )
#BLAH BLAH  ( A level 2 header )
?REGEX      ( search for matches among registered special sections )
formal      ( title, blurb, params, LL, utilityspec, nesting_tree )
ch_av       ( summary of choice and availability by alternative, up to 50 )
ch_av_all   ( summary of choice and availability by alternative, all alts )
choice_distributions
datasummary
excludedcases
idco_variable_analysis
latest
ll
new
params
simple_parameters
single_parameter_resultpart
stats_quantity_ca
stats_quantity_ca_by_all
stats_quantity_ca_by_alt
stats_utility_ca
stats_utility_ca_by_all
stats_utility_ca_by_alt
stats_utility_co
stats_utility_co_by_alt
artparams
blurb
blurb1
blurb2
blurb3
blurb4
blurb5
blurb6
blurb7
blurb8
blurb9
computed_factors
data
data_statistics
dt_info
estimation_result
exception
failure
headnote
nesting_tree
nesting_tree_textonly
notes
options
param
parameters
possible_overspecification
probabilityspec
queryinfo
report
report_v0
title
utilitydata
utilityspec
utilityspec_ca_only