Skip to content

Commit f955d64

Browse files
committed
refactor: Remove least squares cost function using Gauss-Newton approximation
This was a test implementation in the first place, and we are not using it since -- as expected -- it only make sense when initial values are close to the minimum. Moreover, it is easy to achieve the same behavior by just providing an all-zero model Hessian to the normal least squares cost function. The performance difference was negligible for all test cases.
1 parent 9b389d5 commit f955d64

File tree

4 files changed

+0
-589
lines changed

4 files changed

+0
-589
lines changed

minuit2.net/CostFunctions/CostFunction.cs

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -381,68 +381,6 @@ public static ICostFunction LeastSquares(
381381
errorDefinitionInSigma,
382382
false);
383383
}
384-
385-
public static ICostFunction LeastSquaresWithGaussNewtonApproximation(
386-
IReadOnlyList<double> x,
387-
IReadOnlyList<double> y,
388-
IReadOnlyList<string> parameters,
389-
Func<double, IReadOnlyList<double>, double> model,
390-
Func<double, IReadOnlyList<double>, IReadOnlyList<double>> modelGradient,
391-
double errorDefinitionInSigma = 1)
392-
{
393-
DataValidation.EnsureMatchingSizesBetween((x, nameof(x)), (y, nameof(y)));
394-
return new LeastSquaresWithGaussNewtonApproximation(
395-
x,
396-
y,
397-
_ => 1,
398-
parameters,
399-
model,
400-
modelGradient,
401-
errorDefinitionInSigma,
402-
true);
403-
}
404-
405-
public static ICostFunction LeastSquaresWithGaussNewtonApproximation(
406-
IReadOnlyList<double> x,
407-
IReadOnlyList<double> y,
408-
double yError,
409-
IReadOnlyList<string> parameters,
410-
Func<double, IReadOnlyList<double>, double> model,
411-
Func<double, IReadOnlyList<double>, IReadOnlyList<double>> modelGradient,
412-
double errorDefinitionInSigma = 1)
413-
{
414-
DataValidation.EnsureMatchingSizesBetween((x, nameof(x)), (y, nameof(y)));
415-
return new LeastSquaresWithGaussNewtonApproximation(
416-
x,
417-
y,
418-
_ => yError,
419-
parameters,
420-
model,
421-
modelGradient,
422-
errorDefinitionInSigma,
423-
false);
424-
}
425-
426-
public static ICostFunction LeastSquaresWithGaussNewtonApproximation(
427-
IReadOnlyList<double> x,
428-
IReadOnlyList<double> y,
429-
IReadOnlyList<double> yError,
430-
IReadOnlyList<string> parameters,
431-
Func<double, IReadOnlyList<double>, double> model,
432-
Func<double, IReadOnlyList<double>, IReadOnlyList<double>> modelGradient,
433-
double errorDefinitionInSigma = 1)
434-
{
435-
DataValidation.EnsureMatchingSizesBetween((x, nameof(x)), (y, nameof(y)), (yError, nameof(yError)));
436-
return new LeastSquaresWithGaussNewtonApproximation(
437-
x,
438-
y,
439-
i => yError[i],
440-
parameters,
441-
model,
442-
modelGradient,
443-
errorDefinitionInSigma,
444-
false);
445-
}
446384

447385
public static ICostFunction Sum(params ICostFunction[] components) => new CostFunctionSum(components);
448386
}

minuit2.net/CostFunctions/LeastSquaresWithGaussNewtonApproximation.cs

Lines changed: 0 additions & 95 deletions
This file was deleted.

test/ExampleProblems/ConfigurableLeastSquaresProblem.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,10 @@ public class LeastSquaresCostBuilder(
3535
private bool _hasYErrors = true;
3636
private bool _hasGradient;
3737
private bool _hasHessian;
38-
private bool _isUsingGaussNewtonApproximation;
3938
private double _errorDefinitionInSigma = 1;
4039

4140
public ICostFunction Build() => _hasYErrors switch
4241
{
43-
true when _isUsingGaussNewtonApproximation => CostFunction.LeastSquaresWithGaussNewtonApproximation(xValues, yValues, yError, _parameterNames, model, modelGradient, _errorDefinitionInSigma),
44-
false when _isUsingGaussNewtonApproximation => CostFunction.LeastSquaresWithGaussNewtonApproximation(xValues, yValues, _parameterNames, model, modelGradient, _errorDefinitionInSigma),
4542
true when _hasHessian => CostFunction.LeastSquares(xValues, yValues, yError, _parameterNames, model, modelGradient, modelHessian, _errorDefinitionInSigma),
4643
false when _hasHessian => CostFunction.LeastSquares(xValues, yValues, _parameterNames, model, modelGradient, modelHessian, _errorDefinitionInSigma),
4744
true when _hasGradient => CostFunction.LeastSquares(xValues, yValues, yError, _parameterNames, model, modelGradient, _errorDefinitionInSigma),
@@ -68,12 +65,6 @@ public LeastSquaresCostBuilder WithHessian(bool hasHessian = true)
6865
return this;
6966
}
7067

71-
public LeastSquaresCostBuilder UsingGaussNewtonApproximation(bool isUsingGaussNewtonApproximation = true)
72-
{
73-
_isUsingGaussNewtonApproximation = isUsingGaussNewtonApproximation;
74-
return this;
75-
}
76-
7768
public LeastSquaresCostBuilder WithErrorDefinition(double sigma)
7869
{
7970
_errorDefinitionInSigma = sigma;

0 commit comments

Comments
 (0)