curve_fit
Builtin index · Signal processing
result = curve_fit(model, xdata, ydata, p0, [max_iter=200], [tol=1e-10])
Input: model, xdata, ydata, p0, max_iter, tol · Output: result
What it is
model: a string naming a user-defined function model(x, params) (x a scalar, params a length-P vector) returning a scalar; xdata, ydata: length-N vectors; p0: a length-P vector, the initial parameter guess; max_iter/tol (optional numbers).
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = curve_fit(model, xdata, ydata, p0, [max_iter=200], [tol=1e-10]) | model string, scalar, or vectormodel: a string naming a user-defined function model(x, params) (x a scalar, params a length-P vector) returning a scalar.xdata vectorxdata, ydata: length-N vectors.ydata vectorxdata, ydata: length-N vectors.p0 vectorp0: a length-P vector, the initial parameter guess.max_iter number, optional (default 200)max_iter/tol (optional numbers).tol number, optional (default 1e-10)max_iter/tol (optional numbers). | Model —kind "curve_fit"params vectora length-P vectorcost numbera number, residual sum of squaresconverged booleana booleanp_values —two-sided, Student t on dof = n - pci matrixa (p, 2) matrix at level=, default 0.95bootstrap=N —with seed= | Returns a Model (kind "curve_fit") with fields params (a length-P vector), cost (a number, residual sum of squares), converged (a boolean). Nonlinear least-squares curve fitting via Levenberg-Marquardt (the Jacobian is numerical, forward differences): finds params minimizing sum((model(x_i, params) - y_i)^2). Every fit also carries its parameter uncertainty (*added in v0.4.4*): stderr, cov, correlation, t, p_values (two-sided, Student t on dof = n - p), ci (a (p, 2) matrix at level=, default 0.95), dof, rmse, sigma, and note -- which says when there are no degrees of freedom (the statistics are then NaN) or a parameter is not identifiable from the data (stderr = inf). These are linearized at the solution (cov = s^2 (J^T J)^+): exact for a model linear in its parameters with independent normal errors, an approximation otherwise. bootstrap=N (with seed=) refits N residual resamples and adds bootstrap_stderr/bootstrap_ci -- when the two disagree, distrust the linearization. method= picks the solver: "lm" (Levenberg-Marquardt, default), "gauss_newton" (with step halving) or "gradient_descent" (backtracking; slow, and says converged = false rather than pretending). summary(fit) prints the table. r_squared is included. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
model(x, p) := p[0] * exp(-p[1] * x)
fit = curve_fit("model", xdata, ydata, [1, 1]) # initial guess [1, 1]
print("a = {fit.params[0]}, b = {fit.params[1]}, cost = {fit.cost}")
from numerics-dsp
model(x, p) := p[0] * exp(-p[1] * x)
xdata = linspace(0, 5, 50)
ydata = 3 * exp(-0.7 * xdata) + 0.02 * randn(50, seed=1) # noisy samples of a=3, b=0.7
fit = curve_fit("model", xdata, ydata, [1, 1]) # initial guess [1, 1]
print("a = {fit.params[0]}, b = {fit.params[1]}, cost = {fit.cost}")
a = 2.990869, b = 0.702002, cost = 0.018922
from signal-processing
How it works
curve_fit(model, xdata, ydata, p0, [max_iter=200], [tol=1e-10]) — nonlinear least-squares curve fitting via Levenberg-Marquardt: finds params minimizing sum((model(x_i, params) - y_i)^2). model takes a scalar x and a params vector; the Jacobian is computed numerically (forward differences), since Qu has no automatic differentiation. Model kind "curve_fit", fields params, cost (the residual sum of squares at the returned parameters), and converged. Also [method="lm"|"gauss_newton"|"gradient_descent"], [level=0.95], [bootstrap=0], [seed=]: every fit carries its linearized parameter uncertainty (stderr, cov, correlation, t, p_values, ci, dof, rmse, sigma, r_squared, note, ...) and, with bootstrap=N, a residual bootstrap (bootstrap_stderr, bootstrap_ci). See
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.