#NEWLINE# def iterative_fit( self , maxiter = 3 ) : #NEWLINE# #INDENT# '\n        Perform an iterative two-step procedure to estimate a WLS model.\n\n        The model is assumed to have heteroscedastic errors.\n        The variance is estimated by OLS regression of the link transformed\n        squared residuals on Z, i.e.::\n\n           link(sigma_i) = x_i*gamma.\n\n        Parameters\n        ----------\n        maxiter : integer, optional\n            the number of iterations\n\n        Notes\n        -----\n        maxiter=1: returns the estimated based on given weights\n        maxiter=2: performs a second estimation with the updated weights,\n                   this is 2-step estimation\n        maxiter>2: iteratively estimate and update the weights\n\n        TODO: possible extension stop iteration if change in parameter\n            estimates is smaller than x_tol\n\n        Repeated calls to fit_iterative, will do one redundant pinv_wexog\n        calculation. Calling fit_iterative(maxiter) ones does not do any\n        redundant recalculations (whitening or calculating pinv_wexog).\n\n        ' #NEWLINE# import collections #NEWLINE# self . history = collections . defaultdict ( list ) #NEWLINE# res_resid = None #NEWLINE# for i in range ( maxiter ) : #NEWLINE# #INDENT# if hasattr ( self , 'pinv_wexog' ) : #NEWLINE# #INDENT# del self . pinv_wexog #NEWLINE# #UNINDENT# results = self . fit ( ) #NEWLINE# self . history [ 'self_params' ] . append ( results . params ) #NEWLINE# if ( not ( i == ( res_resid - 1 ) ) ) : #NEWLINE# #INDENT# self . results_old = results #NEWLINE# res_resid = OLS ( self . link ( ( results . resid ** 2 ) ) , self . exog_var ) . fit ( ) #NEWLINE# self . history [ 'ols_params' ] . append ( res_resid . params ) #NEWLINE# self . weights = ( 1.0 / self . linkinv ( res_resid . fittedvalues ) ) #NEWLINE# self . weights /= self . weights . max ( ) #NEWLINE# self . weights [ ( self . weights < 1e-14 ) ] = 1e-14 #NEWLINE# self . initialize ( ) #NEWLINE# #UNINDENT# #UNINDENT# results . _results . results_residual_regression = res_resid #NEWLINE# return results