Skip to content

Commit

Permalink
worn on non-convergence, fix numeric overflow
Browse files Browse the repository at this point in the history
ported from: UDST/urbansim#211
  • Loading branch information
Eh2406 committed May 18, 2018
1 parent 5505427 commit 7d7022f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions choicemodels/mnl.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,9 @@ def mnl_probs(data, beta, numalts):
raise Exception("Number of alternatives is zero")
utilities.reshape(numalts, utilities.size() // numalts)

# https://stats.stackexchange.com/questions/304758/softmax-overflow
utilities = utilities.subtract(utilities.max(0))

exponentiated_utility = utilities.exp(inplace=True)
if clamp:
exponentiated_utility.inftoval(1e20)
Expand Down Expand Up @@ -658,6 +661,9 @@ def mnl_estimate(data, chosen, numalts, GPU=False, coeffrange=(-1000, 1000),
bounds=bounds)
logger.debug('finish: scipy optimization for MNL fit')

if bfgs_result[2]['warnflag'] > 0:
logger.warn("mnl did not converge correctly: %s", bfgs_result)

beta = bfgs_result[0]
stderr = mnl_loglik(
beta, data, chosen, numalts, weights, stderr=1, lcgrad=lcgrad)
Expand Down
6 changes: 6 additions & 0 deletions choicemodels/tools/pmat.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ def cumsum(self, axis):
# elif self.typ == 'cuda':
# return PMAT(misc.cumsum(self.mat,axis=axis))

def max(self, axis):
if self.typ == 'numpy':
return PMAT(np.max(self.mat, axis=axis))
elif self.typ == 'cuda':
return PMAT(self.mat.max(axis=axis))

def argmax(self, axis):
if self.typ == 'numpy':
return PMAT(np.argmax(self.mat, axis=axis))
Expand Down

0 comments on commit 7d7022f

Please sign in to comment.