def test_mnl_estimation(obs, alts): """ Confirm that estimated params from the new interface match urbansim.urbanchoice. Only runs if the urbansim package has been installed. """ try: from urbansim.urbanchoice.mnl import mnl_estimate except: print("Comparison of MNL estimation results skipped because urbansim is not installed") return model_expression = 'obsval + altval - 1' mct = MergedChoiceTable(obs, alts, 'choice') # new interface m = MultinomialLogit(mct, model_expression) r = m.fit().get_raw_results() # old interface dm = dmatrix(model_expression, mct.to_frame()) chosen = np.reshape(mct.to_frame()[mct.choice_col].values, (100, 5)) log_lik, fit = mnl_estimate(np.array(dm), chosen, numalts=5) for k,v in log_lik.items(): assert(v == pytest.approx(r['log_likelihood'][k], 0.00001)) assert_frame_equal(fit, r['fit_parameters'][['Coefficient', 'Std. Error', 'T-Score']])
def test_mnl_prediction(obs, alts): """ Confirm that fitted probabilities in the new codebase match urbansim.urbanchoice. Only runs if the urbansim package has been installed. """ try: from urbansim.urbanchoice.mnl import mnl_simulate except: print("Comparison of MNL simulation results skipped because urbansim is not installed") return # produce a fitted model mct = MergedChoiceTable(obs, alts, 'choice', 5) m = MultinomialLogit(mct, model_expression='obsval + altval - 1') results = m.fit() # get predicted probabilities using choicemodels probs1 = results.probabilities(mct) # compare to probabilities from urbansim.urbanchoice dm = dmatrix(results.model_expression, data=mct.to_frame(), return_type='dataframe') probs = mnl_simulate(data=dm, coeff=results.fitted_parameters, numalts=mct.sample_size, returnprobs=True) df = mct.to_frame() df['prob'] = probs.flatten() probs2 = df.prob pd.testing.assert_series_equal(probs1, probs2)
def fit(self): """ Fit the model; save and report results. This uses PyLogit via ChoiceModels. The `fit()` method can be run as many times as desired. Results will not be saved with Orca or ModelManager until the `register()` method is run. """ long_df = self._to_long(self._get_data()) # Set initial coefs to 0 if none provided pc = self._get_param_count() if (self.initial_coefs is None) or (len(self.initial_coefs) != pc): self.initial_coefs = np.zeros(pc).tolist() model = MultinomialLogit(data=long_df, observation_id_col='_obs_id', choice_col='_chosen', model_expression=self.model_expression, model_labels=self.model_labels, alternative_id_col='_alt_id', initial_coefs=self.initial_coefs) results = model.fit() self.name = self._generate_name() self.summary_table = str(results.report_fit()) print(self.summary_table) # We need the PyLogit fitted model object for prediction, so save it directly self.model = results.get_raw_results()
def test_mnl_estimation(obs, alts): """ Confirm that estimated params from the new interface match urbansim.urbanchoice. Only runs if the urbansim package has been installed. """ try: from urbansim.urbanchoice.mnl import mnl_estimate except: print( "Comparison of MNL estimation results skipped because urbansim is not installed" ) return model_expression = 'obsval + altval - 1' mct = MergedChoiceTable(obs, alts, 'choice') # new interface m = MultinomialLogit(mct, model_expression) r = m.fit().get_raw_results() # old interface dm = dmatrix(model_expression, mct.to_frame()) chosen = np.reshape(mct.to_frame()[mct.choice_col].values, (100, 5)) log_lik, fit = mnl_estimate(np.array(dm), chosen, numalts=5) for k, v in log_lik.items(): assert (v == pytest.approx(r['log_likelihood'][k], 0.00001)) assert_frame_equal( fit, r['fit_parameters'][['Coefficient', 'Std. Error', 'T-Score']])
def test_mnl(obs, alts): """ Confirm that MNL estimation runs, using the native estimator. """ model_expression = 'obsval + altval - 1' mct = MergedChoiceTable(obs, alts, 'choice') m = MultinomialLogit(mct, model_expression) print(m.fit())
def test_mnl(obs, alts): """ Confirm that MNL estimation runs, using the native estimator. """ model_expression = 'obsval + altval - 1' mct = MergedChoiceTable(obs, alts, 'choice') m = MultinomialLogit(mct, model_expression) print(m.fit())
def test_mnl_estimation(obs, alts): """ Confirm that estimated params from the new interface match urbansim.urbanchoice. """ model_expression = 'obsval + altval - 1' mct = MergedChoiceTable(obs, alts, 'choice') # new interface m = MultinomialLogit(mct, model_expression) r = m.fit().get_raw_results() # old interface dm = dmatrix(model_expression, mct.to_frame()) chosen = np.reshape(mct.to_frame()[mct.choice_col].values, (100, 5)) log_lik, fit = mnl_estimate(np.array(dm), chosen, numalts=5) for k,v in log_lik.items(): assert(v == pytest.approx(r['log_likelihood'][k], 0.00001)) assert_frame_equal(fit, r['fit_parameters'][['Coefficient', 'Std. Error', 'T-Score']])
def test_mnl_prediction(obs, alts): """ Confirm that fitted probabilities in the new codebase match urbansim.urbanchoice. Only runs if the urbansim package has been installed. """ try: from urbansim.urbanchoice.mnl import mnl_simulate except: print( "Comparison of MNL simulation results skipped because urbansim is not installed" ) return # produce a fitted model mct = MergedChoiceTable(obs, alts, 'choice', 5) m = MultinomialLogit(mct, model_expression='obsval + altval - 1') results = m.fit() # get predicted probabilities using choicemodels probs1 = results.probabilities(mct) # compare to probabilities from urbansim.urbanchoice dm = dmatrix(results.model_expression, data=mct.to_frame(), return_type='dataframe') probs = mnl_simulate(data=dm, coeff=results.fitted_parameters, numalts=mct.sample_size, returnprobs=True) df = mct.to_frame() df['prob'] = probs.flatten() probs2 = df.prob pd.testing.assert_series_equal(probs1, probs2)
def fit(self): """ Fit the model; save and report results. This uses the ChoiceModels estimation engine (originally from UrbanSim MNL). The `fit()` method can be run as many times as desired. Results will not be saved with Orca or ModelManager until the `register()` method is run. """ # TO DO - update choicemodels to accept a column name for chosen alts observations = self._get_df(tables=self.choosers, filters=self.chooser_filters) chosen = observations[self.choice_column] alternatives = self._get_df(tables=self.alternatives, filters=self.alt_filters) data = MergedChoiceTable(observations=observations, alternatives=alternatives, chosen_alternatives=chosen, sample_size=self._get_alt_sample_size()) model = MultinomialLogit(data=data.to_frame(), observation_id_col=data.observation_id_col, choice_col=data.choice_col, model_expression=self.model_expression) results = model.fit() self.name = self._generate_name() self.summary_table = str(results) print(self.summary_table) # For now, just save the summary table and fitted parameters coefs = results.get_raw_results()['fit_parameters']['Coefficient'] self.fitted_parameters = coefs.tolist()
def test_mnl_prediction(obs, alts): """ Confirm that fitted probabilities in the new codebase match urbansim.urbanchoice. """ # produce a fitted model mct = MergedChoiceTable(obs, alts, 'choice', 5) m = MultinomialLogit(mct, model_expression='obsval + altval - 1') results = m.fit() # get predicted probabilities using choicemodels probs1 = results.probabilities(mct) # compare to probabilities from urbansim.urbanchoice dm = dmatrix(results.model_expression, data=mct.to_frame(), return_type='dataframe') probs = mnl_simulate(data=dm, coeff=results.fitted_parameters, numalts=mct.sample_size, returnprobs=True) df = mct.to_frame() df['prob'] = probs.flatten() probs2 = df.prob pd.testing.assert_series_equal(probs1, probs2)
def fitted_model(obs, alts): mct = MergedChoiceTable(obs, alts, 'choice', sample_size=5) m = MultinomialLogit(mct, model_expression='obsval + altval - 1') return m.fit()
def fit(self, mct=None): """ Fit the model; save and report results. This uses the ChoiceModels estimation engine (originally from UrbanSim MNL). The `fit()` method can be run as many times as desired. Results will not be saved with Orca or ModelManager until the `register()` method is run. After sampling alternatives for each chooser, the merged choice table is saved to the class object for diagnostic use (`mergedchoicetable` with type choicemodels.tools.MergedChoiceTable). Parameters ---------- mct : choicemodels.tools.MergedChoiceTable This parameter is a temporary backdoor allowing us to pass in a more complicated choice table than can be generated within the template, for example including sampling weights or interaction terms. Returns ------- None """ check_choicemodels_version() from choicemodels import MultinomialLogit from choicemodels.tools import MergedChoiceTable if (mct is not None): df_from_mct = mct.to_frame() idx_names = df_from_mct.index.names df_from_mct = df_from_mct.reset_index() df_from_mct = apply_filter_query( df_from_mct, self.chooser_filters).set_index(idx_names) mct = MergedChoiceTable.from_df(df_from_mct) else: observations = get_data(tables=self.choosers, filters=self.chooser_filters, model_expression=self.model_expression, extra_columns=self.choice_column) if (self.chooser_sample_size is not None): observations = observations.sample(self.chooser_sample_size) alternatives = get_data(tables=self.alternatives, filters=self.alt_filters, model_expression=self.model_expression) mct = MergedChoiceTable(observations=observations, alternatives=alternatives, chosen_alternatives=self.choice_column, sample_size=self.alt_sample_size) model = MultinomialLogit(data=mct, model_expression=self.model_expression) results = model.fit() self.name = self._generate_name() self.summary_table = str(results) print(self.summary_table) coefs = results.get_raw_results()['fit_parameters']['Coefficient'] self.fitted_parameters = coefs.tolist() self.model = results # Save merged choice table to the class object for diagnostics self.mergedchoicetable = mct
def fit(self, mct=None): """ Fit the model; save and report results. This uses the ChoiceModels estimation engine (originally from UrbanSim MNL). The `fit()` method can be run as many times as desired. Results will not be saved with Orca or ModelManager until the `register()` method is run. After sampling alternatives for each chooser, the merged choice table is saved to the class object for diagnostic use (`mergedchoicetable` with type choicemodels.tools.MergedChoiceTable). Parameters ---------- mct : choicemodels.tools.MergedChoiceTable This parameter is a temporary backdoor allowing us to pass in a more complicated merged choice table than can be generated within the template, for example including sampling weights or interaction terms. This will work for model estimation, but is not yet hooked up to the prediction functionality. Returns ------- None """ if (mct is not None): data = mct else: # TO DO - update choicemodels to accept a column name for chosen alts observations = self._get_df(tables=self.choosers, filters=self.chooser_filters) if (self.chooser_sample_size is not None): observations = observations.sample(self.chooser_sample_size) chosen = observations[self.choice_column] alternatives = self._get_df(tables=self.alternatives, filters=self.alt_filters) data = MergedChoiceTable(observations=observations, alternatives=alternatives, chosen_alternatives=chosen, sample_size=self._get_alt_sample_size()) model = MultinomialLogit(data=data.to_frame(), observation_id_col=data.observation_id_col, choice_col=data.choice_col, model_expression=self.model_expression) results = model.fit() self.name = self._generate_name() self.summary_table = str(results) print(self.summary_table) # For now, just save the summary table and fitted parameters coefs = results.get_raw_results()['fit_parameters']['Coefficient'] self.fitted_parameters = coefs.tolist() # Save merged choice table to the class object for diagnostic use self.mergedchoicetable = data
pd.set_option('display.float_format', lambda x: '%.3f' % x) choosers = trips.loc[np.random.choice(trips.index, 500, replace=False)] choosers = choosers.loc[choosers.trip_distance_miles.notnull()] numalts = 10 merged = MergedChoiceTable(observations = choosers, alternatives = tracts, chosen_alternatives = choosers.full_tract_id, sample_size = numalts) model_expression = "home_density + work_density + school_density" model = MultinomialLogit(merged.to_frame(), merged.observation_id_col, merged.choice_col, model_expression) results = model.fit() results.report_fit() """ 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,
def fitted_model(obs, alts): mct = MergedChoiceTable(obs, alts, 'choice', sample_size=5) m = MultinomialLogit(mct, model_expression='obsval + altval - 1') return m.fit()