def test_confidence1():
    x = np.linspace(0.3,10,100)
    np.random.seed(0)

    y = 1/(0.1*x)+2+0.1*np.random.randn(x.size)

    pars = lmfit.Parameters()
    pars.add_many(('a', 0.1), ('b', 1))

    minimizer = lmfit.Minimizer(residual, pars, fcn_args=(x, y) )
    out = minimizer.leastsq()
    # lmfit.report_fit(out)

    assert(out.nfev >   5)
    assert(out.nfev < 500)
    assert(out.chisqr < 3.0)
    assert(out.nvarys == 2)

    assert_paramval(out.params['a'],  0.1, tol=0.1)
    assert_paramval(out.params['b'], -2.0, tol=0.1)

    ci = lmfit.conf_interval(minimizer, out)
    assert_allclose(ci['b'][0][0],  0.997,  rtol=0.01)
    assert_allclose(ci['b'][0][1], -2.022,  rtol=0.01)
    assert_allclose(ci['b'][2][0],  0.674,  rtol=0.01)
    assert_allclose(ci['b'][2][1], -1.997,  rtol=0.01)
    assert_allclose(ci['b'][5][0],  0.95,   rtol=0.01)
    assert_allclose(ci['b'][5][1], -1.96,   rtol=0.01)
def test_confidence2():
    x = np.linspace(0.3,10,100)
    np.random.seed(0)

    y = 1/(0.1*x)+2+0.1*np.random.randn(x.size)

    pars = lmfit.Parameters()
    pars.add_many(('a', 0.1), ('b', 1), ('c', 1.0))
    pars['a'].max = 0.25
    pars['a'].min = 0.00
    pars['a'].value = 0.2
    pars['c'].vary = False

    minimizer = lmfit.Minimizer(residual2, pars, fcn_args=(x, y) )
    out = minimizer.minimize(method='nelder')
    out = minimizer.minimize(method='leastsq', params=out.params)
    # lmfit.report_fit(out)

    assert(out.nfev >   5)
    assert(out.nfev < 500)
    assert(out.chisqr < 3.0)
    assert(out.nvarys == 2)

    assert_paramval(out.params['a'],  0.1, tol=0.1)
    assert_paramval(out.params['b'], -2.0, tol=0.1)

    ci = lmfit.conf_interval(minimizer, out)
    assert_allclose(ci['b'][0][0],  0.997,  rtol=0.01)
    assert_allclose(ci['b'][0][1], -2.022,  rtol=0.01)
    assert_allclose(ci['b'][2][0],  0.674,  rtol=0.01)
    assert_allclose(ci['b'][2][1], -1.997,  rtol=0.01)
    assert_allclose(ci['b'][5][0],  0.95,   rtol=0.01)
    assert_allclose(ci['b'][5][1], -1.96,   rtol=0.01)

    lmfit.printfuncs.report_ci(ci)
Example #3
0
def test_confidence1():
    x = np.linspace(0.3, 10, 100)
    np.random.seed(0)

    y = 1 / (0.1 * x) + 2 + 0.1 * np.random.randn(x.size)

    pars = lmfit.Parameters()
    pars.add_many(('a', 0.1), ('b', 1))

    minimizer = lmfit.Minimizer(residual, pars, fcn_args=(x, y))
    out = minimizer.leastsq()
    # lmfit.report_fit(out)

    assert (out.nfev > 5)
    assert (out.nfev < 500)
    assert (out.chisqr < 3.0)
    assert (out.nvarys == 2)

    assert_paramval(out.params['a'], 0.1, tol=0.1)
    assert_paramval(out.params['b'], -2.0, tol=0.1)

    ci = lmfit.conf_interval(minimizer, out)
    assert_allclose(ci['b'][0][0], 0.997, rtol=0.01)
    assert_allclose(ci['b'][0][1], -2.022, rtol=0.01)
    assert_allclose(ci['b'][2][0], 0.674, rtol=0.01)
    assert_allclose(ci['b'][2][1], -1.997, rtol=0.01)
    assert_allclose(ci['b'][5][0], 0.95, rtol=0.01)
    assert_allclose(ci['b'][5][1], -1.96, rtol=0.01)
Example #4
0
def test_confidence2():
    x = np.linspace(0.3, 10, 100)
    np.random.seed(0)

    y = 1 / (0.1 * x) + 2 + 0.1 * np.random.randn(x.size)

    pars = lmfit.Parameters()
    pars.add_many(('a', 0.1), ('b', 1), ('c', 1.0))
    pars['a'].max = 0.25
    pars['a'].min = 0.00
    pars['a'].value = 0.2
    pars['c'].vary = False

    minimizer = lmfit.Minimizer(residual2, pars, fcn_args=(x, y))
    out = minimizer.minimize(method='nelder')
    out = minimizer.minimize(method='leastsq', params=out.params)
    # lmfit.report_fit(out)

    assert (out.nfev > 5)
    assert (out.nfev < 500)
    assert (out.chisqr < 3.0)
    assert (out.nvarys == 2)

    assert_paramval(out.params['a'], 0.1, tol=0.1)
    assert_paramval(out.params['b'], -2.0, tol=0.1)

    ci = lmfit.conf_interval(minimizer, out)
    assert_allclose(ci['b'][0][0], 0.997, rtol=0.01)
    assert_allclose(ci['b'][0][1], -2.022, rtol=0.01)
    assert_allclose(ci['b'][2][0], 0.674, rtol=0.01)
    assert_allclose(ci['b'][2][1], -1.997, rtol=0.01)
    assert_allclose(ci['b'][5][0], 0.95, rtol=0.01)
    assert_allclose(ci['b'][5][1], -1.96, rtol=0.01)

    lmfit.printfuncs.report_ci(ci)
Example #5
0
def test_basic():
    # create data to be fitted
    x = np.linspace(0, 15, 301)
    data = (5. * np.sin(2 * x - 0.1) * np.exp(-x * x * 0.025) +
            np.random.normal(size=len(x), scale=0.2))

    # define objective function: returns the array to be minimized
    def fcn2min(params, x, data):
        """ model decaying sine wave, 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 leastsq model
    result = minimize(fcn2min, params, args=(x, data))

    assert (result.nfev > 5)
    assert (result.nfev < 500)
    assert (result.chisqr > 1)
    assert (result.nvarys == 4)
    assert_paramval(result.params['amp'], 5.03, tol=0.05)
    assert_paramval(result.params['omega'], 2.0, tol=0.05)
Example #6
0
def test_basic():
    # create data to be fitted
    x = np.linspace(0, 15, 301)
    data = (5. * np.sin(2 * x - 0.1) * np.exp(-x*x*0.025) +
            np.random.normal(size=len(x), scale=0.2))

    # define objective function: returns the array to be minimized
    def fcn2min(params, x, data):
        """ model decaying sine wave, 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 leastsq model
    result = minimize(fcn2min, params, args=(x, data))

    assert(result.nfev > 5)
    assert(result.nfev < 500)
    assert(result.chisqr > 1)
    assert(result.nvarys == 4)
    assert_paramval(result.params['amp'], 5.03, tol=0.05)
    assert_paramval(result.params['omega'], 2.0, tol=0.05)
Example #7
0
def test_confidence_pnames(data, pars):
    """Test if pnames works as expected."""
    minimizer = lmfit.Minimizer(residual, pars, fcn_args=(data))
    out = minimizer.leastsq()

    assert_paramval(out.params['a'], 0.1, tol=0.1)
    assert_paramval(out.params['b'], -2.0, tol=0.1)

    ci = lmfit.conf_interval(minimizer, out, p_names=['a'])
    assert 'a' in ci
    assert 'b' not in ci
Example #8
0
def test_bounded_jacobian():
    pars = Parameters()
    pars.add('x0', value=2.0)
    pars.add('x1', value=2.0, min=1.5)

    global jac_count

    jac_count = 0

    def resid(params):
        x0 = params['x0']
        x1 = params['x1']
        return np.array([10 * (x1 - x0*x0), 1-x0])

    def jac(params):
        global jac_count
        jac_count += 1
        x0 = params['x0']
        return np.array([[-20*x0, 10], [-1, 0]])

    out0 = minimize(resid, pars, Dfun=None)

    assert_paramval(out0.params['x0'], 1.2243, tol=0.02)
    assert_paramval(out0.params['x1'], 1.5000, tol=0.02)
    assert jac_count == 0

    out1 = minimize(resid, pars, Dfun=jac)

    assert_paramval(out1.params['x0'], 1.2243, tol=0.02)
    assert_paramval(out1.params['x1'], 1.5000, tol=0.02)
    assert jac_count > 5
Example #9
0
def test_bounded_jacobian_CG():
    pars = Parameters()
    pars.add('x0', value=2.0)
    pars.add('x1', value=2.0, min=1.5)

    global jac_count

    jac_count = 0

    def resid(params):
        x0 = params['x0']
        x1 = params['x1']
        return np.array([10 * (x1 - x0*x0), 1-x0])

    def jac(params):
        global jac_count
        jac_count += 1
        x0 = params['x0']
        # Jacobian of the *error*, i.e. the summed squared residuals
        return 2 * np.sum(np.array([[-20 * x0, 10],
                                    [-1, 0]]).T * resid(params), axis=1)

    out0 = minimize(resid, pars, method='CG')
    assert_paramval(out0.params['x0'], 1.2243, tol=0.02)
    assert_paramval(out0.params['x1'], 1.5000, tol=0.02)
    assert jac_count == 0

    out1 = minimize(resid, pars, method='CG', Dfun=jac)
    assert_paramval(out1.params['x0'], 1.2243, tol=0.02)
    assert_paramval(out1.params['x1'], 1.5000, tol=0.02)
    assert jac_count > 5
Example #10
0
def test_bounded_jacobian():
    pars = Parameters()
    pars.add('x0', value=2.0)
    pars.add('x1', value=2.0, min=1.5)

    global jac_count

    jac_count = 0

    def resid(params):
        x0 = params['x0']
        x1 = params['x1']
        return np.array([10 * (x1 - x0*x0), 1-x0])

    def jac(params):
        global jac_count
        jac_count += 1
        x0 = params['x0']
        return np.array([[-20*x0, 10], [-1, 0]])

    out0 = minimize(resid, pars, Dfun=None)

    assert_paramval(out0.params['x0'], 1.2243, tol=0.02)
    assert_paramval(out0.params['x1'], 1.5000, tol=0.02)
    assert(jac_count == 0)

    out1 = minimize(resid, pars, Dfun=jac)

    assert_paramval(out1.params['x0'], 1.2243, tol=0.02)
    assert_paramval(out1.params['x1'], 1.5000, tol=0.02)
    assert(jac_count > 5)
