API
LsqFit.curve_fit — Function
curve_fit(model, xdata, ydata, p0) -> fit
curve_fit(model, xdata, ydata, wt, p0) -> fitFit data to a non-linear model. p0 is an initial model parameter guess (see Example), and wt is an optional weighting of the observations. The return object is a composite type (LsqFitResult), with some interesting values:
fit.resid: residuals = vector of residualsfit.jacobian: estimated Jacobian at solution
additionally, it is possible to query the degrees of freedom with
dof(fit)coef(fit)
Weights
The values of wt are inverse variances, not standard deviations. A vector weights the squared residuals ∑ wtᵢ (model−y)ᵢ², so the optimal choice is wtᵢ = 1/σᵢ². A matrix is the inverse covariance Σ⁻¹ of the observations (a diagonal of 1/σᵢ² is the vector case).
The type of wt sets whether the residual scale σ² is known or estimated:
wt | scale σ² | vcov(fit) |
|---|---|---|
| omitted | estimated | σ̂² (JᵀJ)⁻¹ |
PrecisionWeights(1 ./ σ.^2) | known | (JᵀWJ)⁻¹ |
PrecisionMatrix(inv(Σ)) | known | (JᵀWJ)⁻¹ |
AnalyticWeights(1 ./ σ.^2) | estimated | σ̂² (JᵀWJ)⁻¹ |
FrequencyWeights(counts) | estimated, nobs=∑w | σ̂² (JᵀWJ)⁻¹ |
AnalyticWeights are relative inverse-variance weights and do not depend on the overall scale of the weights, matching MATLAB nlinfit, Origin and LabPlot. PrecisionWeights are the exact inverse variances and PrecisionMatrix the exact inverse covariance, with the scale known; both use the normal critical value for confidence intervals. vcov(fit) is the parameter covariance and stderror(fit) its square-rooted diagonal. The "Weights" page of the manual has the derivation and a coverage check.
Passing a bare inverse-variance Vector or inverse-covariance Matrix is deprecated. Use PrecisionWeights(1 ./ σ.^2) / PrecisionMatrix(inv(Σ)) for the same known-variance covariance (with a calibrated normal interval), or AnalyticWeights to estimate the scale.
Example
# a two-parameter exponential model
# x: array of independent variables
# p: array of model parameters
model(x, p) = p[1]*exp.(-x.*p[2])
# some example data
# xdata: independent variables
# ydata: dependent variable
xdata = range(0, stop=10, length=20)
ydata = model(xdata, [1.0 2.0]) + 0.01*randn(length(xdata))
p0 = [0.5, 0.5]
fit = curve_fit(model, xdata, ydata, p0)Weight types
LsqFit.PrecisionWeights — Type
PrecisionWeights(vs)Weights whose values are the exact, known inverse variances 1 ./ σ.^2 of the observations. The residual scale is treated as known rather than estimated, so vcov is (JᵀWJ)⁻¹ with no mean-squared-error factor and confidence intervals use the normal (asymptotic) critical value.
This is the typed equivalent of passing a bare inverse-variance vector. Use AnalyticWeights instead when the weights are only relative precisions and the scale should be estimated.
LsqFit.PrecisionMatrix — Type
PrecisionMatrix(m)The exact, known precision matrix (inverse covariance) inv(Σ) of correlated observations. This is the matrix counterpart of PrecisionWeights: the residual scale is treated as known, so vcov is (JᵀWJ)⁻¹ with no mean-squared-error factor and confidence intervals use the normal critical value.