Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

an aborted fit with method='least_squares' does not report correct fit results and statistics #896

Closed
newville opened this issue Jun 23, 2023 · 3 comments · Fixed by #897

Comments

@newville
Copy link
Member

Description

As shown in #894, a fit with method='least_squares' that aborts, perhaps due to too many function evaluations, does not properly evaluate the residual and fit statistics with "last/best available parameter values".

Note that other methods do evaluate the residual and fit statistics with "last/best available parameter values" when the fit is aborted.

A Minimal, Complete, and Verifiable example

see #894

Link(s)

see #894

@newville newville mentioned this issue Jun 23, 2023
12 tasks
@eendebakpt
Copy link
Contributor

@newville Thanks for picking this up. It seems also basinhopping, ampgo and shgo have the same issue. Tested with:

import lmfit
from lmfit import Model
import numpy as np

def gaussian(
    x, mean: float, std: float, amplitude: float = 1.0, offset: float = 0.0):
    x0 = x - mean
    y = offset + amplitude * np.exp(-x0 * x0 / (2.0 * std * std))
    return y


x_data, y_data = (np.array([18.025, 18.075, 18.125, 18.175, 18.225, 18.275, 18.325, 18.375,
        18.425, 18.475, 18.525, 18.575, 18.625, 18.675, 18.725, 18.775,
        18.825, 18.875, 18.925, 18.975, 19.025, 19.075, 19.125, 19.175,
        19.225, 19.275, 19.325, 19.375, 19.425, 19.475, 19.525, 19.575,
        19.625, 19.675, 19.725, 19.775, 19.825, 19.875, 19.925, 19.975,
        20.025, 20.075, 20.125, 20.175, 20.225, 20.275, 20.325, 20.375,
        20.425, 20.475, 20.525, 20.575, 20.625, 20.675, 20.725, 20.775,
        20.825, 20.875, 20.925, 20.975]), np.array([ 43,  27,  14,  16,  20,  10,  15,   8,   3,   9,   2,   4,   8,
          3,   3,   1,   2,   4,   5,   0,   1,   3,   1,   0,   0,   1,
          0,   0,   0,   0,   0,   0,   1,  23, 202, 824, 344, 279, 276,
         23,   2,   7,   8,  17,  22,   3,  25,  24,  17,  99, 288, 172,
        252, 103,  24,  20,   4,   1,   3,   1], dtype=int))

initial_parameters = {'mean': 18.286083743842365, 'std': 20.07653394255875, 'amplitude': 43}

m=Model(gaussian)
m.set_param_hint("amplitude", min=0)

# not tested: 'differential_evolution', 'brute','newton', 'trust-ncg','trust-krylov','dogleg','dual_annealing' 
for method in ['leastsq','least_squares','basinhopping','ampgo','nelder','lbfgsb','powell','cg','cobyla'
              ,'bfgs','shgo',    ]:
    r=m.fit(y_data, **initial_parameters, x=x_data,  method=method, max_nfev=2)
    #print(r.success,r.message)
    print(method, r.redchi) # has no valid value

@newville
Copy link
Member Author

@eendebakpt Thanks for catching that I guess that should not be too surprising that other methods have a similar problem.

@newville
Copy link
Member Author

@eendebakpt I adapted the PR at #897 to handle (and test) aborted fits for more methods, and make sure that they also end up with clearly aborted but still not-useless values for fit statistics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants