Skip to content

Commit

Permalink
Add .report_fit method to LocationChoiceModel.
Browse files Browse the repository at this point in the history
  • Loading branch information
jiffyclub committed Apr 18, 2014
1 parent f3c472c commit 0a18f61
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion urbansim/models/lcm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from __future__ import print_function

import numpy as np
from patsy import dmatrix
from prettytable import PrettyTable

from . import util
from ..urbanchoice import interaction, mnl
Expand Down Expand Up @@ -35,6 +38,10 @@ def __init__(self, alts_fit_filters, alts_predict_filters,
self.sample_size = sample_size
self.name = name or 'LocationChoiceModel'

self._log_lks = None
self._model_columns = None
self.fit_results = None

def fit(self, choosers, alternatives, current_choice):
"""
Fit and save model parameters based on given data.
Expand All @@ -61,12 +68,36 @@ def fit(self, choosers, alternatives, current_choice):
Log-liklihood ratio
"""
alternatives = util.apply_filter_query(self.alts_fit_filters)
alternatives = util.apply_filter_query(
alternatives, self.alts_fit_filters)
_, merged, chosen = interaction.mnl_interaction_dataset(
choosers, alternatives, self.sample_size, current_choice)
model_design = dmatrix(
self.model_expression, data=merged, return_type='dataframe')
self._model_columns = model_design.columns
fit, results = mnl.mnl_estimate(
model_design.as_matrix(), chosen, self.sample_size)
self._log_lks = fit
self.fit_results = results
return fit

def report_fit(self):
"""
Print a report of the fit results.
"""
if not self.fit_results:
print('Model not yet fit.')
return

print('Null Log-liklihood: {}'.format(self._log_lks[0]))
print('Log-liklihood at convergence: {}'.format(self._log_lks[1]))
print('Log-liklihood Ratio: {}\n'.format(self._log_lks[2]))

tbl = PrettyTable(
['Component', 'Coefficient', 'Std. Error', 'T-Score'])
tbl.align['Component'] = 'l'
for c, x in zip(self._model_columns, self.fit_results):
tbl.add_row((c,) + x)

print(tbl)

0 comments on commit 0a18f61

Please sign in to comment.