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

Incorrect value in MinimizerResult.init_values #820

Closed
schtandard opened this issue Oct 30, 2022 · 1 comment · Fixed by #821
Closed

Incorrect value in MinimizerResult.init_values #820

schtandard opened this issue Oct 30, 2022 · 1 comment · Fixed by #821

Comments

@schtandard
Copy link
Contributor

First Time Issue Code

Yes, I read the instructions and I am sure this is a GitHub Issue.

Description

Using the simple example from the documentation and printing result.init_values (or result.init_vals) as in the MWE below reveals an incorrect initial value for amp. It should be 10 but it is 10.954451150103322.

A Minimal, Complete, and Verifiable example
import numpy as np

from lmfit import Minimizer, Parameters, report_fit

# create data to be fitted
x = np.linspace(0, 15, 301)
np.random.seed(2021)
data = (5.0 * np.sin(2.0*x - 0.1) * np.exp(-x*x*0.025) +
        np.random.normal(size=x.size, scale=0.2))


# define objective function: returns the array to be minimized
def fcn2min(params, x, data):
    """Model a decaying sine wave and subtract data."""
    amp = params['amp']
    shift = params['shift']
    omega = params['omega']
    decay = params['decay']
    model = amp * np.sin(x*omega + shift) * np.exp(-x*x*decay)
    return model - data


# create a set of Parameters
params = Parameters()
params.add('amp', value=10, min=0)
params.add('decay', value=0.1)
params.add('shift', value=0.0, min=-np.pi/2., max=np.pi/2.)
params.add('omega', value=3.0)

# do fit, here with the default leastsq algorithm
minner = Minimizer(fcn2min, params, fcn_args=(x, data))
result = minner.minimize()

# calculate final result
final = data + result.residual

# write error report
report_fit(result)

# now print the inital values
print(result.init_values)

This gives the output

[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 59
    # data points      = 301
    # variables        = 4
    chi-square         = 12.1867036
    reduced chi-square = 0.04103267
    Akaike info crit   = -957.236198
    Bayesian info crit = -942.407756
[[Variables]]
    amp:    5.03087926 +/- 0.04005805 (0.80%) (init = 10)
    decay:  0.02495454 +/- 4.5395e-04 (1.82%) (init = 0.1)
    shift: -0.10264934 +/- 0.01022298 (9.96%) (init = 0)
    omega:  2.00026304 +/- 0.00326184 (0.16%) (init = 3)
[[Correlations]] (unreported correlations are < 0.100)
    C(shift, omega) = -0.785
    C(amp, decay)   = 0.584
    C(amp, shift)   = -0.118
{'amp': 10.954451150103322, 'decay': 0.1, 'shift': 0.0, 'omega': 3.0}

report_fit correctly reports an initial value of 10 for amp but result.init_values does not.

Version information
Python: 3.9.13 | packaged by conda-forge | (main, May 27 2022, 16:50:36) [MSC v.1929 64 bit (AMD64)]

lmfit: 1.0.3, scipy: 1.9.3, numpy: 1.23.4,asteval: 0.9.27, uncertainties: 3.1.7
@newville
Copy link
Member

@schtandard OK, thanks... that's not good!

It looks to me like that is still the "internal value", before bounds are applied. I'll check that out.

@newville newville mentioned this issue Oct 31, 2022
12 tasks
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