Skip to content

uncertainties not propagated to all constraint expressions #855

Closed
@newville

Description

@newville

Description

As shown at https://groups.google.com/g/lmfit-py/c/KGrks5UBqfs/m/F2XfM286BwAJ, uncertainties are not correctly propagated to constraint expressions that include other constraint expressions.

A Minimal, Complete, and Verifiable example
import numpy as np
from lmfit.models import SineModel

model = SineModel()
params = model.make_params(amplitude=1, frequency=9.0, shift=0)
params.add("period", expr="1/frequency")
params.add("period_2a", expr="2*period")
params.add("period_2b", expr="2/frequency")
params.add("thing_1",   expr="shift + frequency")
params.add("thing_2",   expr="shift + 1/period")

np.random.seed(3)

xs = np.linspace(0, 1, 51)
ys = np.sin(7.45 * xs) + 0.01 * np.random.normal(size=xs.shape)

fit = model.fit(ys, x=xs, params=params)
print(fit.fit_report())

which shows:

[[Model]]
    Model(sine)
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 25
    # data points      = 51
    # variables        = 3
    chi-square         = 0.00505054
    reduced chi-square = 1.0522e-04
    Akaike info crit   = -464.224405
    Bayesian info crit = -458.428928
    R-squared          = 0.99978830
[[Variables]]
    amplitude:  1.00048377 +/- 0.00209960 (0.21%) (init = 1)
    frequency:  7.43904407 +/- 0.00641239 (0.09%) (init = 9)
    shift:      0.00310624 +/- 0.00382291 (123.07%) (init = 0)
    period:     0.13442587 +/- 1.1587e-04 (0.09%) == '1/frequency'
    period_2a:  0.26885175 +/- 0.00000000 (0.00%) == '2*period'
    period_2b:  0.26885175 +/- 2.3175e-04 (0.09%) == '2/frequency'
    thing_1:    7.44215031 +/- 0.00372918 (0.05%) == 'shift + frequency'
    thing_2:    7.44215031 +/- 0.00382290 (0.05%) == 'shift + 1/period'
[[Correlations]] (unreported correlations are < 0.100)
    C(frequency, shift) = -0.8531

The uncertainties in period_2a and period_2b should be the same, as should the uncertainties in thing_1 and thing_2.

Version information

Python: 3.10.9 | packaged by conda-forge | (main, Feb 2 2023, 20:24:27) [Clang 14.0.6 ]
lmfit: 1.1.0, scipy: 1.10.1, numpy: 1.24.2,asteval: 0.9.29, uncertainties: 3.1.7

Link(s)

https://groups.google.com/g/lmfit-py/c/KGrks5UBqfs/m/F2XfM286BwAJ

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions