Browse Source

Neaten up code somewhat; add IMinimizer

Add IMinimizer interface so that same curve fitting approach can be used
for any function minimization algorithm.
pull/173/head
joemoorhouse 13 years ago
parent
commit
9b6771cbef
  1. 1
      src/Numerics/Numerics.csproj
  2. 35
      src/Numerics/Optimization/BrentMinimizer.cs
  3. 72
      src/Numerics/Optimization/IMinimizer.cs
  4. 43
      src/Numerics/Optimization/NonLinearLeastSquaresMinimizer.cs
  5. 64
      src/Numerics/Optimization/PowellMinimizer.cs
  6. 8
      src/UnitTests/OptimizationTests/FunctionMinimizationTests.cs
  7. 8
      src/UnitTests/OptimizationTests/NonLinearLeastSquaresTest.cs

1
src/Numerics/Numerics.csproj

@ -88,6 +88,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Optimization\BrentMinimizer.cs" />
<Compile Include="Optimization\IMinimizer.cs" />
<Compile Include="Optimization\NonLinearLeastSquaresMinimizer.cs" />
<Compile Include="Optimization\PowellMinimizer.cs" />
<Compile Include="Precision.Comparison.cs" />

35
src/Numerics/Optimization/BrentMinimizer.cs

@ -1,10 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// <copyright file="BrentMinimizer.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2010 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
namespace MathNet.Numerics.Optimization
{
using System;
/// <summary>
/// Options for Brent Minimization.
/// </summary>

72
src/Numerics/Optimization/IMinimizer.cs

@ -0,0 +1,72 @@
// <copyright file="IMinimizer.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2010 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
namespace MathNet.Numerics.Optimization
{
using System;
/// <summary>
/// Interface implemented by a class that minimizes f(p) where p is a vector of model parameters.
/// A class implenting this interface can then be used to solve curve fitting problems.
/// </summary>
public interface IMinimizer
{
double[] Minimize(Func<double[], double> function, double[] pInitialGuess);
}
public static class NonLinearCurveFitExtensions
{
/// <summary>
/// Non-Linear Least-Squares fitting the points (x,y) to a specified function of y : x -> f(x, p), p being a vector of parameters.
/// returning its best fitting parameters p.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="f"></param>
/// <param name="pStart">Initial guess of parameters, p.</param>
/// <returns></returns>
public static double[] CurveFit(this IMinimizer minimizer, double[] x, double[] y, Func<double, double[], double> f,
double[] pStart)
{
// Need to minimize sum of squares of residuals; create this function:
Func<double[], double> function = (p) =>
{
double sum = 0;
for (int i = 0; i < x.Length; ++i)
{
double temp = y[i] - f(x[i], p);
sum += temp * temp;
}
return sum;
};
return minimizer.Minimize(function, pStart);
}
}
}

43
src/Numerics/Optimization/NonLinearLeastSquaresMinimizer.cs

@ -1,12 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MathNet.Numerics.Providers.Optimization;
using MathNet.Numerics.Providers.Optimization.Mkl;
// <copyright file="NonLinearLeastSquaresMinimizer.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2010 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
namespace MathNet.Numerics.Optimization
{
using System;
using MathNet.Numerics.Providers.Optimization;
using MathNet.Numerics.Providers.Optimization.Mkl;
/// <summary>
/// Options for Non-Linear Least Squares Minimization.
/// </summary>
@ -66,9 +93,9 @@ namespace MathNet.Numerics.Optimization
/// <summary>
/// This class is a special function minimizer that minimizes functions of the form
/// f(p) = |r(p)|^2 where r is a vector of residuals and p is a vector of model parameters.
/// f(p) = |r(p)|^2 where r is a vector of residuals and p is a vector of model parameters.
/// </summary>
public class NonLinearLeastSquaresMinimizer
public class NonLinearLeastSquaresMinimizer // Note does not implement IMinimizer, since it curve fitting problems can be solved more efficiently.
{
public NonLinearLeastSquaresResult Result { get; private set; }

64
src/Numerics/Optimization/PowellMinimizer.cs

@ -1,10 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// <copyright file="PowellMinimizer.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2010 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
namespace MathNet.Numerics.Optimization
{
using System;
using System.Linq;
/// <summary>
/// Options for Powell Minimization.
/// </summary>
@ -33,38 +61,12 @@ namespace MathNet.Numerics.Optimization
/// <summary>
/// Minimizes f(p) where p is a vector of model parameters using the Powell method.
/// </summary>
public class PowellMinimizer
public class PowellMinimizer : IMinimizer
{
public PowellResult Result { get; private set; }
public readonly PowellOptions Options = new PowellOptions();
/// <summary>
/// Non-Linear Least-Squares fitting the points (x,y) to a specified function of y : x -> f(x, p), p being a vector of parameters.
/// returning its best fitting parameters p.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="f"></param>
/// <param name="pStart">Initial guess of parameters, p.</param>
/// <returns></returns>
public double[] CurveFit(double[] x, double[] y, Func<double, double[], double> f,
double[] pStart)
{
// Need to minimize sum of squares of residuals; create this function:
Func<double[], double> function = (p) =>
{
double sum = 0;
for (int i = 0; i < x.Length; ++i)
{
double temp = y[i] - f(x[i], p);
sum += temp * temp;
}
return sum;
};
return Minimize(function, pStart);
}
/// <summary>
/// Find the minimum of the supplied function using the Powell method.
/// </summary>

8
src/UnitTests/OptimizationTests/FunctionMinimizationTests.cs

@ -28,12 +28,12 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System;
using MathNet.Numerics.Optimization;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.OptimizationTests
{
using System;
using MathNet.Numerics.Optimization;
using NUnit.Framework;
[TestFixture]
public class FunctionMinimizationTests
{

8
src/UnitTests/OptimizationTests/NonLinearLeastSquaresTest.cs

@ -28,12 +28,12 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System;
using MathNet.Numerics.Optimization;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.OptimizationTests
{
using System;
using MathNet.Numerics.Optimization;
using NUnit.Framework;
[TestFixture]
public class NonLinearLeastSquaresTest
{

Loading…
Cancel
Save