Skip to content

opt

tangent/opt validated against scipy.optimize

Numerical optimization: function minimization, curve fitting, and root finding. Derivative-free and gradient-based methods share one declarative interface, and gradients are optional (supply them, return them from the objective, or let finite differences fill in).

Terminal window
npm install @tangent.to/opt # npm
deno add jsr:@tangent/opt # Deno / JSR
Run the example notebook

Minimization

The declarative entry point takes a single spec object and dispatches by method. L-BFGS is the default for smooth problems; Nelder-Mead needs no gradient. An objective may return a number, { loss, gradient }, or { value, gradient } as grad’s valueAndGrad and compile do, so one of those is an objective as it stands.

SignatureDescription
minimize({ f, x0, method, grad?, bounds? })Minimize a scalar function of several variables. Returns { x, fx, converged, nfev, iterations }.
lbfgs(f, x0, options?)Limited-memory quasi-Newton with a strong-Wolfe line search.
nelderMead(f, x0, options?)Derivative-free downhill simplex.
gradientDescent(f, x0, options?)Gradient descent with optional backtracking line search.
adam(f, x0, options?)Adam, for stochastic or noisy objectives.
adamStep(x, gradient, state, options)One Adam update in place, for a loop that owns its own sampling; state is created on the first call. Also gradientStep, momentumStep, rmspropStep. The drivers above are built from these.
methods()List the available method names.

Scalar problems

SignatureDescription
minimizeScalar(f, options?)Brent minimization on a one-dimensional function, with automatic bracketing.
rootScalar(f, options?)Brent-Dekker root finding on a bracketed sign change.

Curve fitting and least squares

SignatureDescription
curveFit({ f, xdata, ydata, p0, bounds?, loss? })Fit a model to data. Returns parameters, covariance and standard errors. Robust losses (huber, soft_l1, cauchy) resist outliers.
leastSquares({ residual, x0, jac?, bounds? })Nonlinear least squares (Levenberg-Marquardt).

Gradients and bounds

SignatureDescription
numericalGradient(f, x, h?)Central finite-difference gradient.
numericalJacobian(f, x, h?)Central finite-difference Jacobian.
numericalHessian(f, x, h?)Central finite-difference Hessian.
makeBoundsTransform(bounds)Box-bounds transform shared by every method.

Verified against scipy

The comparison suite runs every method against scipy.optimize on shared problems: L-BFGS matches L-BFGS-B to within its own tolerance, curveFit recovers parameters and standard errors that agree with curve_fit, and rootScalar matches brentq to machine precision. Under three gross outliers a plain fit’s slope collapses while the Huber-robust fit holds it near the true value.