In [1]:
import numpy as np
import pandas as pd
from patsy import dmatrix
from urbansim.urbanchoice import interaction, mnl
from choicemodels import MultinomialLogit
In [2]:
# Suppress deprecation warnings
import warnings; warnings.simplefilter('ignore')
In [ ]:
In [3]:
# Suppress scientific notation in the Pandas display output
pd.set_option('display.float_format', lambda x: '%.3f' % x)
In [4]:
tracts = pd.read_csv('../data/tracts.csv').set_index('full_tract_id')
print(tracts.shape[0])
print(tracts.head())
In [5]:
trips = pd.read_csv('../data/trips.csv').set_index('place_id')
print(trips.shape[0])
print(trips.head())
In [ ]:
In [6]:
# - each trip is a realized choice of a particular census tract
# - we can randomly sample alternative census tracts and build a model
# of destination choice
In [7]:
# `interaction.mnl_interaction_dataset()` is not documented very well, but
# this is how it seems to work
# Takes following input:
# - choosers: pandas.DataFrame with unique index
# - alternatives: pandas.DataFrame with unique index
# - SAMPLE_SIZE: number of alternatives for each choice scenario
# - chosenalts: list containing the alternative id chosen by each chooser?
# Returns following output:
# - full list of alternatives that were sampled
# - long-format DataFrame merging the two tables
# - numchoosers X SAMPLE_SIZE matrix representing chosen alternatives
In [6]:
choosers = trips.loc[np.random.choice(trips.index, 500, replace=False)]
choosers = choosers.loc[choosers.trip_distance_miles.notnull()]
print(choosers.shape[0])
print(choosers.head())
In [7]:
numalts = 100
_, merged, chosen = interaction.mnl_interaction_dataset(
choosers=choosers, alternatives=tracts, SAMPLE_SIZE=numalts,
chosenalts=choosers.full_tract_id)
print(merged.shape[0])
print(chosen.shape)
In [8]:
model_expression = "home_density + work_density + school_density"
model_design = dmatrix(model_expression, data=merged, return_type='dataframe')
print(model_design.head())
In [9]:
log_likelihoods, fit_parameters = mnl.mnl_estimate(
model_design.as_matrix(), chosen, numalts=numalts)
print(log_likelihoods)
print(fit_parameters)
In [ ]:
In [10]:
from choicemodels import MultinomialLogit
from choicemodels.tools import MergedChoiceTable
In [11]:
# Start with the same sample of trips
print(choosers.shape[0])
In [12]:
merged = MergedChoiceTable(observations = choosers,
alternatives = tracts,
chosen_alternatives = choosers.full_tract_id,
sample_size = numalts)
print(type(merged))
print(merged.to_frame().shape[0])
In [17]:
%%time
model_expression = "home_density + work_density + school_density - 1"
model = MultinomialLogit(data = merged.to_frame(),
observation_id_col = merged.observation_id_col,
choice_col = merged.choice_col,
model_expression = model_expression)
results = model.fit()
print(results)
In [14]:
print(type(results))
In [15]:
from collections import OrderedDict
In [16]:
%%time
model_expression = OrderedDict([('home_density', 'all_same'),
('work_density', 'all_same'),
('school_density', 'all_same')])
model = MultinomialLogit(data = merged.to_frame(),
observation_id_col = merged.observation_id_col,
alternative_id_col = merged.alternative_id_col,
choice_col = merged.choice_col,
model_expression = model_expression)
results = model.fit()
print(results)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: