Skip to content

Performance: Logit and GLM, and sklearn, IRLS slow with highly collinear exog #3990

Open

Description

@josef-pkt

based on a discussion on the sklearn mailing list, and some timings shown there

GLM IRLS is very slow compared to the other optimizers in artificial datasets created with sklearn. The main problem seems to be that several singular values are in the neighborhood of 1e-14 or 1e-15.
(My first guess was that convergence criterion in GLM uses deviance, but the slowness is also with params)

If I add noise to exog to make it well conditioned, as in the following script, then IRLS and other optimizers are comparable. In a larger dataset nobs=1Million, GLM takes about 4 times longer than Logit, using current master.

(Note, timing in statsmodels include covariance estimation)

sklearn.LogisticRegression: l1 6.6286749839782715
sklearn.LogisticRegression: l2 7.15504789352417
QC check did not pass for 51 out of 51 parameters
Try increasing solver accuracy or number of iterations, decreasing alpha, or switch solvers
Could not trim params automatically due to failed QC check.  Trimming using trim_mode == 'size' will still work.
sm.Logit l1 4.817397832870483
sm.Logit l1_cvxopt_cp 26.204403162002563
sm.Logit newton 6.074285984039307
sm.Logit nm 135.2503378391266
m:\josef_new\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\base\model.py:511: ConvergenceWarning: Maximum Likelihood optimization failed to converge. Check mle_retvals
  "Check mle_retvals", ConvergenceWarning)
sm.Logit bfgs 5.553915977478027
sm.Logit lbfgs 4.537201166152954
sm.Logit powell 271.670814037323
sm.Logit cg 8.214794874191284
sm.Logit ncg 7.019952058792114
sm.GLM.fit IRLS 20.715612173080444
sm.GLM L2: 157.6966769695282
sm.GLM.fit bfgs 19.05343985557556
sm.GLM.fit lbfgs 17.348237991333008
sm.GLM.fit newton 18.742539167404175
m:\josef_new\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\base\model.py:511: ConvergenceWarning: Maximum Likelihood optimization failed to converge. Check mle_retvals
  "Check mle_retvals", ConvergenceWarning)
sm.GLM.fit nm 29.833044052124023
sm.GLM.fit powell 172.68626284599304
sm.GLM.fit cg 19.932121992111206
sm.GLM.fit ncg 18.5813729763031
sm.GLM.fit basinhopping 1227.4370460510254

# -*- coding: utf-8 -*-
"""
Created on Thu Oct  5 15:48:35 2017

scikit-learn mailing list
"""

import numpy as np
import sklearn.datasets
from sklearn.preprocessing import StandardScaler

#np.random.seed(987126)
X, y = sklearn.datasets.make_classification(n_samples=1000000,
n_features=50, random_state=987126) #123)
X0 = StandardScaler(copy=True, with_mean=True, with_std=True).fit_transform(X)
from statsmodels.tools.tools import add_constant
X += 0.5 * np.random.randn(*X.shape)
X = add_constant(X)
import time
tol = 0.0001
maxiter = 1000
DISP = 0


if 1: # sk.LogisticRegression
    import sklearn
    from sklearn.linear_model import LogisticRegression

    for method in ["l1", "l2"]: # TODO, add solvers:
        t = time.time()
        model = LogisticRegression(C=1, tol=tol, max_iter=maxiter,
penalty=method)
        model.fit(X0,y)
        print("sklearn.LogisticRegression:", method, time.time() - t)




if 1: # sm.Logit.fit_regularized
    from statsmodels.discrete.discrete_model import Logit
    for method in ["l1", "l1_cvxopt_cp"]:
        t = time.time()
        model = Logit(y,X)
        result = model.fit_regularized(method=method, maxiter=maxiter,
                                       alpha=1.,
                                       abstol=tol,
                                       acc=tol,
                                       tol=tol, gtol=tol, pgtol=tol,
                                       disp=DISP)
        print("sm.Logit", method, time.time() - t)

if 1: # sm.Logit.fit
    from statsmodels.discrete.discrete_model import Logit

    SOLVERS = ["newton", "nm",
"bfgs","lbfgs","powell","cg","ncg"] #,"basinhopping",]
    for method in SOLVERS:
        t = time.time()
        model = Logit(y,X)
        result = model.fit(method=method, maxiter=maxiter,
                           niter=maxiter,
                           ftol=tol,
                           tol=tol, gtol=tol, pgtol=tol,  # Hmmm.. needs to be reviewed.
                           disp=DISP)
        print("sm.Logit", method, time.time() - t)

if 1: # sm.GLM.fit
    from statsmodels.genmod.generalized_linear_model import GLM
    from statsmodels.genmod.generalized_linear_model import families
    for method in ["IRLS"]:
        t = time.time()
        model = GLM(y, X, family=families.Binomial(link=families.links.logit))
        result = model.fit(method=method, atol=tol,
maxiter=maxiter, full_output=False, disp=DISP, tol_criterion='params')
        print("sm.GLM.fit", method, time.time() - t)


if 1: # GLM.fit_regularized
    from statsmodels.genmod.generalized_linear_model import GLM
    from statsmodels.genmod.generalized_linear_model import families
    t = time.time()
    model = GLM(y, X, family=families.Binomial(link=families.links.logit))
    result = model.fit_regularized(method='elastic_net', alpha=1.0,
L1_wt=0.0, cnvrg_tol=tol, maxiter=maxiter)
    print("sm.GLM L2:", time.time() - t)



if 1: # GLM.fit
    # Hits division by zero.
    SOLVERS = ["bfgs","lbfgs", "newton", "nm",
"powell","cg","ncg","basinhopping",]
    from statsmodels.genmod.generalized_linear_model import GLM
    from statsmodels.genmod.generalized_linear_model import families
    for method in SOLVERS:
        t = time.time()
        model = GLM(y, X, family=families.Binomial(link=families.links.logit))
        result = model.fit(method=method,
#                           scale="X2",
#                            alpha=1.,
#                            abstol=tol,
#                            acc=tol,
#                            tol=tol, gtol=tol, pgtol=tol,
#                            maxiter=maxiter,
#                            #full_output=False,
                           
                           disp=DISP)
        print("sm.GLM.fit", method, time.time() - t)

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions