Skip to content

Commit

Permalink
switch to L-BFGS-B, print log-likelihood to stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Bernhardsson committed Sep 7, 2018
1 parent b5f7ef7 commit f45b0ab
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions convoys/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
'GeneralizedGamma']


def generalized_gamma_LL(x, X, B, T, W, fix_k, fix_p, hierarchical):
def generalized_gamma_LL(x, X, B, T, W, fix_k, fix_p,
hierarchical, callback=None):
k = exp(x[0]) if fix_k is None else fix_k
p = exp(x[1]) if fix_p is None else fix_p
log_sigma_alpha = x[2]
Expand Down Expand Up @@ -55,6 +56,8 @@ def generalized_gamma_LL(x, X, B, T, W, fix_k, fix_p, hierarchical):

if isnan(LL):
return -numpy.inf
if callback is not None:
callback(LL)
return LL


Expand Down Expand Up @@ -179,18 +182,18 @@ def fit(self, X, B, T, W=None, fix_k=None, fix_p=None):
# Callback for progress to stdout
sys.stdout.write('\n')

def callback(x, x_history=[]):
x_history.append(x)
sys.stdout.write('Finding MAP: %13d\r' % len(x_history))
def callback(LL, value_history=[]):
value_history.append(LL)
sys.stdout.write('Finding MAP: %13d (%18.6e)\r' %
(len(value_history), value_history[-1]))
sys.stdout.flush()

# Find the maximum a posteriori of the distribution
res = scipy.optimize.minimize(
lambda x: -generalized_gamma_LL(x, *args),
lambda x: -generalized_gamma_LL(x, *args, callback=callback),
x0,
jac=autograd.grad(lambda x: -generalized_gamma_LL(x, *args)),
method='CG',
callback=callback,
method='L-BFGS-B',
)
sys.stdout.write('\n')
result = {'map': res.x}
Expand Down

0 comments on commit f45b0ab

Please sign in to comment.