Security News
The Dark Side of Open Source
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
ml-levenberg-marquardt
Advanced tools
Readme
Curve fitting method in javascript.
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. and http://people.duke.edu/~hpgavin/ce281/lm.pdf
In order to get a general idea of the problem you could also check the Wikipedia article.
$ npm i ml-levenberg-marquardt
Next there is some options could change the behavior of the code.
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.
The jacobian matrix is approximated as mention above, the gradientDifference option is the step size (dp) to calculate de 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
import { levenbergMarquardt } from 'ml-levenberg-marquardt';
// const { levenbergMarquardt } = require("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
// }
// import library
import LM from 'ml-levenberg-marquardt';
// const LM = require('ml-levenberg-marquardt').default;
// 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
let data = {
x: [
/* x1, x2, ... */
],
y: [
/* y1, y2, ... */
],
};
// array of initial parameter values (must be provided)
let initialValues = [
/* a, b, c, ... */
];
// Optionally, restrict parameters to minimum & maximum values
let minValues = [
/* a_min, b_min, c_min, ... */
];
let maxValues = [
/* a_max, b_max, c_max, ... */
];
const options = {
damping: 1.5,
initialValues: initialValues,
minValues: minValues,
maxValues: maxValues,
gradientDifference: 10e-2,
maxIterations: 100,
errorTolerance: 10e-3,
};
let fittedParams = LM(data, sinFunction, options);
Or test it in Runkit
FAQs
Curve fitting method in javascript
The npm package ml-levenberg-marquardt receives a total of 10,649 weekly downloads. As such, ml-levenberg-marquardt popularity was classified as popular.
We found that ml-levenberg-marquardt demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 7 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
Research
Security News
The Socket Research team found this npm package includes code for collecting sensitive developer information, including your operating system username, Git username, and Git email.
Security News
OpenJS is warning of social engineering takeovers targeting open source projects after receiving a credible attempt on the foundation.