Example #11
0
def test_bounds():
    if not HAS_LEAST_SQUARES:
        raise nose.SkipTest
    p_true = Parameters()
    p_true.add('amp', value=14.0)
    p_true.add('period', value=5.4321)
    p_true.add('shift', value=0.12345)
    p_true.add('decay', value=0.01000)

    def residual(pars, x, data=None):
        amp = pars['amp']
        per = pars['period']
        shift = pars['shift']
        decay = pars['decay']

        if abs(shift) > pi/2:
            shift = shift - sign(shift)*pi

        model = amp*sin(shift + x/per) * exp(-x*x*decay*decay)
        if data is None:
            return model
        return (model - data)

    n = 1500
    xmin = 0.
    xmax = 250.0
    random.seed(0)
    noise = random.normal(scale=2.80, size=n)
    x     = linspace(xmin, xmax, n)
    data  = residual(p_true, x) + noise

    fit_params = Parameters()
    fit_params.add('amp', value=13.0, max=20, min=0.0)
    fit_params.add('period', value=2, max=10)
    fit_params.add('shift', value=0.0, max=pi/2., min=-pi/2.)
    fit_params.add('decay', value=0.02, max=0.10, min=0.00)

    min = Minimizer(residual, fit_params, (x, data))
    out = min.least_squares()

    assert(out.nfev  > 10)
    assert(out.nfree > 50)
    assert(out.chisqr > 1.0)

    print(fit_report(out, show_correl=True, modelpars=p_true))
    assert_paramval(out.params['decay'], 0.01, tol=1.e-2)
    assert_paramval(out.params['shift'], 0.123, tol=1.e-2)
Example #12
0
def test_bounds():
    p_true = Parameters()
    p_true.add("amp", value=14.0)
    p_true.add("period", value=5.4321)
    p_true.add("shift", value=0.12345)
    p_true.add("decay", value=0.01000)

    def residual(pars, x, data=None):
        amp = pars["amp"].value
        per = pars["period"].value
        shift = pars["shift"].value
        decay = pars["decay"].value

        if abs(shift) > pi / 2:
            shift = shift - sign(shift) * pi

        model = amp * sin(shift + x / per) * exp(-x * x * decay * decay)
        if data is None:
            return model
        return model - data

    n = 1500
    xmin = 0.0
    xmax = 250.0
    random.seed(0)
    noise = random.normal(scale=2.80, size=n)
    x = linspace(xmin, xmax, n)
    data = residual(p_true, x) + noise

    fit_params = Parameters()
    fit_params.add("amp", value=13.0, max=20, min=0.0)
    fit_params.add("period", value=2, max=10)
    fit_params.add("shift", value=0.0, max=pi / 2.0, min=-pi / 2.0)
    fit_params.add("decay", value=0.02, max=0.10, min=0.00)

    out = minimize(residual, fit_params, args=(x,), kws={"data": data})

    fit = residual(out.params, x)

    assert out.nfev > 10
    assert out.nfree > 50
    assert out.chisqr > 1.0

    print(fit_report(out, show_correl=True, modelpars=p_true))
    assert_paramval(out.params["decay"], 0.01, tol=1.0e-2)
    assert_paramval(out.params["shift"], 0.123, tol=1.0e-2)
Example #13
0
def test_bounds():
    p_true = Parameters()
    p_true.add('amp', value=14.0)
    p_true.add('period', value=5.4321)
    p_true.add('shift', value=0.12345)
    p_true.add('decay', value=0.01000)

    def residual(pars, x, data=None):
        amp = pars['amp']
        per = pars['period']
        shift = pars['shift']
        decay = pars['decay']

        if abs(shift) > pi / 2:
            shift = shift - sign(shift) * pi

        model = amp * sin(shift + x / per) * exp(-x * x * decay * decay)
        if data is None:
            return model
        return model - data

    n = 1500
    xmin = 0.
    xmax = 250.0
    random.seed(0)
    noise = random.normal(scale=2.80, size=n)
    x = linspace(xmin, xmax, n)
    data = residual(p_true, x) + noise

    fit_params = Parameters()
    fit_params.add('amp', value=13.0, max=20, min=0.0)
    fit_params.add('period', value=2, max=10)
    fit_params.add('shift', value=0.0, max=pi / 2., min=-pi / 2.)
    fit_params.add('decay', value=0.02, max=0.10, min=0.00)

    out = minimize(residual, fit_params, args=(x, ), kws={'data': data})

    assert out.nfev > 10
    assert out.nfree > 50
    assert out.chisqr > 1.0

    assert_paramval(out.params['decay'], 0.01, tol=1.e-2)
    assert_paramval(out.params['shift'], 0.123, tol=1.e-2)
