Repository: statsmodels/statsmodels

Issue:
REF: catch convergence warning for initial optimization in discrete/count models
count models use a preliminary estimate, e.g. Poisson, to get better start_params for fitting more complex models like ZIP.

It is confusing to see the convergence warning for the initial estimate when the final estimate converged.

I think currently only count models are affected by this.
(GLM has a start_irls, but, IIRC, irls never issues a convergence warning.)


Discussion:

Resolving patch:
diff --git a/statsmodels/discrete/discrete_model.py b/statsmodels/discrete/discrete_model.py
--- a/statsmodels/discrete/discrete_model.py
+++ b/statsmodels/discrete/discrete_model.py
@@ -1504,7 +1504,9 @@ def fit(self, start_params=None, method='bfgs', maxiter=35,
                                  'warn_convergence': False}
             optim_kwds_prelim.update(kwargs.get('optim_kwds_prelim', {}))
             mod_poi = Poisson(self.endog, self.exog, offset=offset)
-            res_poi = mod_poi.fit(**optim_kwds_prelim)
+            with warnings.catch_warnings():
+                warnings.simplefilter("always")
+                res_poi = mod_poi.fit(**optim_kwds_prelim)
             start_params = res_poi.params
             a = self._estimate_dispersion(res_poi.predict(), res_poi.resid,
                                           df_resid=res_poi.df_resid)
@@ -1556,11 +1558,14 @@ def fit_regularized(self, start_params=None, method='l1',
             if np.size(offset) == 1 and offset == 0:
                 offset = None
             mod_poi = Poisson(self.endog, self.exog, offset=offset)
-            start_params = mod_poi.fit_regularized(
-                start_params=start_params, method=method, maxiter=maxiter,
-                full_output=full_output, disp=0, callback=callback,
-                alpha=alpha_p, trim_mode=trim_mode, auto_trim_tol=auto_trim_tol,
-                size_trim_tol=size_trim_tol, qc_tol=qc_tol, **kwargs).params
+            with warnings.catch_warnings():
+                warnings.simplefilter("always")
+                start_params = mod_poi.fit_regularized(
+                    start_params=start_params, method=method, maxiter=maxiter,
+                    full_output=full_output, disp=0, callback=callback,
+                    alpha=alpha_p, trim_mode=trim_mode,
+                    auto_trim_tol=auto_trim_tol, size_trim_tol=size_trim_tol,
+                    qc_tol=qc_tol, **kwargs).params
             start_params = np.append(start_params, 0.1)
 
         cntfit = super(CountModel, self).fit_regularized(
@@ -2908,7 +2913,9 @@ def fit(self, start_params=None, method='bfgs', maxiter=35,
                                  'warn_convergence': False}
             optim_kwds_prelim.update(kwargs.get('optim_kwds_prelim', {}))
             mod_poi = Poisson(self.endog, self.exog, offset=offset)
-            res_poi = mod_poi.fit(**optim_kwds_prelim)
+            with warnings.catch_warnings():
+                warnings.simplefilter("always")
+                res_poi = mod_poi.fit(**optim_kwds_prelim)
             start_params = res_poi.params
             if self.loglike_method.startswith('nb'):
                 a = self._estimate_dispersion(res_poi.predict(), res_poi.resid,
@@ -2973,11 +2980,14 @@ def fit_regularized(self, start_params=None, method='l1',
             if np.size(offset) == 1 and offset == 0:
                 offset = None
             mod_poi = Poisson(self.endog, self.exog, offset=offset)
-            start_params = mod_poi.fit_regularized(
-                start_params=start_params, method=method, maxiter=maxiter,
-                full_output=full_output, disp=0, callback=callback,
-                alpha=alpha_p, trim_mode=trim_mode, auto_trim_tol=auto_trim_tol,
-                size_trim_tol=size_trim_tol, qc_tol=qc_tol, **kwargs).params
+            with warnings.catch_warnings():
+                warnings.simplefilter("always")
+                start_params = mod_poi.fit_regularized(
+                    start_params=start_params, method=method, maxiter=maxiter,
+                    full_output=full_output, disp=0, callback=callback,
+                    alpha=alpha_p, trim_mode=trim_mode,
+                    auto_trim_tol=auto_trim_tol, size_trim_tol=size_trim_tol,
+                    qc_tol=qc_tol, **kwargs).params
             if self.loglike_method.startswith('nb'):
                 start_params = np.append(start_params, 0.1)
 
@@ -3284,7 +3294,9 @@ def fit(self, start_params=None, method='bfgs', maxiter=35,
                                  'warn_convergence': False}
             optim_kwds_prelim.update(kwargs.get('optim_kwds_prelim', {}))
             mod_poi = Poisson(self.endog, self.exog, offset=offset)
-            res_poi = mod_poi.fit(**optim_kwds_prelim)
+            with warnings.catch_warnings():
+                warnings.simplefilter("always")
+                res_poi = mod_poi.fit(**optim_kwds_prelim)
             start_params = res_poi.params
             a = self._estimate_dispersion(res_poi.predict(), res_poi.resid,
                                           df_resid=res_poi.df_resid)
@@ -3333,11 +3345,14 @@ def fit_regularized(self, start_params=None, method='l1',
             if np.size(offset) == 1 and offset == 0:
                 offset = None
             mod_poi = Poisson(self.endog, self.exog, offset=offset)
-            start_params = mod_poi.fit_regularized(
-                start_params=start_params, method=method, maxiter=maxiter,
-                full_output=full_output, disp=0, callback=callback,
-                alpha=alpha_p, trim_mode=trim_mode, auto_trim_tol=auto_trim_tol,
-                size_trim_tol=size_trim_tol, qc_tol=qc_tol, **kwargs).params
+            with warnings.catch_warnings():
+                warnings.simplefilter("always")
+                start_params = mod_poi.fit_regularized(
+                    start_params=start_params, method=method, maxiter=maxiter,
+                    full_output=full_output, disp=0, callback=callback,
+                    alpha=alpha_p, trim_mode=trim_mode,
+                    auto_trim_tol=auto_trim_tol, size_trim_tol=size_trim_tol,
+                    qc_tol=qc_tol, **kwargs).params
             start_params = np.append(start_params, 0.1)
 
         cntfit = super(CountModel, self).fit_regularized(


Test patch: