Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example does not converge on RunKit #40

Open
carstenschwede opened this issue Jun 24, 2021 · 4 comments
Open

Example does not converge on RunKit #40

carstenschwede opened this issue Jun 24, 2021 · 4 comments

Comments

@carstenschwede
Copy link

carstenschwede commented Jun 24, 2021

Running the included example on RunKit only converges in ~15% of cases and depends strongly on the chosen initial values.

const LM = require("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);
}

let data = {
  x: [],
  y: []
};
let trueParams = [2, 2];
let sampleFunction = sinFunction(trueParams);

let numSamples = 1000;

for (let i = 0; i < numSamples; i++) {
  let x = i / numSamples * 2 * Math.PI;
  data.x[i] = x;
  data.y[i] = sampleFunction(x);
}

let success = 0;
let counter = 0;

let maxSampleRuns = 250;
while (maxSampleRuns--) {
  let initialValues = [Math.random() - 0.5, Math.random() - 0.5];

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

  let fittedParams = LM(data, sinFunction, options);
  if (fittedParams.parameterError < 0.01) {
    success++;
  }
  counter++;
  if (counter % 10 === 0) {
    console.log(`Convergence Rate ${(100*success/counter).toFixed(1)}%`);
  }
}

results in

Convergence Rate 10.0%
Convergence Rate 7.5%
Convergence Rate 12.9%
Convergence Rate 12.5%
...
Convergence Rate 14.8%
Convergence Rate 15.0%
@jobo322
Copy link
Member

jobo322 commented Jun 24, 2021

Thanks, as soon as possible I will have a look at this.

@carstenschwede
Copy link
Author

Convergence is very susceptible to initialValues, please see my edit.

@jobo322
Copy link
Member

jobo322 commented Oct 6, 2021

Hello @carstenschwede do you have the way to keep the initial values for which the algorithm converges?

@carstenschwede
Copy link
Author

Please see my code, it runs 250 iterations with random initial values in each, in about 15% of cases the random initial values lead to convergence.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants