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.CurveFitSolution — Type
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.
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.
StatsAPI.residuals — Function
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.
CurveFit.mse — Function
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.
StatsAPI.dof — Function
dof(sol::CurveFitSolution)Return the number of degrees of freedom of the model.
StatsAPI.dof_residual — Function
dof_residual(sol::CurveFitSolution)Return the residual degrees of freedom.
This is defined as nobs(sol) - dof(sol).
StatsAPI.predict — Function
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.
StatsAPI.coef — Function
coef(sol::CurveFitSolution)Return the fitted coefficients.
The ordering of coefficients depends on the fitting algorithm used.
StatsAPI.nobs — Function
nobs(sol::CurveFitSolution)Return the number of observations used in the fit.
StatsAPI.fitted — Function
fitted(sol::CurveFitSolution)Return the fitted values at the original data points.
StatsAPI.rss — Function
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.
CurveFit.isconverged — Function
isconverged(sol::CurveFitSolution)Return true if the underlying solver successfully converged.
This is determined from the solver return code.
StatsAPI.vcov — Function
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)).
StatsAPI.stderror — Function
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.
CurveFit.margin_error — Function
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.
StatsAPI.confint — Function
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.