Auto Ownership#

import logging

import traveler as tv

tv.log_to_stdout(logging.INFO)
[00:00.85] INFO: Logging to stdout set to level 20
import mtc

store = mtc.mini()
store
<Store with keys households, persons, skims, land_use, time_periods, tours>
from traveler.steps import SampleHouseholds

store = SampleHouseholds(n=100_000).run_on(store)
/Users/jpn/Git/traveler/src/traveler/steps/_sample_households.py:268: UserWarning: n (100000) is greater than the number of households in the store, returning all households.
  return self.run_via_polars(store)
store.households.to_pandas()
hhsize num_workers auto_ownership TAZ HHT income home_zone_id
household_id
2717868 2 1 1 25 1 361000 25
763899 1 0 1 6 4 59220 6
2222791 2 2 2 9 2 197000 9
112477 1 1 0 17 6 2200 17
370491 3 1 0 21 1 16500 21
... ... ... ... ... ... ... ...
109218 1 1 0 10 4 15000 10
570708 1 0 0 23 6 13100 23
2762199 1 0 0 21 0 0 21
2049372 1 1 1 18 4 103000 18
702559 2 0 0 13 1 14800 13

5000 rows × 7 columns

store.persons.to_pandas()
household_id age PNUM sex pemploy pstudent ptype _household_idx_
person_id
25671 25671 47 1 1 3 3 4 3817
25675 25675 27 1 2 3 2 3 4066
25678 25678 30 1 2 3 3 4 3498
25683 25683 23 1 1 3 3 4 4539
25684 25684 52 1 1 3 3 4 4710
... ... ... ... ... ... ... ... ...
7554848 2863513 68 1 1 3 3 5 1559
7554855 2863520 68 1 1 3 3 5 4711
7554859 2863524 93 1 2 3 3 5 1148
7554887 2863552 76 1 2 3 3 5 371
7554903 2863568 82 1 2 3 3 5 4690

8212 rows × 8 columns

store.info()
<mtc.tables.store.Store>
  households = <JaxTable with shape (5000,)>
  persons = <JaxTable with shape (8212,)>
  skims = <Skims with 21 keys>
  land_use = <JaxTable with shape (25,)>
  time_periods = {'EA': 0, 'AM': 1, 'MD': 2, 'PM': 3, 'EV': 4}
  tours = <JaxTable with shape (20000,)>
from mtc.models.annotate_households import (
    annotate_households,
    household_value_of_time,
)
from mtc.models.annotate_landuse import annotate_landuse
from mtc.models.annotate_persons import (
    annotate_persons,
)
store.households.info()
<JaxTable>
- household_id         (5000,) int32
- hhsize               (5000,) int8
- num_workers          (5000,) int8
- auto_ownership       (5000,) int8
- TAZ                  (5000,) int32
- HHT                  (5000,) int8
- income               (5000,) int32
- home_zone_id         (5000,) categorical: (25 categories)
store.persons.info()
<JaxTable>
- person_id            (8212,) int32
- household_id         (8212,) int32
- age                  (8212,) int32
- PNUM                 (8212,) int32
- sex                  (8212,) int32
- pemploy              (8212,) int32
- pstudent             (8212,) int32
- ptype                (8212,) int32
- _household_idx_      (8212,) int32
# household_value_of_time.__step__._compute_values.chunk_size = 10_000_000
# type: ignore
store = store.run_steps(
    annotate_landuse, annotate_persons, annotate_households, household_value_of_time
)
[00:02.32] INFO: compute_values step annotate_landuse completed in 0:00:00.05
[00:02.69] INFO: compute_values step annotate_persons completed in 0:00:00.36
[00:02.86] INFO: compute_values step annotate_households completed in 0:00:00.16
[00:02.94] INFO: compute_values step household_value_of_time completed in 0:00:00.07
store.households.info()
<JaxTable>
- household_id         (5000,) int32
- hhsize               (5000,) int8
- num_workers          (5000,) int8
- auto_ownership       (5000,) int8
- TAZ                  (5000,) int32
- HHT                  (5000,) int8
- income               (5000,) int32
- home_zone_id         (5000,) categorical: (25 categories)
- income_in_thousands  (5000,) float32
- income_segment       (5000,) int8
- family               (5000,) bool
- non_family           (5000,) bool
- num_drivers          (5000,) int8
- num_adults           (5000,) int8
- num_children         (5000,) int8
- num_young_children   (5000,) int8
- num_children_5_to_15 (5000,) int8
- num_children_16_to_17 (5000,) int8
- num_college_age      (5000,) int8
- num_young_adults     (5000,) int8
- home_is_urban        (5000,) bool
- home_is_rural        (5000,) bool
- median_value_of_time (5000,) float32
- random_vot           (5000,) float32
store.completed_steps
{'annotate_households',
 'annotate_landuse',
 'annotate_persons',
 'household_value_of_time'}
store.persons.info()
<JaxTable>
- person_id            (8212,) int32
- household_id         (8212,) int32
- age                  (8212,) int32
- PNUM                 (8212,) int32
- sex                  (8212,) int32
- pemploy              (8212,) int32
- pstudent             (8212,) int32
- ptype                (8212,) int32
- _household_idx_      (8212,) int32
- age_16_to_19         (8212,) bool
- age_16_p             (8212,) bool
- adult                (8212,) bool
- male                 (8212,) bool
- female               (8212,) bool
- has_non_worker       (8212,) bool
- has_retiree          (8212,) bool
- has_preschool_kid    (8212,) bool
- has_driving_kid      (8212,) bool
- has_school_kid       (8212,) bool
- has_full_time        (8212,) bool
- has_part_time        (8212,) bool
- has_university       (8212,) bool
- student_is_employed  (8212,) bool
- nonstudent_to_school (8212,) bool
- is_student           (8212,) bool
- is_gradeschool       (8212,) bool
- is_highschool        (8212,) bool
- is_university        (8212,) bool
- school_segment       (8212,) int8
- is_worker            (8212,) bool
- home_zone_id         (8212,) categorical: (25 categories)
store.households.info()
<JaxTable>
- household_id         (5000,) int32
- hhsize               (5000,) int8
- num_workers          (5000,) int8
- auto_ownership       (5000,) int8
- TAZ                  (5000,) int32
- HHT                  (5000,) int8
- income               (5000,) int32
- home_zone_id         (5000,) categorical: (25 categories)
- income_in_thousands  (5000,) float32
- income_segment       (5000,) int8
- family               (5000,) bool
- non_family           (5000,) bool
- num_drivers          (5000,) int8
- num_adults           (5000,) int8
- num_children         (5000,) int8
- num_young_children   (5000,) int8
- num_children_5_to_15 (5000,) int8
- num_children_16_to_17 (5000,) int8
- num_college_age      (5000,) int8
- num_young_adults     (5000,) int8
- home_is_urban        (5000,) bool
- home_is_rural        (5000,) bool
- median_value_of_time (5000,) float32
- random_vot           (5000,) float32
store.validate(verbose=1, coerce="safe", missing_fields="ignore")
Found 2 category groups for validation.
  TIMEPERIOD: ['EA', 'AM', 'MD', 'PM', 'EV'] (ordered=False)
  TAZ: [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 25] (ordered=False)
