Solutions

Curve fitting results are returned as CurveFitSolution objects. These objects store the fitted coefficients, residuals, the problem solved and the algorithm used to solve it. It also stores a return code which holds information about the success of the solver. For more information on the ReturnCode type, see SciMLBase.jl.

CurveFit.CurveFitSolutionType
CurveFitSolution(alg, coeffs, resid, prob, retcode, original=nothing)

Represents the solution to a curve fitting problem. This is a callable struct and can be used to evaluate the solution at a point. Exact evaluation mechanism depends on the algorithm used to solve the problem.

Aliasing semantics

The x/y/sigma arrays in sol.prob alias the solver cache's internal arrays rather than copies of them. For nonlinear fits those internal arrays are, by default, copies of the problems input arrays. A subsequent reinit! of the cache mutates them in place, so a solution returned beforehand will reflect the new data. Copy the sol.prob fields if you need a stable snapshot.

Whether the cache aliases or copies the input x/y/sigma/u0 arrays can be controlled by passing a CurveFitAliasSpecifier as the alias keyword to init/solve. By default x/y/sigma are aliased and u0 is copied for the linear fits, while the nonlinear fits copy all of them so that reinit! doesn't overwrite the users input.

source

StatsAPI functions

CurveFitSolution objects can be treated as statistical models and CurveFit defines various statistics methods for the CurveFitSolution type, many of which extend StatsAPI.jl functions. Import these functions from StatsAPI.jl.

StatsAPI.residualsMethod
residuals(sol::CurveFitSolution; weighted::Bool = true)

Return the residuals of the fitted model.

When the problem was constructed with a sigma, residuals are returned as the weighted form (y - ŷ) / σ (i.e. the quantity actually minimized by the solver). Pass weighted = false to instead get the raw y - ŷ. Without a sigma, both options return the same thing.

source
CurveFit.mseFunction
mse(sol::CurveFitSolution; weighted::Bool = true)

Return the mean squared error of the fit.

Computed as rss(sol; weighted) / dof_residual(sol). With a sigma on the problem this is the reduced χ²; pass weighted = false for the plain MSE.

source
StatsAPI.dofMethod
dof(sol::CurveFitSolution)

Return the number of degrees of freedom of the model.

Note that this counts the fitted coefficients. Coefficients that are held fixed (e.g. the constant of ExpSumFitAlgorithm with withconst = false) are excluded.

source
StatsAPI.dof_residualMethod
dof_residual(sol::CurveFitSolution)

Return the residual degrees of freedom.

This is defined as nobs(sol) - dof(sol).

source
StatsAPI.predictMethod
predict(sol::CurveFitSolution, x = sol.prob.x)

Evaluate the fitted model with new data.

If x is not provided, predictions are returned at the original data points used during fitting.

source
StatsAPI.coefMethod
coef(sol::CurveFitSolution)

Return the fitted coefficients.

The ordering of coefficients depends on the fitting algorithm used.

source
StatsAPI.nobsMethod
nobs(sol::CurveFitSolution)

Return the number of observations used in the fit.

source
StatsAPI.fittedMethod
fitted(sol::CurveFitSolution)

Return the fitted values at the original data points.

source
StatsAPI.rssMethod
rss(sol::CurveFitSolution; weighted::Bool = true)

Return the residual sum of squares (RSS), defined as sum(abs2, residuals(sol; weighted)).

When sigma was provided to the problem, the default weighted RSS is χ². Pass weighted = false to get the unweighted sum(abs2, y - ŷ) instead.

source
CurveFit.isconvergedFunction
isconverged(sol::CurveFitSolution)

Return true if the underlying solver successfully converged.

This is determined from the solver return code.

source
StatsAPI.vcovMethod
vcov(sol::CurveFitSolution; absolute_sigma::Bool = false)

Return the variance–covariance matrix of the fitted coefficients.

The covariance matrix is computed via QR on the (weighted) Jacobian. When sigma was provided to the problem, the Jacobian is scaled by 1 / σ so the result is (JᵀWJ)⁻¹ with W = diag(1 / σ²).

The covariance is then rescaled by reduced χ² (i.e. mse(sol)) so that sigma acts as a relative weight. Pass absolute_sigma = true to skip this rescaling when sigma carries absolute physical uncertainties (analogous to scipy's curve_fit(absolute_sigma=true)).

Fits estimated in a transformed space

Some fits estimate their coefficients by ordinary least squares (OLS) in a transformed space rather than in original y-space:

To keep the reported uncertainty consistent with how the coefficients were actually estimated, the covariance is computed in that transformed space and then mapped back to the original parameters via the delta method, rather than from the original-space Jacobian used above.

Note that residuals, rss, and mse remain in original y-space as fit diagnostics, so for these fits mse(sol) is not the variance scaling behind vcov.

source
StatsAPI.stderrorMethod
stderror(sol::CurveFitSolution; absolute_sigma = false, rtol = NaN, atol = 0)

Return the standard errors of the fitted coefficients.

Standard errors are computed as the square roots of the diagonal elements of the variance–covariance matrix. See vcov for the meaning of absolute_sigma.

source
CurveFit.margin_errorFunction
margin_error(sol::CurveFitSolution, alpha = 0.05; absolute_sigma = false, rtol = NaN, atol = 0)

Returns the margin of error of the fitted coefficients, computed as stderror(sol) * t where t is the critical value of the t-distribution for 1 - alpha / 2. See vcov for the meaning of absolute_sigma.

source
StatsAPI.confintMethod
confint(sol::CurveFitSolution; level = 0.95, absolute_sigma = false, rtol = NaN, atol = 0)

Return confidence intervals for the fitted parameters.

The confidence intervals are returned as a vector of (lower, upper) tuples, computed as coef(sol) ± margin_error(sol). See vcov for the meaning of absolute_sigma.

source