<s> import numpy as np <EOL> from statsmodels . base import model <EOL> import statsmodels . base . model as base <EOL> from statsmodels . tools . decorators import cache_readonly <EOL> from scipy . optimize import brent <EOL> from statsmodels . compat . numpy import np_matrix_rank <EOL> """<STR_LIT>""" <EOL> _predict_docstring = """<STR_LIT>""" <EOL> _predict_cov_params_docstring = """<STR_LIT>""" <EOL> class PHSurvivalTime ( object ) : <EOL> def __init__ ( self , time , status , exog , strata = None , entry = None , <EOL> offset = None ) : <EOL> """<STR_LIT>""" <EOL> if strata is None : <EOL> strata = np . zeros ( len ( time ) , dtype = np . int32 ) <EOL> if entry is None : <EOL> entry = np . zeros ( len ( time ) ) <EOL> n1 , n2 , n3 , n4 = len ( time ) , len ( status ) , len ( strata ) , len ( entry ) <EOL> nv = [ n1 , n2 , n3 , n4 ] <EOL> if max ( nv ) != min ( nv ) : <EOL> raise ValueError ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> if min ( time ) < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if min ( entry ) < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if np . any ( entry > time ) : <EOL> raise ValueError ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> stu = np . unique ( strata ) <EOL> sth = dict ( [ ( x , [ ] ) for x in stu ] ) <EOL> for i , k in enumerate ( strata ) : <EOL> sth [ k ] . append ( i ) <EOL> stratum_rows = [ np . asarray ( sth [ k ] , dtype = np . int32 ) for k in stu ] <EOL> stratum_names = stu <EOL> ix = [ i for i , ix in enumerate ( stratum_rows ) if status [ ix ] . sum ( ) > <NUM_LIT:0> ] <EOL> self . nstrat_orig = len ( stratum_rows ) <EOL> stratum_rows = [ stratum_rows [ i ] for i in ix ] <EOL> stratum_names = [ stratum_names [ i ] for i in ix ] <EOL> nstrat = len ( stratum_rows ) <EOL> self . nstrat = nstrat <EOL> for stx , ix in enumerate ( stratum_rows ) : <EOL> last_failure = max ( time [ ix ] [ status [ ix ] == <NUM_LIT:1> ] ) <EOL> ii = [ i for i , t in enumerate ( entry [ ix ] ) if <EOL> t <= last_failure ] <EOL> stratum_rows [ stx ] = stratum_rows [ stx ] [ ii ] <EOL> for stx , ix in enumerate ( stratum_rows ) : <EOL> first_failure = min ( time [ ix ] [ status [ ix ] == <NUM_LIT:1> ] ) <EOL> ii = [ i for i , t in enumerate ( time [ ix ] ) if <EOL> t >= first_failure ] <EOL> stratum_rows [ stx ] = stratum_rows [ stx ] [ ii ] <EOL> for stx , ix in enumerate ( stratum_rows ) : <EOL> ii = np . argsort ( time [ ix ] ) <EOL> stratum_rows [ stx ] = stratum_rows [ stx ] [ ii ] <EOL> if offset is not None : <EOL> self . offset_s = [ ] <EOL> for stx in range ( nstrat ) : <EOL> self . offset_s . append ( offset [ stratum_rows [ stx ] ] ) <EOL> else : <EOL> self . offset_s = None <EOL> self . n_obs = sum ( [ len ( ix ) for ix in stratum_rows ] ) <EOL> self . time_s = [ ] <EOL> self . exog_s = [ ] <EOL> self . status_s = [ ] <EOL> self . entry_s = [ ] <EOL> for ix in stratum_rows : <EOL> self . time_s . append ( time [ ix ] ) <EOL> self . exog_s . append ( exog [ ix , : ] ) <EOL> self . status_s . append ( status [ ix ] ) <EOL> self . entry_s . append ( entry [ ix ] ) <EOL> self . stratum_rows = stratum_rows <EOL> self . stratum_names = stratum_names <EOL> self . ufailt_ix , self . risk_enter , self . risk_exit , self . ufailt = [ ] , [ ] , [ ] , [ ] <EOL> for stx in range ( self . nstrat ) : <EOL> ift = np . flatnonzero ( self . status_s [ stx ] == <NUM_LIT:1> ) <EOL> ft = self . time_s [ stx ] [ ift ] <EOL> uft = np . unique ( ft ) <EOL> nuft = len ( uft ) <EOL> uft_map = dict ( [ ( x , i ) for i , x in enumerate ( uft ) ] ) <EOL> uft_ix = [ [ ] for k in range ( nuft ) ] <EOL> for ix , ti in zip ( ift , ft ) : <EOL> uft_ix [ uft_map [ ti ] ] . append ( ix ) <EOL> risk_enter1 = [ [ ] for k in range ( nuft ) ] <EOL> for i , t in enumerate ( self . time_s [ stx ] ) : <EOL> ix = np . searchsorted ( uft , t , "<STR_LIT:right>" ) - <NUM_LIT:1> <EOL> if ix >= <NUM_LIT:0> : <EOL> risk_enter1 [ ix ] . append ( i ) <EOL> risk_exit1 = [ [ ] for k in range ( nuft ) ] <EOL> for i , t in enumerate ( self . entry_s [ stx ] ) : <EOL> ix = np . searchsorted ( uft , t ) <EOL> risk_exit1 [ ix ] . append ( i ) <EOL> self . ufailt . append ( uft ) <EOL> self . ufailt_ix . append ( [ np . asarray ( x , dtype = np . int32 ) for x in uft_ix ] ) <EOL> self . risk_enter . append ( [ np . asarray ( x , dtype = np . int32 ) for x in risk_enter1 ] ) <EOL> self . risk_exit . append ( [ np . asarray ( x , dtype = np . int32 ) for x in risk_exit1 ] ) <EOL> class PHReg ( model . LikelihoodModel ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , endog , exog , status = None , entry = None , <EOL> strata = None , offset = None , ties = '<STR_LIT>' , <EOL> missing = '<STR_LIT>' , ** kwargs ) : <EOL> if status is None : <EOL> status = np . ones ( len ( endog ) ) <EOL> super ( PHReg , self ) . __init__ ( endog , exog , status = status , <EOL> entry = entry , strata = strata , <EOL> offset = offset , missing = missing , <EOL> ** kwargs ) <EOL> if self . status is not None : <EOL> self . status = np . asarray ( self . status ) <EOL> if self . entry is not None : <EOL> self . entry = np . asarray ( self . entry ) <EOL> if self . strata is not None : <EOL> self . strata = np . asarray ( self . strata ) <EOL> if self . offset is not None : <EOL> self . offset = np . asarray ( self . offset ) <EOL> self . surv = PHSurvivalTime ( self . endog , self . status , <EOL> self . exog , self . strata , <EOL> self . entry , self . offset ) <EOL> self . missing = missing <EOL> self . df_resid = ( np . float ( self . exog . shape [ <NUM_LIT:0> ] - <EOL> np_matrix_rank ( self . exog ) ) ) <EOL> self . df_model = np . float ( np_matrix_rank ( self . exog ) ) <EOL> ties = ties . lower ( ) <EOL> if ties not in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> raise ValueError ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> self . ties = ties <EOL> @ classmethod <EOL> def from_formula ( cls , formula , data , status = None , entry = None , <EOL> strata = None , offset = None , subset = None , <EOL> ties = '<STR_LIT>' , missing = '<STR_LIT>' , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( status , str ) : <EOL> status = data [ status ] <EOL> if isinstance ( entry , str ) : <EOL> entry = data [ entry ] <EOL> if isinstance ( strata , str ) : <EOL> strata = data [ strata ] <EOL> if isinstance ( offset , str ) : <EOL> offset = data [ offset ] <EOL> mod = super ( PHReg , cls ) . from_formula ( formula , data , <EOL> status = status , entry = entry , strata = strata , <EOL> offset = offset , subset = subset , ties = ties , <EOL> missing = missing , * args , ** kwargs ) <EOL> return mod <EOL> def fit ( self , groups = None , ** args ) : <EOL> """<STR_LIT>""" <EOL> if groups is not None : <EOL> if len ( groups ) != len ( self . endog ) : <EOL> msg = ( "<STR_LIT>" % <EOL> ( len ( groups ) , len ( self . endog ) ) ) <EOL> raise ValueError ( msg ) <EOL> self . groups = np . asarray ( groups ) <EOL> else : <EOL> self . groups = None <EOL> if '<STR_LIT>' not in args : <EOL> args [ '<STR_LIT>' ] = False <EOL> fit_rslts = super ( PHReg , self ) . fit ( ** args ) <EOL> if self . groups is None : <EOL> cov_params = fit_rslts . cov_params ( ) <EOL> else : <EOL> cov_params = self . robust_covariance ( fit_rslts . params ) <EOL> results = PHRegResults ( self , fit_rslts . params , cov_params ) <EOL> return results <EOL> def fit_regularized ( self , method = "<STR_LIT>" , maxiter = <NUM_LIT:100> , <EOL> alpha = <NUM_LIT:0.> , L1_wt = <NUM_LIT:1.> , start_params = None , <EOL> cnvrg_tol = <NUM_LIT> , zero_tol = <NUM_LIT> , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> k_exog = self . exog . shape [ <NUM_LIT:1> ] <EOL> n_exog = self . exog . shape [ <NUM_LIT:0> ] <EOL> if np . isscalar ( alpha ) : <EOL> alpha = alpha * np . ones ( k_exog , dtype = np . float64 ) <EOL> self . groups = None <EOL> if start_params is None : <EOL> params = np . zeros ( k_exog , dtype = np . float64 ) <EOL> else : <EOL> params = start_params . copy ( ) <EOL> import copy <EOL> surv = copy . deepcopy ( self . surv ) <EOL> if self . offset is None : <EOL> offset_s_base = [ np . zeros ( len ( x ) ) for x in surv . stratum_rows ] <EOL> surv . offset_s = [ x . copy ( ) for x in offset_s_base ] <EOL> else : <EOL> offset_s_base = [ x . copy ( ) for x in surv . offset_s ] <EOL> model_1var = copy . deepcopy ( self ) <EOL> model_1var . surv = surv <EOL> model_1var . ties = self . ties <EOL> def gen_npfuncs ( k ) : <EOL> def nploglike ( params ) : <EOL> pen = alpha [ k ] * ( ( <NUM_LIT:1> - L1_wt ) * params ** <NUM_LIT:2> / <NUM_LIT:2> + L1_wt * np . abs ( params ) ) <EOL> return - model_1var . loglike ( np . r_ [ params ] ) / n_exog + pen <EOL> def npscore ( params ) : <EOL> pen_grad = alpha [ k ] * ( <NUM_LIT:1> - L1_wt ) * params <EOL> return - model_1var . score ( np . r_ [ params ] ) [ <NUM_LIT:0> ] / n_exog + pen_grad <EOL> def nphess ( params ) : <EOL> pen_hess = alpha [ k ] * ( <NUM_LIT:1> - L1_wt ) <EOL> return - model_1var . hessian ( np . r_ [ params ] ) [ <NUM_LIT:0> , <NUM_LIT:0> ] / n_exog + pen_hess <EOL> return nploglike , npscore , nphess <EOL> nploglike_funcs = [ gen_npfuncs ( k ) for k in range ( len ( params ) ) ] <EOL> exog_s = [ ] <EOL> for k in range ( k_exog ) : <EOL> ex = [ x [ : , k ] [ : , None ] for x in surv . exog_s ] <EOL> exog_s . append ( ex ) <EOL> converged = False <EOL> btol = <NUM_LIT> <EOL> params_zero = np . zeros ( len ( params ) , dtype = bool ) <EOL> for itr in range ( maxiter ) : <EOL> params_save = params . copy ( ) <EOL> for k in range ( k_exog ) : <EOL> if params_zero [ k ] : <EOL> continue <EOL> surv . exog_s = exog_s [ k ] <EOL> params0 = params . copy ( ) <EOL> params0 [ k ] = <NUM_LIT:0> <EOL> for stx in range ( self . surv . nstrat ) : <EOL> v = np . dot ( self . surv . exog_s [ stx ] , params0 ) <EOL> surv . offset_s [ stx ] = offset_s_base [ stx ] + v <EOL> params [ k ] = _opt_1d ( nploglike_funcs [ k ] , params [ k ] , <EOL> alpha [ k ] * L1_wt , tol = btol ) <EOL> if itr > <NUM_LIT:0> and np . abs ( params [ k ] ) < zero_tol : <EOL> params_zero [ k ] = True <EOL> params [ k ] = <NUM_LIT:0.> <EOL> pchange = np . max ( np . abs ( params - params_save ) ) <EOL> if pchange < cnvrg_tol : <EOL> converged = True <EOL> break <EOL> params *= np . abs ( params ) >= zero_tol <EOL> ii = np . flatnonzero ( params ) <EOL> cov = np . zeros ( ( k_exog , k_exog ) , dtype = np . float64 ) <EOL> if len ( ii ) > <NUM_LIT:0> : <EOL> model = self . __class__ ( self . endog , self . exog [ : , ii ] , <EOL> status = self . status , entry = self . entry , <EOL> strata = self . strata , offset = self . offset , <EOL> ties = self . ties , missing = self . missing ) <EOL> rslt = model . fit ( ) <EOL> cov [ np . ix_ ( ii , ii ) ] = rslt . normalized_cov_params <EOL> rfit = PHRegResults ( self , params , cov_params = cov ) <EOL> rfit . converged = converged <EOL> rfit . regularized = True <EOL> return rfit <EOL> def loglike ( self , params ) : <EOL> """<STR_LIT>""" <EOL> if self . ties == "<STR_LIT>" : <EOL> return self . breslow_loglike ( params ) <EOL> elif self . ties == "<STR_LIT>" : <EOL> return self . efron_loglike ( params ) <EOL> def score ( self , params ) : <EOL> """<STR_LIT>""" <EOL> if self . ties == "<STR_LIT>" : <EOL> return self . breslow_gradient ( params ) <EOL> elif self . ties == "<STR_LIT>" : <EOL> return self . efron_gradient ( params ) <EOL> def hessian ( self , params ) : <EOL> """<STR_LIT>""" <EOL> if self . ties == "<STR_LIT>" : <EOL> return self . breslow_hessian ( params ) <EOL> else : <EOL> return self . efron_hessian ( params ) <EOL> def breslow_loglike ( self , params ) : <EOL> """<STR_LIT>""" <EOL> surv = self . surv <EOL> like = <NUM_LIT:0.> <EOL> for stx in range ( surv . nstrat ) : <EOL> uft_ix = surv . ufailt_ix [ stx ] <EOL> exog_s = surv . exog_s [ stx ] <EOL> nuft = len ( uft_ix ) <EOL> linpred = np . dot ( exog_s , params ) <EOL> if surv . offset_s is not None : <EOL> linpred += surv . offset_s [ stx ] <EOL> linpred -= linpred . max ( ) <EOL> e_linpred = np . exp ( linpred ) <EOL> xp0 = <NUM_LIT:0.> <EOL> for i in range ( nuft ) [ : : - <NUM_LIT:1> ] : <EOL> ix = surv . risk_enter [ stx ] [ i ] <EOL> xp0 += e_linpred [ ix ] . sum ( ) <EOL> ix = uft_ix [ i ] <EOL> like += ( linpred [ ix ] - np . log ( xp0 ) ) . sum ( ) <EOL> ix = surv . risk_exit [ stx ] [ i ] <EOL> xp0 -= e_linpred [ ix ] . sum ( ) <EOL> return like <EOL> def efron_loglike ( self , params ) : <EOL> """<STR_LIT>""" <EOL> surv = self . surv <EOL> like = <NUM_LIT:0.> <EOL> for stx in range ( surv . nstrat ) : <EOL> exog_s = surv . exog_s [ stx ] <EOL> linpred = np . dot ( exog_s , params ) <EOL> if surv . offset_s is not None : <EOL> linpred += surv . offset_s [ stx ] <EOL> linpred -= linpred . max ( ) <EOL> e_linpred = np . exp ( linpred ) <EOL> xp0 = <NUM_LIT:0.> <EOL> uft_ix = surv . ufailt_ix [ stx ] <EOL> nuft = len ( uft_ix ) <EOL> for i in range ( nuft ) [ : : - <NUM_LIT:1> ] : <EOL> ix = surv . risk_enter [ stx ] [ i ] <EOL> xp0 += e_linpred [ ix ] . sum ( ) <EOL> xp0f = e_linpred [ uft_ix [ i ] ] . sum ( ) <EOL> ix = uft_ix [ i ] <EOL> like += linpred [ ix ] . sum ( ) <EOL> m = len ( ix ) <EOL> J = np . arange ( m , dtype = np . float64 ) / m <EOL> like -= np . log ( xp0 - J * xp0f ) . sum ( ) <EOL> ix = surv . risk_exit [ stx ] [ i ] <EOL> xp0 -= e_linpred [ ix ] . sum ( ) <EOL> return like <EOL> def breslow_gradient ( self , params ) : <EOL> """<STR_LIT>""" <EOL> surv = self . surv <EOL> grad = <NUM_LIT:0.> <EOL> for stx in range ( surv . nstrat ) : <EOL> strat_ix = surv . stratum_rows [ stx ] <EOL> uft_ix = surv . ufailt_ix [ stx ] <EOL> nuft = len ( uft_ix ) <EOL> exog_s = surv . exog_s [ stx ] <EOL> linpred = np . dot ( exog_s , params ) <EOL> if surv . offset_s is not None : <EOL> linpred += surv . offset_s [ stx ] <EOL> linpred -= linpred . max ( ) <EOL> e_linpred = np . exp ( linpred ) <EOL> xp0 , xp1 = <NUM_LIT:0.> , <NUM_LIT:0.> <EOL> for i in range ( nuft ) [ : : - <NUM_LIT:1> ] : <EOL> ix = surv . risk_enter [ stx ] [ i ] <EOL> if len ( ix ) > <NUM_LIT:0> : <EOL> v = exog_s [ ix , : ] <EOL> xp0 += e_linpred [ ix ] . sum ( ) <EOL> xp1 += ( e_linpred [ ix ] [ : , None ] * v ) . sum ( <NUM_LIT:0> ) <EOL> ix = uft_ix [ i ] <EOL> grad += ( exog_s [ ix , : ] - xp1 / xp0 ) . sum ( <NUM_LIT:0> ) <EOL> ix = surv . risk_exit [ stx ] [ i ] <EOL> if len ( ix ) > <NUM_LIT:0> : <EOL> v = exog_s [ ix , : ] <EOL> xp0 -= e_linpred [ ix ] . sum ( ) <EOL> xp1 -= ( e_linpred [ ix ] [ : , None ] * v ) . sum ( <NUM_LIT:0> ) <EOL> return grad <EOL> def efron_gradient ( self , params ) : <EOL> """<STR_LIT>""" <EOL> surv = self . surv <EOL> grad = <NUM_LIT:0.> <EOL> for stx in range ( surv . nstrat ) : <EOL> strat_ix = surv . stratum_rows [ stx ] <EOL> exog_s = surv . exog_s [ stx ] <EOL> linpred = np . dot ( exog_s , params ) <EOL> if surv . offset_s is not None : <EOL> linpred += surv . offset_s [ stx ] <EOL> linpred -= linpred . max ( ) <EOL> e_linpred = np . exp ( linpred ) <EOL> xp0 , xp1 = <NUM_LIT:0.> , <NUM_LIT:0.> <EOL> uft_ix = surv . ufailt_ix [ stx ] <EOL> nuft = len ( uft_ix ) <EOL> for i in range ( nuft ) [ : : - <NUM_LIT:1> ] : <EOL> ix = surv . risk_enter [ stx ] [ i ] <EOL> if len ( ix ) > <NUM_LIT:0> : <EOL> v = exog_s [ ix , : ] <EOL> xp0 += e_linpred [ ix ] . sum ( ) <EOL> xp1 += ( e_linpred [ ix ] [ : , None ] * v ) . sum ( <NUM_LIT:0> ) <EOL> ixf = uft_ix [ i ] <EOL> if len ( ixf ) > <NUM_LIT:0> : <EOL> v = exog_s [ ixf , : ] <EOL> xp0f = e_linpred [ ixf ] . sum ( ) <EOL> xp1f = ( e_linpred [ ixf ] [ : , None ] * v ) . sum ( <NUM_LIT:0> ) <EOL> grad += v . sum ( <NUM_LIT:0> ) <EOL> m = len ( ixf ) <EOL> J = np . arange ( m , dtype = np . float64 ) / m <EOL> numer = xp1 - np . outer ( J , xp1f ) <EOL> denom = xp0 - np . outer ( J , xp0f ) <EOL> ratio = numer / denom <EOL> rsum = ratio . sum ( <NUM_LIT:0> ) <EOL> grad -= rsum <EOL> ix = surv . risk_exit [ stx ] [ i ] <EOL> if len ( ix ) > <NUM_LIT:0> : <EOL> v = exog_s [ ix , : ] <EOL> xp0 -= e_linpred [ ix ] . sum ( ) <EOL> xp1 -= ( e_linpred [ ix ] [ : , None ] * v ) . sum ( <NUM_LIT:0> ) <EOL> return grad <EOL> def breslow_hessian ( self , params ) : <EOL> """<STR_LIT>""" <EOL> surv = self . surv <EOL> hess = <NUM_LIT:0.> <EOL> for stx in range ( surv . nstrat ) : <EOL> uft_ix = surv . ufailt_ix [ stx ] <EOL> nuft = len ( uft_ix ) <EOL> exog_s = surv . exog_s [ stx ] <EOL> linpred = np . dot ( exog_s , params ) <EOL> if surv . offset_s is not None : <EOL> linpred += surv . offset_s [ stx ] <EOL> linpred -= linpred . max ( ) <EOL> e_linpred = np . exp ( linpred ) <EOL> xp0 , xp1 , xp2 = <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:0.> <EOL> for i in range ( nuft ) [ : : - <NUM_LIT:1> ] : <EOL> ix = surv . risk_enter [ stx ] [ i ] <EOL> if len ( ix ) > <NUM_LIT:0> : <EOL> xp0 += e_linpred [ ix ] . sum ( ) <EOL> v = exog_s [ ix , : ] <EOL> xp1 += ( e_linpred [ ix ] [ : , None ] * v ) . sum ( <NUM_LIT:0> ) <EOL> mat = v [ None , : , : ] <EOL> elx = e_linpred [ ix ] <EOL> xp2 += ( mat . T * mat * elx [ None , : , None ] ) . sum ( <NUM_LIT:1> ) <EOL> m = len ( uft_ix [ i ] ) <EOL> hess += m * ( xp2 / xp0 - np . outer ( xp1 , xp1 ) / xp0 ** <NUM_LIT:2> ) <EOL> ix = surv . risk_exit [ stx ] [ i ] <EOL> if len ( ix ) > <NUM_LIT:0> : <EOL> xp0 -= e_linpred [ ix ] . sum ( ) <EOL> v = exog_s [ ix , : ] <EOL> xp1 -= ( e_linpred [ ix ] [ : , None ] * v ) . sum ( <NUM_LIT:0> ) <EOL> mat = v [ None , : , : ] <EOL> elx = e_linpred [ ix ] <EOL> xp2 -= ( mat . T * mat * elx [ None , : , None ] ) . sum ( <NUM_LIT:1> ) <EOL> return - hess <EOL> def efron_hessian ( self , params ) : <EOL> """<STR_LIT>""" <EOL> surv = self . surv <EOL> hess = <NUM_LIT:0.> <EOL> for stx in range ( surv . nstrat ) : <EOL> exog_s = surv . exog_s [ stx ] <EOL> linpred = np . dot ( exog_s , params ) <EOL> if surv . offset_s is not None : <EOL> linpred += surv . offset_s [ stx ] <EOL> linpred -= linpred . max ( ) <EOL> e_linpred = np . exp ( linpred ) <EOL> xp0 , xp1 , xp2 = <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:0.> <EOL> uft_ix = surv . ufailt_ix [ stx ] <EOL> nuft = len ( uft_ix ) <EOL> for i in range ( nuft ) [ : : - <NUM_LIT:1> ] : <EOL> ix = surv . risk_enter [ stx ] [ i ] <EOL> if len ( ix ) > <NUM_LIT:0> : <EOL> xp0 += e_linpred [ ix ] . sum ( ) <EOL> v = exog_s [ ix , : ] <EOL> xp1 += ( e_linpred [ ix ] [ : , None ] * v ) . sum ( <NUM_LIT:0> ) <EOL> mat = v [ None , : , : ] <EOL> elx = e_linpred [ ix ] <EOL> xp2 += ( mat . T * mat * elx [ None , : , None ] ) . sum ( <NUM_LIT:1> ) <EOL> ixf = uft_ix [ i ] <EOL> if len ( ixf ) > <NUM_LIT:0> : <EOL> v = exog_s [ ixf , : ] <EOL> xp0f = e_linpred [ ixf ] . sum ( ) <EOL> xp1f = ( e_linpred [ ixf ] [ : , None ] * v ) . sum ( <NUM_LIT:0> ) <EOL> mat = v [ None , : , : ] <EOL> elx = e_linpred [ ixf ] <EOL> xp2f = ( mat . T * mat * elx [ None , : , None ] ) . sum ( <NUM_LIT:1> ) <EOL> m = len ( uft_ix [ i ] ) <EOL> J = np . arange ( m , dtype = np . float64 ) / m <EOL> c0 = xp0 - J * xp0f <EOL> mat = ( xp2 [ None , : , : ] - J [ : , None , None ] * xp2f ) / c0 [ : , None , None ] <EOL> hess += mat . sum ( <NUM_LIT:0> ) <EOL> mat = ( xp1 [ None , : ] - np . outer ( J , xp1f ) ) / c0 [ : , None ] <EOL> mat = mat [ : , : , None ] * mat [ : , None , : ] <EOL> hess -= mat . sum ( <NUM_LIT:0> ) <EOL> ix = surv . risk_exit [ stx ] [ i ] <EOL> if len ( ix ) > <NUM_LIT:0> : <EOL> xp0 -= e_linpred [ ix ] . sum ( ) <EOL> v = exog_s [ ix , : ] <EOL> xp1 -= ( e_linpred [ ix ] [ : , None ] * v ) . sum ( <NUM_LIT:0> ) <EOL> mat = v [ None , : , : ] <EOL> elx = e_linpred [ ix ] <EOL> xp2 -= ( mat . T * mat * elx [ None , : , None ] ) . sum ( <NUM_LIT:1> ) <EOL> return - hess <EOL> def robust_covariance ( self , params ) : <EOL> """<STR_LIT>""" <EOL> if self . groups is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> hess = self . hessian ( params ) <EOL> score_obs = self . score_residuals ( params ) <EOL> grads = { } <EOL> for i , g in enumerate ( self . groups ) : <EOL> if g not in grads : <EOL> grads [ g ] = <NUM_LIT:0.> <EOL> grads [ g ] += score_obs [ i , : ] <EOL> grads = np . asarray ( list ( grads . values ( ) ) ) <EOL> mat = grads [ None , : , : ] <EOL> mat = mat . T * mat <EOL> mat = mat . sum ( <NUM_LIT:1> ) <EOL> hess_inv = np . linalg . inv ( hess ) <EOL> cmat = np . dot ( hess_inv , np . dot ( mat , hess_inv ) ) <EOL> return cmat <EOL> def score_residuals ( self , params ) : <EOL> """<STR_LIT>""" <EOL> surv = self . surv <EOL> score_resid = np . zeros ( self . exog . shape , dtype = np . float64 ) <EOL> mask = np . zeros ( self . exog . shape [ <NUM_LIT:0> ] , dtype = np . int32 ) <EOL> w_avg = self . weighted_covariate_averages ( params ) <EOL> for stx in range ( surv . nstrat ) : <EOL> uft_ix = surv . ufailt_ix [ stx ] <EOL> exog_s = surv . exog_s [ stx ] <EOL> nuft = len ( uft_ix ) <EOL> strat_ix = surv . stratum_rows [ stx ] <EOL> xp0 = <NUM_LIT:0.> <EOL> linpred = np . dot ( exog_s , params ) <EOL> if surv . offset_s is not None : <EOL> linpred += surv . offset_s [ stx ] <EOL> linpred -= linpred . max ( ) <EOL> e_linpred = np . exp ( linpred ) <EOL> at_risk_ix = set ( [ ] ) <EOL> for i in range ( nuft ) [ : : - <NUM_LIT:1> ] : <EOL> ix = surv . risk_enter [ stx ] [ i ] <EOL> at_risk_ix |= set ( ix ) <EOL> xp0 += e_linpred [ ix ] . sum ( ) <EOL> atr_ix = list ( at_risk_ix ) <EOL> leverage = exog_s [ atr_ix , : ] - w_avg [ stx ] [ i , : ] <EOL> d = np . zeros ( exog_s . shape [ <NUM_LIT:0> ] ) <EOL> d [ uft_ix [ i ] ] = <NUM_LIT:1> <EOL> dchaz = len ( uft_ix [ i ] ) / xp0 <EOL> mrp = d [ atr_ix ] - e_linpred [ atr_ix ] * dchaz <EOL> ii = strat_ix [ atr_ix ] <EOL> score_resid [ ii , : ] += leverage * mrp [ : , None ] <EOL> mask [ ii ] = <NUM_LIT:1> <EOL> ix = surv . risk_exit [ stx ] [ i ] <EOL> at_risk_ix -= set ( ix ) <EOL> xp0 -= e_linpred [ ix ] . sum ( ) <EOL> jj = np . flatnonzero ( mask == <NUM_LIT:0> ) <EOL> if len ( jj ) > <NUM_LIT:0> : <EOL> score_resid [ jj , : ] = np . nan <EOL> return score_resid <EOL> def weighted_covariate_averages ( self , params ) : <EOL> """<STR_LIT>""" <EOL> surv = self . surv <EOL> averages = [ ] <EOL> xp0 , xp1 = <NUM_LIT:0.> , <NUM_LIT:0.> <EOL> for stx in range ( surv . nstrat ) : <EOL> uft_ix = surv . ufailt_ix [ stx ] <EOL> exog_s = surv . exog_s [ stx ] <EOL> nuft = len ( uft_ix ) <EOL> average_s = np . zeros ( ( len ( uft_ix ) , exog_s . shape [ <NUM_LIT:1> ] ) , <EOL> dtype = np . float64 ) <EOL> linpred = np . dot ( exog_s , params ) <EOL> if surv . offset_s is not None : <EOL> linpred += surv . offset_s [ stx ] <EOL> linpred -= linpred . max ( ) <EOL> e_linpred = np . exp ( linpred ) <EOL> for i in range ( nuft ) [ : : - <NUM_LIT:1> ] : <EOL> ix = surv . risk_enter [ stx ] [ i ] <EOL> xp0 += e_linpred [ ix ] . sum ( ) <EOL> xp1 += np . dot ( e_linpred [ ix ] , exog_s [ ix , : ] ) <EOL> average_s [ i , : ] = xp1 / xp0 <EOL> ix = surv . risk_exit [ stx ] [ i ] <EOL> xp0 -= e_linpred [ ix ] . sum ( ) <EOL> xp1 -= np . dot ( e_linpred [ ix ] , exog_s [ ix , : ] ) <EOL> averages . append ( average_s ) <EOL> return averages <EOL> def baseline_cumulative_hazard ( self , params ) : <EOL> """<STR_LIT>""" <EOL> surv = self . surv <EOL> rslt = [ ] <EOL> for stx in range ( surv . nstrat ) : <EOL> uft = surv . ufailt [ stx ] <EOL> uft_ix = surv . ufailt_ix [ stx ] <EOL> exog_s = surv . exog_s [ stx ] <EOL> nuft = len ( uft_ix ) <EOL> linpred = np . dot ( exog_s , params ) <EOL> if surv . offset_s is not None : <EOL> linpred += surv . offset_s [ stx ] <EOL> e_linpred = np . exp ( linpred ) <EOL> xp0 = <NUM_LIT:0.> <EOL> h0 = np . zeros ( nuft , dtype = np . float64 ) <EOL> for i in range ( nuft ) [ : : - <NUM_LIT:1> ] : <EOL> ix = surv . risk_enter [ stx ] [ i ] <EOL> xp0 += e_linpred [ ix ] . sum ( ) <EOL> ix = uft_ix [ i ] <EOL> h0 [ i ] = len ( ix ) / xp0 <EOL> ix = surv . risk_exit [ stx ] [ i ] <EOL> xp0 -= e_linpred [ ix ] . sum ( ) <EOL> cumhaz = np . cumsum ( h0 ) - h0 <EOL> current_strata_surv = np . exp ( - cumhaz ) <EOL> rslt . append ( [ uft , cumhaz , current_strata_surv ] ) <EOL> return rslt <EOL> def baseline_cumulative_hazard_function ( self , params ) : <EOL> """<STR_LIT>""" <EOL> from scipy . interpolate import interp1d <EOL> surv = self . surv <EOL> base = self . baseline_cumulative_hazard ( params ) <EOL> cumhaz_f = { } <EOL> for stx in range ( surv . nstrat ) : <EOL> time_h = base [ stx ] [ <NUM_LIT:0> ] <EOL> cumhaz = base [ stx ] [ <NUM_LIT:1> ] <EOL> time_h = np . r_ [ - np . inf , time_h , np . inf ] <EOL> cumhaz = np . r_ [ cumhaz [ <NUM_LIT:0> ] , cumhaz , cumhaz [ - <NUM_LIT:1> ] ] <EOL> func = interp1d ( time_h , cumhaz , kind = '<STR_LIT>' ) <EOL> cumhaz_f [ self . surv . stratum_names [ stx ] ] = func <EOL> return cumhaz_f <EOL> def predict ( self , params , exog = None , cov_params = None , endog = None , <EOL> strata = None , offset = None , pred_type = "<STR_LIT>" ) : <EOL> pred_type = pred_type . lower ( ) <EOL> if pred_type not in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> msg = "<STR_LIT>" % pred_type <EOL> raise ValueError ( msg ) <EOL> class bunch : <EOL> predicted_values = None <EOL> standard_errors = None <EOL> ret_val = bunch ( ) <EOL> exog_provided = True <EOL> if exog is None : <EOL> exog = self . exog <EOL> exog_provided = False <EOL> lhr = np . dot ( exog , params ) <EOL> if offset is not None : <EOL> lhr += offset <EOL> elif self . offset is not None and not exog_provided : <EOL> lhr += self . offset <EOL> if pred_type == "<STR_LIT>" : <EOL> ret_val . predicted_values = lhr <EOL> if cov_params is not None : <EOL> mat = np . dot ( exog , cov_params ) <EOL> va = ( mat * exog ) . sum ( <NUM_LIT:1> ) <EOL> ret_val . standard_errors = np . sqrt ( va ) <EOL> return ret_val <EOL> hr = np . exp ( lhr ) <EOL> if pred_type == "<STR_LIT>" : <EOL> ret_val . predicted_values = hr <EOL> return ret_val <EOL> if endog is None and exog_provided : <EOL> msg = "<STR_LIT>" <EOL> raise ValueError ( msg ) <EOL> elif endog is None and not exog_provided : <EOL> endog = self . endog <EOL> if strata is None : <EOL> if exog_provided and self . surv . nstrat > <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if self . strata is None : <EOL> strata = [ self . surv . stratum_names [ <NUM_LIT:0> ] , ] * len ( endog ) <EOL> else : <EOL> strata = self . strata <EOL> cumhaz = np . nan * np . ones ( len ( endog ) , dtype = np . float64 ) <EOL> stv = np . unique ( strata ) <EOL> bhaz = self . baseline_cumulative_hazard_function ( params ) <EOL> for stx in stv : <EOL> ix = np . flatnonzero ( strata == stx ) <EOL> func = bhaz [ stx ] <EOL> cumhaz [ ix ] = func ( endog [ ix ] ) * hr [ ix ] <EOL> if pred_type == "<STR_LIT>" : <EOL> ret_val . predicted_values = cumhaz <EOL> elif pred_type == "<STR_LIT>" : <EOL> ret_val . predicted_values = np . exp ( - cumhaz ) <EOL> return ret_val <EOL> predict . __doc__ = _predict_docstring % { '<STR_LIT>' : _predict_cov_params_docstring } <EOL> def get_distribution ( self , params ) : <EOL> """<STR_LIT>""" <EOL> from scipy . stats . distributions import rv_discrete <EOL> surv = self . surv <EOL> bhaz = self . baseline_cumulative_hazard ( params ) <EOL> pk , xk = [ ] , [ ] <EOL> for stx in range ( self . surv . nstrat ) : <EOL> exog_s = surv . exog_s [ stx ] <EOL> linpred = np . dot ( exog_s , params ) <EOL> if surv . offset_s is not None : <EOL> linpred += surv . offset_s [ stx ] <EOL> e_linpred = np . exp ( linpred ) <EOL> pts = bhaz [ stx ] [ <NUM_LIT:0> ] <EOL> ichaz = np . outer ( e_linpred , bhaz [ stx ] [ <NUM_LIT:1> ] ) <EOL> usurv = np . exp ( - ichaz ) <EOL> usurv = np . concatenate ( ( usurv , np . zeros ( ( usurv . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) ) , <EOL> axis = <NUM_LIT:1> ) <EOL> probs = - np . diff ( usurv , <NUM_LIT:1> ) <EOL> pk . append ( probs ) <EOL> xk . append ( np . outer ( np . ones ( probs . shape [ <NUM_LIT:0> ] ) , pts ) ) <EOL> mxc = max ( [ x . shape [ <NUM_LIT:1> ] for x in xk ] ) <EOL> for k in range ( self . surv . nstrat ) : <EOL> if xk [ k ] . shape [ <NUM_LIT:1> ] < mxc : <EOL> xk1 = np . zeros ( ( xk . shape [ <NUM_LIT:0> ] , mxc ) ) <EOL> pk1 = np . zeros ( ( pk . shape [ <NUM_LIT:0> ] , mxc ) ) <EOL> xk1 [ : , - mxc : ] = xk <EOL> pk1 [ : , - mxc : ] = pk <EOL> xk [ k ] , pk [ k ] = xk1 , pk1 <EOL> xka = np . nan * np . zeros ( ( len ( self . endog ) , mxc ) , dtype = np . float64 ) <EOL> pka = np . ones ( ( len ( self . endog ) , mxc ) , dtype = np . float64 ) / mxc <EOL> for stx in range ( self . surv . nstrat ) : <EOL> ix = self . surv . stratum_rows [ stx ] <EOL> xka [ ix , : ] = xk [ stx ] <EOL> pka [ ix , : ] = pk [ stx ] <EOL> dist = rv_discrete_float ( xka , pka ) <EOL> return dist <EOL> class PHRegResults ( base . LikelihoodModelResults ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , model , params , cov_params , covariance_type = "<STR_LIT>" ) : <EOL> self . covariance_type = covariance_type <EOL> self . df_resid = model . df_resid <EOL> self . df_model = model . df_model <EOL> super ( PHRegResults , self ) . __init__ ( model , params , <EOL> normalized_cov_params = cov_params ) <EOL> @ cache_readonly <EOL> def standard_errors ( self ) : <EOL> """<STR_LIT>""" <EOL> return np . sqrt ( np . diag ( self . cov_params ( ) ) ) <EOL> @ cache_readonly <EOL> def bse ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . standard_errors <EOL> def get_distribution ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . model . get_distribution ( self . params ) <EOL> def predict ( self , endog = None , exog = None , strata = None , <EOL> offset = None , transform = True , pred_type = "<STR_LIT>" ) : <EOL> return super ( PHRegResults , self ) . predict ( exog = exog , <EOL> transform = transform , <EOL> cov_params = self . cov_params ( ) , <EOL> endog = endog , <EOL> strata = strata , <EOL> offset = offset , <EOL> pred_type = pred_type ) <EOL> predict . __doc__ = _predict_docstring % { '<STR_LIT>' : '<STR_LIT>' } <EOL> def _group_stats ( self , groups ) : <EOL> """<STR_LIT>""" <EOL> gsize = { } <EOL> for x in groups : <EOL> if x not in gsize : <EOL> gsize [ x ] = <NUM_LIT:0> <EOL> gsize [ x ] += <NUM_LIT:1> <EOL> gsize = np . asarray ( list ( gsize . values ( ) ) ) <EOL> return gsize . min ( ) , gsize . max ( ) , gsize . mean ( ) , len ( gsize ) <EOL> @ cache_readonly <EOL> def weighted_covariate_averages ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . model . weighted_covariate_averages ( self . params ) <EOL> @ cache_readonly <EOL> def score_residuals ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . model . score_residuals ( self . params ) <EOL> @ cache_readonly <EOL> def baseline_cumulative_hazard ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . model . baseline_cumulative_hazard ( self . params ) <EOL> @ cache_readonly <EOL> def baseline_cumulative_hazard_function ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . model . baseline_cumulative_hazard_function ( self . params ) <EOL> @ cache_readonly <EOL> def schoenfeld_residuals ( self ) : <EOL> """<STR_LIT>""" <EOL> surv = self . model . surv <EOL> w_avg = self . weighted_covariate_averages <EOL> sch_resid = np . nan * np . ones ( self . model . exog . shape , dtype = np . float64 ) <EOL> for stx in range ( surv . nstrat ) : <EOL> uft = surv . ufailt [ stx ] <EOL> exog_s = surv . exog_s [ stx ] <EOL> time_s = surv . time_s [ stx ] <EOL> strat_ix = surv . stratum_rows [ stx ] <EOL> ii = np . searchsorted ( uft , time_s ) <EOL> jj = np . flatnonzero ( ii < len ( uft ) ) <EOL> sch_resid [ strat_ix [ jj ] , : ] = exog_s [ jj , : ] - w_avg [ stx ] [ ii [ jj ] , : ] <EOL> jj = np . flatnonzero ( self . model . status == <NUM_LIT:0> ) <EOL> sch_resid [ jj , : ] = np . nan <EOL> return sch_resid <EOL> @ cache_readonly <EOL> def martingale_residuals ( self ) : <EOL> """<STR_LIT>""" <EOL> surv = self . model . surv <EOL> mart_resid = np . nan * np . ones ( len ( self . model . endog ) , dtype = np . float64 ) <EOL> cumhaz_f_list = self . baseline_cumulative_hazard_function <EOL> for stx in range ( surv . nstrat ) : <EOL> cumhaz_f = cumhaz_f_list [ stx ] <EOL> exog_s = surv . exog_s [ stx ] <EOL> time_s = surv . time_s [ stx ] <EOL> linpred = np . dot ( exog_s , self . params ) <EOL> if surv . offset_s is not None : <EOL> linpred += surv . offset_s [ stx ] <EOL> e_linpred = np . exp ( linpred ) <EOL> ii = surv . stratum_rows [ stx ] <EOL> chaz = cumhaz_f ( time_s ) <EOL> mart_resid [ ii ] = self . model . status [ ii ] - e_linpred * chaz <EOL> return mart_resid <EOL> def summary ( self , yname = None , xname = None , title = None , alpha = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> from statsmodels . iolib import summary2 <EOL> from statsmodels . compat . collections import OrderedDict <EOL> smry = summary2 . Summary ( ) <EOL> float_format = "<STR_LIT>" <EOL> info = OrderedDict ( ) <EOL> info [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> if yname is None : <EOL> yname = self . model . endog_names <EOL> info [ "<STR_LIT>" ] = yname <EOL> info [ "<STR_LIT>" ] = self . model . ties . capitalize ( ) <EOL> info [ "<STR_LIT>" ] = str ( self . model . surv . n_obs ) <EOL> info [ "<STR_LIT>" ] = str ( int ( sum ( self . model . status ) ) ) <EOL> if self . model . groups is not None : <EOL> mn , mx , avg , num = self . _group_stats ( self . model . groups ) <EOL> info [ "<STR_LIT>" ] = "<STR_LIT>" % num <EOL> info [ "<STR_LIT>" ] = "<STR_LIT>" % mn <EOL> info [ "<STR_LIT>" ] = "<STR_LIT>" % mx <EOL> info [ "<STR_LIT>" ] = "<STR_LIT>" % avg <EOL> if self . model . strata is not None : <EOL> mn , mx , avg , num = self . _group_stats ( self . model . strata ) <EOL> info [ "<STR_LIT>" ] = "<STR_LIT>" % num <EOL> info [ "<STR_LIT>" ] = "<STR_LIT>" % mn <EOL> info [ "<STR_LIT>" ] = "<STR_LIT>" % mx <EOL> info [ "<STR_LIT>" ] = "<STR_LIT>" % avg <EOL> smry . add_dict ( info , align = '<STR_LIT:l>' , float_format = float_format ) <EOL> param = summary2 . summary_params ( self , alpha = alpha ) <EOL> param = param . rename ( columns = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } ) <EOL> param . insert ( <NUM_LIT:2> , "<STR_LIT>" , np . exp ( param [ "<STR_LIT>" ] ) ) <EOL> a = "<STR_LIT>" % ( alpha / <NUM_LIT:2> ) <EOL> param . loc [ : , a ] = np . exp ( param . loc [ : , a ] ) <EOL> a = "<STR_LIT>" % ( <NUM_LIT:1> - alpha / <NUM_LIT:2> ) <EOL> param . loc [ : , a ] = np . exp ( param . loc [ : , a ] ) <EOL> if xname != None : <EOL> param . index = xname <EOL> smry . add_df ( param , float_format = float_format ) <EOL> smry . add_title ( title = title , results = self ) <EOL> smry . add_text ( "<STR_LIT>" ) <EOL> dstrat = self . model . surv . nstrat_orig - self . model . surv . nstrat <EOL> if dstrat > <NUM_LIT:0> : <EOL> if dstrat == <NUM_LIT:1> : <EOL> smry . add_text ( "<STR_LIT>" ) <EOL> else : <EOL> smry . add_text ( "<STR_LIT>" % dstrat ) <EOL> if self . model . entry is not None : <EOL> n_entry = sum ( self . model . entry != <NUM_LIT:0> ) <EOL> if n_entry == <NUM_LIT:1> : <EOL> smry . add_text ( "<STR_LIT>" ) <EOL> else : <EOL> smry . add_text ( "<STR_LIT>" % n_entry ) <EOL> if self . model . groups is not None : <EOL> smry . add_text ( "<STR_LIT>" ) <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> smry . add_text ( "<STR_LIT>" ) <EOL> return smry <EOL> class rv_discrete_float ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , xk , pk ) : <EOL> self . xk = xk <EOL> self . pk = pk <EOL> self . cpk = np . cumsum ( self . pk , axis = <NUM_LIT:1> ) <EOL> def rvs ( self ) : <EOL> """<STR_LIT>""" <EOL> n = self . xk . shape [ <NUM_LIT:0> ] <EOL> u = np . random . uniform ( size = n ) <EOL> ix = ( self . cpk < u [ : , None ] ) . sum ( <NUM_LIT:1> ) <EOL> ii = np . arange ( n , dtype = np . int32 ) <EOL> return self . xk [ ( ii , ix ) ] <EOL> def mean ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . xk * self . pk ) . sum ( <NUM_LIT:1> ) <EOL> def var ( self ) : <EOL> """<STR_LIT>""" <EOL> mn = self . mean ( ) <EOL> xkc = self . xk - mn [ : , None ] <EOL> return ( self . pk * ( self . xk - xkc ) ** <NUM_LIT:2> ) . sum ( <NUM_LIT:1> ) <EOL> def std ( self ) : <EOL> """<STR_LIT>""" <EOL> return np . sqrt ( self . var ( ) ) <EOL> def _opt_1d ( funcs , start , L1_wt , tol ) : <EOL> """<STR_LIT>""" <EOL> x = start <EOL> f = funcs [ <NUM_LIT:0> ] ( x ) <EOL> b = funcs [ <NUM_LIT:1> ] ( x ) <EOL> c = funcs [ <NUM_LIT:2> ] ( x ) <EOL> d = b - c * x <EOL> if L1_wt > np . abs ( d ) : <EOL> return <NUM_LIT:0.> <EOL> elif d >= <NUM_LIT:0> : <EOL> x += ( L1_wt - b ) / c <EOL> elif d < <NUM_LIT:0> : <EOL> x -= ( L1_wt + b ) / c <EOL> f1 = funcs [ <NUM_LIT:0> ] ( x ) <EOL> if f1 > f + <NUM_LIT> : <EOL> return brent ( funcs [ <NUM_LIT:0> ] , brack = ( x - <NUM_LIT> , x + <NUM_LIT> ) , tol = tol ) <EOL> return x </s>