table households is <mtc.tables.households.Households object at 0x12c5cce50>
Validating table 'households'...
Field 'household_id' validated as <class 'numpy.int32'>.
Field 'TAZ' validated as <class 'numpy.int32'>.
Field 'HHT' validated as <class 'numpy.int8'>.
Field 'hhsize' validated as <class 'numpy.int8'>.
Field 'income' validated as <class 'numpy.int32'>.
Field 'auto_ownership' validated as <class 'numpy.int8'>.
Field 'random_vot' validated as <class 'numpy.float32'>.
Field 'income_segment' validated as <class 'numpy.int8'>.
Field 'family' validated as <class 'bool'>.
Field 'non_family' validated as <class 'bool'>.
Field 'num_drivers' validated as <class 'numpy.int8'>.
Field 'num_adults' validated as <class 'numpy.int8'>.
Field 'num_children' validated as <class 'numpy.int8'>.
Field 'num_young_children' validated as <class 'numpy.int8'>.
Field 'num_children_5_to_15' validated as <class 'numpy.int8'>.
Field 'num_children_16_to_17' validated as <class 'numpy.int8'>.
Field 'num_college_age' validated as <class 'numpy.int8'>.
Field 'num_young_adults' validated as <class 'numpy.int8'>.
Field 'num_workers' validated as <class 'numpy.int8'>.
table tours is <mtc.tables.tours.Tours object at 0x12b2be650>
Validating table 'tours'...
Field 'tour_id' validated as <class 'numpy.int32'>.
Field 'person_id' validated as <class 'numpy.int32'>.
Field 'household_id' validated as <class 'numpy.int32'>.
Field '_household_idx_' validated as <class 'numpy.int32'>.
Field '_person_idx_' validated as <class 'numpy.int32'>.
table persons is <mtc.tables.persons.Persons object at 0x12c609a90>
Validating table 'persons'...
Field 'person_id' validated as <class 'numpy.int32'>.
Field 'household_id' validated as <class 'numpy.int32'>.
Field '_household_idx_' validated as <class 'numpy.int32'>.
Field 'age' coerced to <class 'jax.numpy.int8'> without loss of information.
Field 'sex' coerced to <class 'jax.numpy.int8'> without loss of information.
Field 'ptype' coerced to <class 'jax.numpy.int8'> without loss of information.
Field 'pemploy' coerced to <class 'jax.numpy.int8'> without loss of information.
Field 'pstudent' coerced to <class 'jax.numpy.int8'> without loss of information.
Field 'adult' validated as <class 'bool'>.
Field 'male' validated as <class 'bool'>.
Field 'female' validated as <class 'bool'>.
Field 'age_16_to_19' validated as <class 'numpy.bool'>.
Field 'age_16_p' validated as <class 'numpy.bool'>.
table land_use is <mtc.tables.landuse.LandUse object at 0x12c5e8f50>
Validating table 'land_use'...
Field 'TAZ' validated as <class 'numpy.int32'>.
Field 'TOTACRE' validated as <class 'numpy.float32'>.
Field 'county_id' validated as <class 'numpy.int8'>.
Field 'TOTHH' validated as <class 'numpy.int32'>.
Field 'TOTPOP' validated as <class 'numpy.int32'>.
Field 'TOTEMP' validated as <class 'numpy.int32'>.
Field 'RESACRE' validated as <class 'numpy.float32'>.
Field 'CIACRE' validated as <class 'numpy.float32'>.
Field 'DISTRICT' validated as <class 'numpy.int8'>.
Field 'SD' validated as <class 'numpy.int8'>.
Field 'AGE0519' validated as <class 'numpy.int32'>.
Field 'RETEMPN' validated as <class 'numpy.int32'>.
Field 'FPSEMPN' validated as <class 'numpy.int32'>.
Field 'HEREMPN' validated as <class 'numpy.int32'>.
Field 'density_index' validated as <class 'numpy.float32'>.
from mtc.models.auto_ownership import auto_ownership
store2 = auto_ownership.run_on(store)
store2.households.info()
<JaxTable>
- household_id         (5000,) int32
- hhsize               (5000,) int8
- num_workers          (5000,) int8
- auto_ownership       (5000,) int8
- TAZ                  (5000,) int32
- HHT                  (5000,) int8
- income               (5000,) int32
- home_zone_id         (5000,) categorical: (25 categories)
- income_in_thousands  (5000,) float32
- income_segment       (5000,) int8
- family               (5000,) bool
- non_family           (5000,) bool
- num_drivers          (5000,) int8
- num_adults           (5000,) int8
- num_children         (5000,) int8
- num_young_children   (5000,) int8
- num_children_5_to_15 (5000,) int8
- num_children_16_to_17 (5000,) int8
- num_college_age      (5000,) int8
- num_young_adults     (5000,) int8
- home_is_urban        (5000,) bool
- home_is_rural        (5000,) bool
- median_value_of_time (5000,) float32
- random_vot           (5000,) float32
- auto_ownership_sim   (5000,) int8
store2.households.to_polars()
shape: (5_000, 25)
household_idhhsizenum_workersauto_ownershipTAZHHTincomehome_zone_idincome_in_thousandsincome_segmentfamilynon_familynum_driversnum_adultsnum_childrennum_young_childrennum_children_5_to_15num_children_16_to_17num_college_agenum_young_adultshome_is_urbanhome_is_ruralmedian_value_of_timerandom_votauto_ownership_sim
i32i8i8i8i32i8i32enumf32i8boolbooli8i8i8i8i8i8i8i8boolboolf32f32i8
2717868211251361000"TAZ-25"361.04truefalse22000000truefalse60.00.3510494
7638991016459220"TAZ-6"59.2200012falsefalse11000000falsefalse25.00.4895431
222279122292197000"TAZ-9"197.04falsefalse22000010falsefalse60.01.2294261
1124771101762200"TAZ-17"2.21falsefalse11000000falsefalse15.01.1882641
37049131021116500"TAZ-21"16.51falsefalse22101000falsefalse15.00.6401151
10921811010415000"TAZ-10"15.01falsefalse11000000falsefalse15.01.9336711
57070810023613100"TAZ-23"13.11falsefalse11000000falsefalse15.01.5321781
27621991002100"TAZ-21"0.01falsefalse11000000falsefalse15.00.6514190
2049372111184103000"TAZ-18"103.04falsefalse11000000falsefalse60.02.9953111
70255920013114800"TAZ-13"14.81falsefalse22000000falsefalse15.00.9997621
auto_ownership.simulate_choice(store)
Array([4, 1, 1, ..., 0, 1, 1], dtype=int8)
import pandas as pd

