#NEWLINE# def estimate_u( sumLogPi = None , nDoc = 0 , gamma = 1.0 , alpha0 = 1.0 , initu = None , approx_grad = False , factr = 10000000.0 , **kwargs ) : #NEWLINE# #INDENT# ' Run gradient optimization to estimate best v for specified problem\n\n      Returns\n      -------- \n      vhat : K-vector of values, 0 < v < 1\n      fofvhat: objective function value at vhat\n      Info : dict with info about estimation algorithm\n\n      Raises\n      --------\n      ValueError on an overflow, any detection of NaN, or failure to converge\n  ' #NEWLINE# sumLogPi = np . squeeze ( np . asarray ( sumLogPi , dtype = np . float64 ) ) #NEWLINE# assert ( sumLogPi . ndim == 1 ) #NEWLINE# K = ( sumLogPi . size - 1 ) #NEWLINE# if ( initu is None ) : #NEWLINE# #INDENT# initu = np . hstack ( [ np . ones ( Info ) , ( alpha0 * np . ones ( K ) ) ] ) #NEWLINE# #UNINDENT# assert ( initu . size == ( 2 * K ) ) #NEWLINE# initc = np . log ( initu ) #NEWLINE# myFunc = ( lambda c : objFunc_c ( c , sumLogPi , nDoc , gamma , alpha0 ) ) #NEWLINE# myGrad = ( lambda c : objGrad_c ( c , sumLogPi , nDoc , gamma , alpha0 ) ) #NEWLINE# with warnings . catch_warnings ( ) : #NEWLINE# #INDENT# warnings . filterwarnings ( 'error' , category = RuntimeWarning , message = 'overflow' ) #NEWLINE# try : #NEWLINE# #INDENT# ( chat , fhat , Info ) = scipy . optimize . fmin_l_bfgs_b ( myFunc , initc , fprime = myGrad , disp = None , approx_grad = approx_grad , factr = factr , ** kwargs ) #NEWLINE# #UNINDENT# except RuntimeWarning : #NEWLINE# #INDENT# raise ValueError ( 'FAILURE: overflow!' ) #NEWLINE# #UNINDENT# except AssertionError : #NEWLINE# #INDENT# raise ValueError ( 'FAILURE: NaN found!' ) #NEWLINE# #UNINDENT# #UNINDENT# if ( Info [ 'warnflag' ] > 1 ) : #NEWLINE# #INDENT# raise ValueError ( ( 'FAILURE: ' + Info [ 'task' ] ) ) #NEWLINE# #UNINDENT# uhat = np . exp ( chat ) #NEWLINE# Info [ 'initu' ] = initu #NEWLINE# Info [ 'objFunc' ] = ( lambda u : objFunc_u ( u , sumLogPi , nDoc , gamma , alpha0 ) ) #NEWLINE# Info [ 'gradFunc' ] = ( lambda u : objGrad_u ( u , sumLogPi , nDoc , gamma , alpha0 ) ) #NEWLINE# return ( uhat , fhat , Info )