Skip to content

ENH/BUG: GLM fit_irls does not have a method option #4001

Closed

Description

@josef-pkt

edit
Currently lstsq and pinv have the same behavior, therefore no backwards incompatible change. Trying an example with near collinearity around the threshold, both lstsq and pinv show exactly the same cross-over between regularized and unregularized solution.

Adding the method argument is therefore an ENH not BUG
<end edit>

also breaks backwards compatibility in corner cases

related #3994 is changing rcond for lstsq which is now the default for irls

wls_results = reg_tools._MinimalWLS(wlsendog, wlsexog, self.weights).fit(method='lstsq')

similarly, RLM doesn't have an option but uses the default which is 'pinv' the same as WLS

wls_results = reg_tools._MinimalWLS(self.endog, self.exog,
                                                weights=self.weights).fit()

I'm using example from #3257 with offset which raises exception SVD not converged in lstsq, and I cannot switch back to "pinv" to compare
LinAlgError: SVD did not converge in Linear Least Squares

Activity

  1. added this to the 0.9 milestone on Oct 9, 2017
  2. josef-pkt commented on Oct 9, 2017

    @josef-pkt
    MemberAuthor
    import statsmodels.api as sm
    import statsmodels.formula.api as smf
    data = sm.datasets.fair.load_pandas()
    data.data['Exposure'] = 1
    data.data['Exposure'][1000:2000] = .5
    data.data['Exposure'][2000:4000] = .2
    data.data['Exposure'][4000:] = 0.1
    #model = smf.glm('affairs ~ age + rate_marriage', data=data.data, exposure=data.data['Exposure'],
    model = smf.glm('affairs ~ age + rate_marriage', data=data.data, offset=np.log(data.data['Exposure']),
                    family=sm.families.Tweedie(link=sm.families.links.Power(1), var_power=1.5))
    x = model.fit()
    x2 = model.fit(method='lbfgs')
    x3 = model.fit(method='newton')
    
    ---------------------------------------------------------------------------
    LinAlgError                               Traceback (most recent call last)
    <ipython-input-59-7b194b05aaa5> in <module>()
          9 model = smf.glm('affairs ~ age + rate_marriage', data=data.data, offset=np.log(data.data['Exposure']),
         10                 family=sm.families.Tweedie(link=sm.families.links.Power(1), var_power=1.5))
    ---> 11 x = model.fit()
         12 x2 = model.fit(method='lbfgs')
         13 x3 = model.fit(method='newton')
    
    m:\...\statsmodels\genmod\generalized_linear_model.py in fit(self, start_params, maxiter, method, tol, scale, cov_type, cov_kwds, use_t, full_output, disp, max_start_irls, **kwargs)
        995             return self._fit_irls(start_params=start_params, maxiter=maxiter,
        996                                   tol=tol, scale=scale, cov_type=cov_type,
    --> 997                                   cov_kwds=cov_kwds, use_t=use_t, **kwargs)
        998         else:
        999             self._optim_hessian = kwargs.get('optim_hessian')
    
    m:\...\statsmodels\genmod\generalized_linear_model.py in _fit_irls(self, start_params, maxiter, tol, scale, cov_type, cov_kwds, use_t, **kwargs)
       1100             wlsendog = (lin_pred + self.family.link.deriv(mu) * (self.endog-mu)
       1101                         - self._offset_exposure)
    -> 1102             wls_results = reg_tools._MinimalWLS(wlsendog, wlsexog, self.weights).fit(method='lstsq')
       1103             lin_pred = np.dot(self.exog, wls_results.params) + self._offset_exposure
       1104             mu = self.family.fitted(lin_pred)
    
    m:\...\statsmodels\regression\_tools.py in fit(self, method)
         90             params = np.linalg.solve(R, np.dot(Q.T, self.wendog))
         91         else:
    ---> 92             params, _, _, _ = np.linalg.lstsq(self.wexog, self.wendog)
         93 
         94         fitted_values = self.exog.dot(params)
    
    C:\...\python-3.4.4.amd64\lib\site-packages\numpy\linalg\linalg.py in lstsq(a, b, rcond)
       1919                                  0, work, lwork, iwork, 0)
       1920     if results['info'] > 0:
    -> 1921         raise LinAlgError('SVD did not converge in Linear Least Squares')
       1922     resids = array([], result_real_t)
       1923     if is_1d:
    
    LinAlgError: SVD did not converge in Linear Least Squares
    
  3. josef-pkt commented on Oct 10, 2017

    @josef-pkt
    MemberAuthor

    suggested name for irls keyword kwarg option wls_method
    maybe misleading,
    This is similar to EIM option in the fit_gradient method in that it is only for the optimization. The current irls in GLM calls WLS at the end independently of the MinimalWLS method used in the irls loop.

    (I just checked, all current MinimalWLS methods use the decomposition of exog, and none uses the product matrix directly x.T dot x)

    possible change: make wls_method also apply to the final WLS call (which does not yet have lstsq as option).
    Another option would be to add rcond, which we don't expose anywhere, AFAIK. (But we can leave that open until we know how to resolve #3994 and #2628)

    If difference pinv and lstsq is minor (they essentially use the same general SVD based approach, AFAIR), then this is a minor issue. (but we still need method option for testing)

    minor aside: scale is not recomputed after the final WLS call

  4. changed the title [-]BUG: GLM fit_irls does not have a method option[/-] [+]ENH/BUG: GLM fit_irls does not have a method option[/+] on Oct 10, 2017
  5. added a commit that references this issue on Oct 11, 2017
    6b631df
  6. added a commit that references this issue on Oct 25, 2017
    8ee804b
  7. added a commit that references this issue on Nov 4, 2017
    f4dc094
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions