# ml-levenberg-marquardt

> Curve fitting method in javascript

Latest version **5.1.0** (published 2026-07-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install ml-levenberg-marquardt
pnpm add ml-levenberg-marquardt
yarn add ml-levenberg-marquardt
bun add ml-levenberg-marquardt
```

## Health

**Score 68/100 (B)** — status: active.

Positive: has types package; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 5.1.0 |
| Published | 2026-07-31 |
| First published | 2017-02-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/ml-levenberg-marquardt) |
| Module format | ESM |
| Dependencies | 2 |
| Unpacked size | 61.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 79 |
| Author | Miguel Asencio |
| Maintainers | stropitek, targos, lpatiny, mljs-bot, maasencioh, jeffersonh44, andcastillo |
| Keywords | machine, learning, data, mining, datamining, levenberg, marquardt |

## Links

- npm: https://www.npmjs.com/package/ml-levenberg-marquardt
- Repository: https://github.com/mljs/levenberg-marquardt
- Homepage: https://github.com/mljs/levenberg-marquardt#readme
- Issues: https://github.com/mljs/levenberg-marquardt/issues
- npm.io page: https://npm.io/package/ml-levenberg-marquardt

## Dependencies (2)

- [ml-matrix](https://npm.io/package/ml-matrix.md) ^6.14.0
- [is-any-array](https://npm.io/package/is-any-array.md) ^3.0.0

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 5.1.0 (latest) — 2026-07-31
- 5.0.1 — 2026-04-16
- 5.0.0 — 2025-06-13
- 4.1.3 — 2023-09-30
- 4.1.2 — 2023-04-11
- 4.1.1 — 2023-04-11
- 4.1.0 — 2022-02-28
- 4.0.0 — 2022-01-21
- 3.1.1 — 2021-03-24
- 3.1.0 — 2020-12-20
- 3.0.1 — 2020-12-03
- 3.0.0 — 2020-12-03
- 2.1.1 — 2020-03-16
- 2.1.0 — 2020-03-03
- 2.0.0 — 2019-06-29
- … 5 more at https://npm.io/package/ml-levenberg-marquardt/versions

## README

# ml-levenberg-marquardt

[![NPM version](https://img.shields.io/npm/v/ml-levenberg-marquardt.svg)](https://www.npmjs.com/package/ml-levenberg-marquardt)
[![npm download](https://img.shields.io/npm/dm/ml-levenberg-marquardt.svg)](https://www.npmjs.com/package/ml-levenberg-marquardt)
[![test coverage](https://img.shields.io/codecov/c/github/mljs/levenberg-marquardt.svg)](https://codecov.io/gh/mljs/levenberg-marquardt)
[![license](https://img.shields.io/npm/l/ml-levenberg-marquardt.svg)](https://github.com/mljs/levenberg-marquardt/blob/main/LICENSE)

Curve fitting method in javascript.

## [API Documentation](https://mljs.github.io/levenberg-marquardt/)

This algorithm is based on the article [Brown, Kenneth M., and J. E. Dennis. "Derivative free analogues of the Levenberg-Marquardt and Gauss algorithms for nonlinear least squares approximation." Numerische Mathematik 18.4 (1971): 289-297.](https://doi.org/10.1007/BF01404679) and [http://people.duke.edu/~hpgavin/ce281/lm.pdf](http://people.duke.edu/~hpgavin/ce281/lm.pdf)

To get a general idea of the problem, you could also check the [Wikipedia article](https://en.wikipedia.org/wiki/Levenberg%E2%80%93Marquardt_algorithm).

## Installation

```console
npm i ml-levenberg-marquardt
```

## Usage

```js
import { levenbergMarquardt } from 'ml-levenberg-marquardt';

const result = levenbergMarquardt(data, parameterizedFunction, options);
```

- `data` — an object `{ x, y }` where `x` and `y` are arrays (or typed arrays) of the same length.
- `parameterizedFunction` — takes an array of parameters and returns a function of the independent variable.
- `options` — see below. `initialValues` is mandatory.

The returned object has `parameterValues` (the fitted parameters), `parameterError` (the sum of squared weighted residuals) and `iterations` (the number of iterations performed).

## Options

| Option                 | Default | Description                                                                                                       |
| ---------------------- | ------- | ----------------------------------------------------------------------------------------------------------------- |
| `initialValues`        | —       | Array of initial parameter values. Mandatory.                                                                     |
| `weights`              | `1`     | Weighting vector. If its length does not match the number of data points, it is rebuilt from the first value.     |
| `damping`              | `1e-2`  | Levenberg-Marquardt parameter λ; small values give a Gauss-Newton update, large values a gradient descent update. |
| `dampingStepDown`      | `9`     | Factor used to reduce the damping when an update improves the fit.                                                |
| `dampingStepUp`        | `11`    | Factor used to increase the damping when an update does not improve the fit.                                      |
| `improvementThreshold` | `1e-3`  | Threshold defining what counts as an improvement.                                                                 |
| `gradientDifference`   | `10e-2` | Step size used to approximate the jacobian. See below.                                                            |
| `centralDifference`    | `false` | Approximate the jacobian by central differences instead of forward differences. See below.                        |
| `jacobianFunction`     | —       | Analytical jacobian of the model. See below.                                                                      |
| `minValues`            | —       | Minimum allowed values for the parameters.                                                                        |
| `maxValues`            | —       | Maximum allowed values for the parameters.                                                                        |
| `maxIterations`        | `100`   | Maximum number of iterations.                                                                                     |
| `errorTolerance`       | `10e-3` | Stop as soon as the error drops below this value.                                                                 |
| `timeout`              | —       | Maximum running time in seconds; throws when exceeded.                                                            |

### centralDifference

The jacobian matrix is approximated by finite difference; forward differences or central differences (one additional function evaluation). The option centralDifference select one of them, by default the jacobian is calculated by forward difference.

### gradientDifference

The jacobian matrix is approximated as mentioned above, the gradientDifference option is the step size (dp) to calculate the difference between the function with the current parameter state and the perturbation added. It could be a number (same step size for all parameters) or an array with different values for each parameter, if the gradientDifference is zero, the derive will be zero, and the parameter will hold fixed

### jacobianFunction

Instead of approximating the jacobian by finite differences, you can provide it analytically. Like `parameterizedFunction`, it takes the parameter array and returns a function of the independent variable, but that function returns the partial derivatives of the model with respect to every parameter, in the same order as the parameters.

Providing it avoids the extra model evaluation per parameter and is more accurate, so the fit usually converges in fewer iterations. When it is set, `centralDifference` and `gradientDifference` are ignored.

```js
import { levenbergMarquardt } from 'ml-levenberg-marquardt';

// y = slope * x + intercept
function line([slope, intercept]) {
  return (x) => slope * x + intercept;
}

// [dy/dslope, dy/dintercept]
function lineJacobian() {
  return (x) => [x, 1];
}

const x = [0, 1, 2, 3, 4, 5, 6];
const y = [-2, 0, 2, 4, 6, 8, 10];

const result = levenbergMarquardt({ x, y }, line, {
  initialValues: [1, 0],
  jacobianFunction: lineJacobian,
});
console.log(result);
// {
//   parameterValues: [1.9999986750084098, -1.9999943899435104],
//   parameterError: 6.78713215849927e-11,
//   iterations: 2
// }
```

## Examples

### Linear regression

```js
import { levenbergMarquardt } from 'ml-levenberg-marquardt';

// Creates linear function using the provided slope and intercept parameters
function line([slope, intercept]) {
  return (x) => slope * x + intercept;
}

// Input points (x,y)
const x = [0, 1, 2, 3, 4, 5, 6];
const y = [-2, 0, 2, 4, 6, 8, 10];

// Parameter values to use for first iteration
const initialValues = [1, 0]; // i.e., y = x

const result = levenbergMarquardt({ x, y }, line, { initialValues });
console.log(result);
// {
//   parameterValues: [1.9999986750084096, -1.9999943899435104]
//   parameterError: 6.787132159723697e-11
//   iterations: 2
// }
```

### Sine fit

```js
import { levenbergMarquardt } from 'ml-levenberg-marquardt';

// function that receives the parameters and returns
// a function with the independent variable as a parameter
function sinFunction([a, b]) {
  return (t) => a * Math.sin(b * t);
}

// array of points to fit
const data = {
  x: [/* x1, x2, ... */],
  y: [/* y1, y2, ... */],
};

// array of initial parameter values (must be provided)
const initialValues = [/* a, b, c, ... */];

// Optionally, restrict parameters to minimum & maximum values
const minValues = [/* a_min, b_min, c_min, ... */];
const maxValues = [/* a_max, b_max, c_max, ... */];

const options = {
  damping: 1.5,
  initialValues,
  minValues,
  maxValues,
  gradientDifference: 10e-2,
  maxIterations: 100,
  errorTolerance: 10e-3,
};

const result = levenbergMarquardt(data, sinFunction, options);
```

## License

[MIT](./LICENSE)

---
_Source: https://npm.io/package/ml-levenberg-marquardt · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