pd.DataFrame(auto_ownership.utility(store, trace=True).trace)
income_in_thousands three_drivers two_drivers
0 361.000031 False True
1 59.220001 False False
2 197.000015 False True
3 2.200000 False False
4 16.500000 False True
... ... ... ...
4995 15.000001 False False
4996 13.100000 False False
4997 0.000000 False False
4998 103.000008 False False
4999 14.800001 False True

5000 rows × 3 columns

auto_ownership.simulate_choice(store)
Array([4, 1, 1, ..., 0, 1, 1], dtype=int8)
auto_ownership.analytic_share(store)
Array([0.26307496, 0.5855463 , 0.02963491, 0.00279747, 0.11894631],      dtype=float32)
auto_ownership.analytic_share_loss(
    store,
    target_shares={
        0: 0.1,
        1: 0.3,
        2: 0.4,
        3: 0.1,
        4: 0.1,
    },
)
Array(0.25510776, dtype=float32)
g = auto_ownership.analytic_share_loss_grad(
    store,
    target_shares={
        0: 0.1,
        1: 0.3,
        2: 0.4,
        3: 0.1,
        4: 0.1,
    },
)

g
AutoOwnershipParams(
    coef_cars1_asc=Array(0.07327148, dtype=float32, weak_type=True),
    coef_cars1_asc_county=Array(0., dtype=float32, weak_type=True),
    coef_cars1_asc_marin=Array(0., dtype=float32, weak_type=True),
    coef_cars1_asc_san_francisco=Array(0., dtype=float32, weak_type=True),
    coef_cars1_auto_time_saving_per_worker=Array(0., dtype=float32, weak_type=True),
    coef_cars1_density_0_10_no_workers=Array(0.22252251, dtype=float32, weak_type=True),
    coef_cars1_density_10_up_no_workers=Array(1.7848635, dtype=float32, weak_type=True),
    coef_cars1_density_10_up_workers=Array(2.4006515, dtype=float32, weak_type=True),
    coef_cars1_drivers_2=Array(0.03263084, dtype=float32, weak_type=True),
    coef_cars1_drivers_3=Array(0.00229748, dtype=float32, weak_type=True),
    coef_cars1_drivers_4_up=Array(0.0008286, dtype=float32, weak_type=True),
    coef_cars1_hh_income_0_30k=Array(1.5066861, dtype=float32, weak_type=True),
    coef_cars1_hh_income_30_up=Array(1.8867602, dtype=float32, weak_type=True),
    coef_cars1_num_workers_clip_3=Array(0.07202546, dtype=float32, weak_type=True),
    coef_cars1_persons_16_17=Array(0.00185995, dtype=float32, weak_type=True),
    coef_cars1_persons_18_24=Array(0.01011639, dtype=float32, weak_type=True),
    coef_cars1_persons_25_34=Array(0.02964097, dtype=float32, weak_type=True),
    coef_cars1_presence_children_0_4=Array(0.00396558, dtype=float32, weak_type=True),
    coef_cars1_presence_children_5_17=Array(0.00998919, dtype=float32, weak_type=True),
    coef_cars234_asc_marin=Array(0., dtype=float32, weak_type=True),
    coef_cars234_presence_children_0_4=Array(-0.00331472, dtype=float32, weak_type=True),
    coef_cars2_asc=Array(-0.02221743, dtype=float32, weak_type=True),
    coef_cars2_asc_county=Array(0., dtype=float32, weak_type=True),
    coef_cars2_asc_san_francisco=Array(-0.02221743, dtype=float32, weak_type=True),
    coef_cars2_auto_time_saving_per_worker=Array(0., dtype=float32, weak_type=True),
    coef_cars2_density_0_10_no_workers=Array(-0.02364007, dtype=float32, weak_type=True),
    coef_cars2_density_10_up_no_workers=Array(-0.21981671, dtype=float32, weak_type=True),
    coef_cars2_density_10_up_workers=Array(-0.34944877, dtype=float32, weak_type=True),
    coef_cars2_drivers_2=Array(-0.0156795, dtype=float32, weak_type=True),
    coef_cars2_drivers_3=Array(-0.00141119, dtype=float32, weak_type=True),
    coef_cars2_drivers_4_up=Array(-0.00030825, dtype=float32, weak_type=True),
    coef_cars2_hh_income_0_30k=Array(-0.586473, dtype=float32, weak_type=True),
    coef_cars2_hh_income_30_up=Array(-0.94900423, dtype=float32, weak_type=True),
    coef_cars2_num_workers_clip_3=Array(-0.03074503, dtype=float32, weak_type=True),
    coef_cars2_persons_16_17=Array(-0.0006489, dtype=float32, weak_type=True),
    coef_cars2_persons_18_24=Array(-0.00259443, dtype=float32, weak_type=True),
    coef_cars2_persons_25_34=Array(-0.01191693, dtype=float32, weak_type=True),
    coef_cars2_presence_children_5_17=Array(-0.00367055, dtype=float32, weak_type=True),
    coef_cars34_asc_county=Array(0., dtype=float32, weak_type=True),
    coef_cars34_asc_san_francisco=Array(0., dtype=float32, weak_type=True),
    coef_cars34_density_0_10_no_workers=Array(-0.00011447, dtype=float32, weak_type=True),
    coef_cars34_density_10_up_no_workers=Array(-7.9052865e-05, dtype=float32, weak_type=True),
    coef_cars34_persons_16_17=Array(-0.00098679, dtype=float32, weak_type=True),
    coef_cars34_persons_18_24=Array(-0.00365545, dtype=float32, weak_type=True),
    coef_cars34_persons_25_34=Array(-0.01036393, dtype=float32, weak_type=True),
    coef_cars34_presence_children_5_17=Array(-0.00444413, dtype=float32, weak_type=True),
    coef_cars3_asc=Array(0.00015584, dtype=float32, weak_type=True),
    coef_cars3_auto_time_saving_per_worker=Array(0., dtype=float32, weak_type=True),
    coef_cars3_drivers_2=Array(0.00024149, dtype=float32, weak_type=True),
    coef_cars3_drivers_3=Array(5.5594464e-05, dtype=float32, weak_type=True),
    coef_cars3_drivers_4_up=Array(-2.7979536e-07, dtype=float32, weak_type=True),
    coef_cars3_hh_income_0_30k=Array(0.0056824, dtype=float32, weak_type=True),
    coef_cars3_hh_income_30_up=Array(0.01130291, dtype=float32, weak_type=True),
    coef_cars3_num_workers_clip_3=Array(0.00050934, dtype=float32, weak_type=True),
    coef_cars4_asc=Array(-0.01873321, dtype=float32, weak_type=True),
    coef_cars4_auto_time_saving_per_worker=Array(0., dtype=float32, weak_type=True),
    coef_cars4_drivers_2=Array(-0.01417157, dtype=float32, weak_type=True),
    coef_cars4_drivers_3=Array(-0.00117014, dtype=float32, weak_type=True),
    coef_cars4_drivers_4_up=Array(-0.00054256, dtype=float32, weak_type=True),
    coef_cars4_hh_income_0_30k=Array(-0.50299746, dtype=float32, weak_type=True),
    coef_cars4_hh_income_30_up=Array(-0.76541436, dtype=float32, weak_type=True),
    coef_cars4_num_workers_clip_3=Array(-0.0266102, dtype=float32, weak_type=True),
    coef_retail_auto_no_workers=Array(0., dtype=float32, weak_type=True),
    coef_retail_auto_workers=Array(0., dtype=float32, weak_type=True),
    coef_retail_non_motor=Array(0., dtype=float32, weak_type=True),
    coef_retail_transit_no_workers=Array(0., dtype=float32, weak_type=True),
    coef_retail_transit_workers=Array(0., dtype=float32, weak_type=True),
)
auto_ownership.calibrate_analytic_share(
    store,
    target_shares={
        0: 0.1,
        1: 0.3,
        2: 0.4,
        3: 0.1,
        4: 0.1,
    },
    calib_params=[
        "coef_cars1_asc",
        "coef_cars2_asc",
        "coef_cars3_asc",
        "coef_cars4_asc",
    ],
)
  message: Optimization terminated successfully.
  success: True
   status: 0
      fun: 0.012498477473855019
        x: [ 1.679e+00  6.343e+00 -3.334e+00 -2.961e+00]
      nit: 17
      jac: [ 1.570e-06 -3.571e-06 -1.522e-06  5.138e-07]
 hess_inv: [[ 4.255e+01  4.628e+01 -7.460e-01  5.284e+01]
            [ 4.628e+01  9.476e+01 -1.335e+00  7.839e+01]
            [-7.460e-01 -1.335e+00  1.021e+00 -1.567e+00]
            [ 5.284e+01  7.839e+01 -1.567e+00  1.718e+02]]
     nfev: 24
     njev: 24
   shares: [ 1.250e-01  3.250e-01  4.250e-01  6.097e-06  1.250e-01]
auto_ownership.analytic_share(
    store,
)
Array([1.2501623e-01, 3.2500395e-01, 4.2497578e-01, 6.0965403e-06,
       1.2499795e-01], dtype=float32)