Skip to content

got multiple values for argument 'constraints', 'bounds' #6655

Closed

Description

@emailhy

Describe the bug

error happens when provide "bounds" or "constraints" option for scipy minimize

Code Sample, a copy-pastable example if possible

~/anaconda3/lib/python3.7/site-packages/statsmodels/base/optimizer.py in _fit_minimize(f, score, start_params, fargs, kwargs, disp, maxiter, callback, retall, full_output, hess)
    273 
    274     res = optimize.minimize(f, start_params, args=fargs, method=kwargs['min_method'],
--> 275                             jac=score, hess=hess, callback=callback, options=options)
    276 
    277     xopt    = res.x

~/anaconda3/lib/python3.7/site-packages/scipy/optimize/_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
    616     elif meth == 'slsqp':
    617         return _minimize_slsqp(fun, x0, args, jac, bounds,
--> 618                                constraints, callback=callback, **options)
    619     elif meth == 'trust-constr':
    620         return _minimize_trustregion_constr(fun, x0, args, jac, hess, hessp,

TypeError: _minimize_slsqp() got multiple values for argument 'constraints'
Details

This happens when a keyword argument is specified that overwrites a positional argument.
as scipy minimize already has bounds, constraints in the signature, and the options pass the bunds and constraints again.

It should handled the same way as jac and hess, thank you

If the issue has not been resolved, please file it in the issue tracker.

Expected Output

Run without give error

Output of import statsmodels.api as sm; sm.show_versions()

Details statsmodels ===========

Installed: 0.11.1

Activity

  1. josef-pkt commented on Apr 22, 2020

    @josef-pkt
    Member

    Which model?
    Can you make up a simple example that we can run?

    This was never checked.

    aside: standard asymptotic theory for inference doesn't apply if the parameter can be at the boundary of the parameter space, i.e. if there are possibly binding inequality constraints.

  2. emailhy commented on Apr 22, 2020

    @emailhy
    ContributorAuthor

    according to the docs: optimizer
    this should affect glm and all other models which can apply scipy optimizer .

    The problem is due to the mismatch of the base/optimizer.py and the scipy's minimize function signature. scipy's minimize function has default named args for bounds and constraints.

    statsmodels' optimizer put bounds, and constraints into the "options" and then pass to scipy's optimizer "options", but scipy has already has named args in its arg list, so cause the bunds and constraints be passed multiple times,

    i verified by modified the optimizer.py

    get the bounds and constraints from the kwargs and not put them into the options.
    optimize.minimize(..., bounds=bounds, constraints=constraints, callback=callback, options=options)

    then it will work,

    Thanks for this great package

  3. emailhy commented on Apr 22, 2020

    @emailhy
    ContributorAuthor

    Here is a minimal example

    import numpy as np
    import statsmodels.api as sm
    from scipy import stats
    from matplotlib import pyplot as plt
    
    data2 = sm.datasets.scotland.load()
    data2.exog = sm.add_constant(data2.exog, prepend=False)
    
    bnds = tuple([(0, None)]*data2.exog.shape[1])
    
    glm_gamma = sm.GLM(data2.endog, data2.exog, family=sm.families.Gamma())
    glm_results = glm_gamma.fit(method='minimize', min_method='SLSQP', bounds = bnds)
    print(glm_results.summary())
    
  4. josef-pkt commented on Apr 22, 2020

    @josef-pkt
    Member

    Thanks

    I'm getting the correpsonding bounds error for the example
    TypeError: _minimize_slsqp() got multiple values for argument 'bounds'

    this works, and imposes nonnegativity constraints in example

    glm_results = glm_gamma.fit(method='lbfgs', bounds=bnds)
    print(glm_results.summary())
    
  5. josef-pkt commented on Apr 22, 2020

    @josef-pkt
    Member

    our _fit_lbfgs
    uses bounds = kwargs.setdefault('bounds', [(None, None)] * len(start_params)) to get the separate keyword argument.

    @emailhy can you open a PR with your fix, and some unit tests?

  6. emailhy commented on Apr 23, 2020

    @emailhy
    ContributorAuthor

    @josef-pkt
    I submitted the PR, but it seems one last check failed at get r dataset. Please review, with this constraints like monotone , nonnegative and complex restrictions can be applied. Thanks

  7. emailhy commented on Apr 26, 2020

    @emailhy
    ContributorAuthor

    @josef-pkt
    I fixed all the format issues in my pull request, Thank you.

  8. emailhy commented on Apr 28, 2020

    @emailhy
    ContributorAuthor

    Hi @josef-pkt, Is there anything else I need to add, the PR passed all checks and all comments has been addressed. Please guide for next step. Thank you.

  9. added this to the 0.12 milestone on Jul 27, 2020
  10. Kvothe-The-Bloodless commented on Aug 3, 2021

    @Kvothe-The-Bloodless

    I think there is a related bug:

    from scipy.optimize import rosen, rosen_der, rosen_hess
    bounds = [(0,1.6), (0, 1.6), (0, 1.4), (0, 1.4), (0, 1.4)]
    result = scipy.optimize.shgo(rosen, bounds, options={'jac':rosen_der,'hess':rosen_hess})
    

    gives

    TypeError: _minimize_slsqp() got multiple values for argument 'jac'

    Do you agree? Is there a work around?

  11. emailhy commented on Aug 4, 2021

    @emailhy
    ContributorAuthor

    @Kvothe-The-Bloodless , that is scipy's bug, but you are right, same reason

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