Example #14
0
def test_bounds():
    p_true = Parameters()
    p_true.add('amp', value=14.0)
    p_true.add('period', value=5.4321)
    p_true.add('shift', value=0.12345)
    p_true.add('decay', value=0.01000)

    def residual(pars, x, data=None):
        amp = pars['amp']
        per = pars['period']
        shift = pars['shift']
        decay = pars['decay']

        if abs(shift) > pi/2:
            shift = shift - sign(shift)*pi

        model = amp*sin(shift + x/per) * exp(-x*x*decay*decay)
        if data is None:
            return model
        return (model - data)

    n = 1500
    xmin = 0.
    xmax = 250.0
    random.seed(0)
    noise = random.normal(scale=2.80, size=n)
    x = linspace(xmin, xmax, n)
    data = residual(p_true, x) + noise

    fit_params = Parameters()
    fit_params.add('amp', value=13.0, max=20, min=0.0)
    fit_params.add('period', value=2, max=10)
    fit_params.add('shift', value=0.0, max=pi/2., min=-pi/2.)
    fit_params.add('decay', value=0.02, max=0.10, min=0.00)

    out = minimize(residual, fit_params, args=(x,), kws={'data': data})

    assert(out.nfev > 10)
    assert(out.nfree > 50)
    assert(out.chisqr > 1.0)

    assert_paramval(out.params['decay'], 0.01, tol=1.e-2)
    assert_paramval(out.params['shift'], 0.123, tol=1.e-2)
Example #15
0
def test_confidence_leastsq(data, pars, verbose, capsys):
    """Calculate confidence interval after leastsq minimization."""
    minimizer = lmfit.Minimizer(residual, pars, fcn_args=(data))
    out = minimizer.leastsq()

    assert 5 < out.nfev < 500
    assert out.chisqr < 3.0
    assert out.nvarys == 2
    assert_paramval(out.params['a'], 0.1, tol=0.1)
    assert_paramval(out.params['b'], -2.0, tol=0.1)

    ci = lmfit.conf_interval(minimizer, out, verbose=verbose)
    assert_allclose(ci['b'][0][0], 0.997, rtol=0.01)
    assert_allclose(ci['b'][0][1], -2.022, rtol=0.01)
    assert_allclose(ci['b'][2][0], 0.683, rtol=0.01)
    assert_allclose(ci['b'][2][1], -1.997, rtol=0.01)
    assert_allclose(ci['b'][5][0], 0.95, rtol=0.01)
    assert_allclose(ci['b'][5][1], -1.96, rtol=0.01)

    if verbose:
        captured = capsys.readouterr()
        assert 'Calculating CI for' in captured.out
Example #16
0
def test_confidence_leastsq(data, pars, verbose, capsys):
    """Calculate confidence interval after leastsq minimization."""
    minimizer = lmfit.Minimizer(residual, pars, fcn_args=(data))
    out = minimizer.leastsq()

    assert 5 < out.nfev < 500
    assert out.chisqr < 3.0
    assert out.nvarys == 2
    assert_paramval(out.params['a'], 0.1, tol=0.1)
    assert_paramval(out.params['b'], -2.0, tol=0.1)

    ci = lmfit.conf_interval(minimizer, out, verbose=verbose)
    assert_allclose(ci['b'][0][0], 0.997, rtol=0.01)
    assert_allclose(ci['b'][0][1], -2.022, rtol=0.01)
    assert_allclose(ci['b'][2][0], 0.683, rtol=0.01)
    assert_allclose(ci['b'][2][1], -1.997, rtol=0.01)
    assert_allclose(ci['b'][5][0], 0.95, rtol=0.01)
    assert_allclose(ci['b'][5][1], -1.96, rtol=0.01)

    if verbose:
        captured = capsys.readouterr()
        assert 'Calculating CI for' in captured.out