Browse Source

Fitting: rename from LeastSquares to Fit for discoverability

v2
Christoph Ruegg 13 years ago
parent
commit
a7e247ecd4
  1. 2
      src/FSharp/FSharp.fsproj
  2. 12
      src/FSharp/Fit.fs
  3. 4
      src/FSharpPortable/FSharpPortable.fsproj
  4. 6
      src/FSharpUnitTests/CurveFittingTests.fs
  5. 25
      src/Numerics/Fit.cs
  6. 2
      src/Numerics/Numerics.csproj
  7. 6
      src/Portable/Portable.csproj
  8. 26
      src/UnitTests/FitTests.cs
  9. 2
      src/UnitTests/UnitTests.csproj

2
src/FSharp/FSharp.fsproj

@ -71,7 +71,7 @@
<Compile Include="BigIntegerExtensions.fs" />
<Compile Include="BigRational.fsi" />
<Compile Include="BigRational.fs" />
<Compile Include="LeastSquares.fs" />
<Compile Include="Fit.fs" />
<Compile Include="RandomVariable.fs" />
</ItemGroup>
<ItemGroup>

12
src/FSharp/LeastSquares.fs → src/FSharp/Fit.fs

@ -1,4 +1,4 @@
// <copyright file="LeastSquares.fs" company="Math.NET">
// <copyright file="Fit.fs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -39,11 +39,11 @@ module Fit =
let private tofs (f:Func<_,_>) = fun a -> f.Invoke(a)
let line x y = let p = LeastSquares.FitToLine(x,y) in (p.[0],p.[1])
let linef x y = LeastSquares.FitToLineFunc(x,y) |> tofs
let line x y = let p = Fit.Line(x,y) in (p.[0],p.[1])
let lineF x y = Fit.LineFunc(x,y) |> tofs
let polynomial order x y = LeastSquares.FitToPolynomial(x,y,order)
let polynomialf order x y = LeastSquares.FitToPolynomialFunc(x,y,order) |> tofs
let polynomial order x y = Fit.Polynomial(x,y,order)
let polynomialF order x y = Fit.PolynomialFunc(x,y,order) |> tofs
let linear functions (x:float[]) (y:float[]) =
functions
@ -51,6 +51,6 @@ module Fit =
|> DenseMatrix.ofColumnsList (Array.length x) (List.length functions)
|> fun m -> m.QR(QRMethod.Thin).Solve(DenseVector(y)).ToArray()
|> List.ofArray
let linearf functions x y =
let linearF functions x y =
let parts = linear functions x y |> List.zip functions
in fun z -> parts |> List.fold (fun s (f,p) -> s+p*(f z)) 0.0

4
src/FSharpPortable/FSharpPortable.fsproj

@ -77,8 +77,8 @@
<Compile Include="..\FSharp\BigRational.fs">
<Link>BigRational.fs</Link>
</Compile>
<Compile Include="..\FSharp\LeastSquares.fs">
<Link>LeastSquares.fs</Link>
<Compile Include="..\FSharp\Fit.fs">
<Link>Fit.fs</Link>
</Compile>
<Compile Include="..\FSharp\RandomVariable.fs">
<Link>RandomVariable.fs</Link>

6
src/FSharpUnitTests/CurveFittingTests.fs

@ -7,8 +7,6 @@ open FsUnit
module CurveFittingTests =
let tofs (f:Func<_,_>) = fun a -> f.Invoke(a)
[<Test>]
let ``When fitting to an exact line should return exact parameters``() =
let f z = 4.0 - 1.5*z
@ -20,7 +18,7 @@ module CurveFittingTests =
a |> should (equalWithin 1.0e-12) 4.0
b |> should (equalWithin 1.0e-12) -1.5
let fres = LeastSquares.FitToLineFunc(x,y) |> tofs
let fres = Fit.lineF x y
in x |> Array.iter (fun x -> fres x |> should (equalWithin 1.0e-12) (f x))
[<Test>]
@ -36,5 +34,5 @@ module CurveFittingTests =
(x,y) ||> Fit.linear [(fun _ -> 1.0); (Math.Sin); (Math.Cos) ]
|> should (equalWithin 1.0e-4) [ -0.287476; 4.02159; -1.46962 ]
let fres = LeastSquares.FitToLinearCombinationFunc(x, y, (fun z -> 1.0), (fun z -> Math.Sin(z)), (fun z -> Math.Cos(z))) |> tofs
let fres = Fit.linearF [(fun z -> 1.0); (fun z -> Math.Sin(z)); (fun z -> Math.Cos(z))] x y
in x |> Array.iter (fun x -> fres x |> should (equalWithin 1.0e-4) (4.02159*Math.Sin(x) - 1.46962*Math.Cos(x) - 0.287476))

25
src/Numerics/LeastSquares.cs → src/Numerics/Fit.cs

@ -1,4 +1,4 @@
// <copyright file="LeastSquares.cs" company="Math.NET">
// <copyright file="Fit.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -35,13 +35,16 @@ using MathNet.Numerics.LinearAlgebra.Generic.Factorization;
namespace MathNet.Numerics
{
public static class LeastSquares
/// <summary>
/// Least-Squares Curve Fitting Routines
/// </summary>
public static class Fit
{
/// <summary>
/// Least-Squares fitting the points (x,y) to a line y : x -> a+b*x,
/// returning its best fitting parameters as [a, b] array.
/// </summary>
public static double[] FitToLine(double[] x, double[] y)
public static double[] Line(double[] x, double[] y)
{
// TODO: we should use a direct algorithm instead (PERF)
return DenseMatrix
@ -54,9 +57,9 @@ namespace MathNet.Numerics
/// Least-Squares fitting the points (x,y) to a line y : x -> a+b*x,
/// returning a function y' for the best fitting line.
/// </summary>
public static Func<double, double> FitToLineFunc(double[] x, double[] y)
public static Func<double, double> LineFunc(double[] x, double[] y)
{
var parameters = FitToLine(x, y);
var parameters = Line(x, y);
double a = parameters[0], b = parameters[1];
return z => a + b*z;
}
@ -65,7 +68,7 @@ namespace MathNet.Numerics
/// Least-Squares fitting the points (x,y) to a k-order polynomial y : x -> p0 + p1*x + p2*x^2 + ... + pk*x^k,
/// returning its best fitting parameters as [p0, p1, p2, ..., pk] array, compatible with Evaluate.Polynomial.
/// </summary>
public static double[] FitToPolynomial(double[] x, double[] y, int order)
public static double[] Polynomial(double[] x, double[] y, int order)
{
// TODO: consider to use a specific algorithm instead
return DenseMatrix
@ -78,9 +81,9 @@ namespace MathNet.Numerics
/// Least-Squares fitting the points (x,y) to a k-order polynomial y : x -> p0 + p1*x + p2*x^2 + ... + pk*x^k,
/// returning a function y' for the best fitting polynomial.
/// </summary>
public static Func<double, double> FitToPolynomialFunc(double[] x, double[] y, int order)
public static Func<double, double> PolynomialFunc(double[] x, double[] y, int order)
{
var parameters = FitToPolynomial(x, y, order);
var parameters = Polynomial(x, y, order);
return z => Evaluate.Polynomial(z, parameters);
}
@ -88,7 +91,7 @@ namespace MathNet.Numerics
/// Least-Squares fitting the points (x,y) to an arbitrary linear combination y : x -> p0*f0(x) + p1*f1(x) + ... + pk*fk(x),
/// returning its best fitting parameters as [p0, p1, p2, ..., pk] array.
/// </summary>
public static double[] FitToLinearCombination(double[] x, double[] y, params Func<double,double>[] functions)
public static double[] LinearCombination(double[] x, double[] y, params Func<double,double>[] functions)
{
// TODO: consider to use a specific algorithm instead
return DenseMatrix
@ -101,9 +104,9 @@ namespace MathNet.Numerics
/// LLeast-Squares fitting the points (x,y) to an arbitrary linear combination y : x -> p0*f0(x) + p1*f1(x) + ... + pk*fk(x),
/// returning a function y' for the best fitting combination.
/// </summary>
public static Func<double, double> FitToLinearCombinationFunc(double[] x, double[] y, params Func<double, double>[] functions)
public static Func<double, double> LinearCombinationFunc(double[] x, double[] y, params Func<double, double>[] functions)
{
var parameters = FitToLinearCombination(x, y, functions);
var parameters = LinearCombination(x, y, functions);
return z => functions.Zip(parameters, (f, p) => p*f(z)).Sum();
}
}

2
src/Numerics/Numerics.csproj

@ -105,7 +105,7 @@
<Compile Include="Constants.cs" />
<Compile Include="Control.cs" />
<Compile Include="Complex32.cs" />
<Compile Include="LeastSquares.cs" />
<Compile Include="Fit.cs" />
<Compile Include="Financial\AbsoluteReturnMeasures.cs" />
<Compile Include="Financial\AbsoluteRiskMeasures.cs" />
<Compile Include="LinearAlgebra\Generic\Matrix.BCL.cs" />

6
src/Portable/Portable.csproj

@ -198,6 +198,9 @@
<Compile Include="..\Numerics\Financial\AbsoluteRiskMeasures.cs">
<Link>Financial\AbsoluteRiskMeasures.cs</Link>
</Compile>
<Compile Include="..\Numerics\Fit.cs">
<Link>Fit.cs</Link>
</Compile>
<Compile Include="..\Numerics\GlobalizationHelper.cs">
<Link>GlobalizationHelper.cs</Link>
</Compile>
@ -282,9 +285,6 @@
<Compile Include="..\Numerics\IPrecisionSupport.cs">
<Link>IPrecisionSupport.cs</Link>
</Compile>
<Compile Include="..\Numerics\LeastSquares.cs">
<Link>LeastSquares.cs</Link>
</Compile>
<Compile Include="..\Numerics\LinearAlgebra\Complex32\DenseMatrix.cs">
<Link>LinearAlgebra\Complex32\DenseMatrix.cs</Link>
</Compile>

26
src/UnitTests/LeastSquaresTests.cs → src/UnitTests/FitTests.cs

@ -1,4 +1,4 @@
// <copyright file="LeastSquaresTests.cs" company="Math.NET">
// <copyright file="FitTests.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -36,7 +36,7 @@ using NUnit.Framework;
namespace MathNet.Numerics.UnitTests
{
[TestFixture]
public class LeastSquaresTests
public class FitTests
{
[Test]
public void FitsToExactLineWhenPointsAreOnLine()
@ -44,12 +44,12 @@ namespace MathNet.Numerics.UnitTests
var x = new[] {30.0, 40.0, 50.0, 12.0, -3.4, 100.5};
var y = x.Select(z => 4.0 - 1.5*z).ToArray();
var resp = LeastSquares.FitToLine(x, y);
var resp = Fit.Line(x, y);
Assert.AreEqual(2, resp.Length);
Assert.AreEqual(4.0, resp[0], 1e-12);
Assert.AreEqual(-1.5, resp[1], 1e-12);
var resf = LeastSquares.FitToLineFunc(x, y);
var resf = Fit.LineFunc(x, y);
foreach (var z in Enumerable.Range(-3, 10))
{
Assert.AreEqual(4.0 - 1.5*z, resf(z), 1e-12);
@ -65,12 +65,12 @@ namespace MathNet.Numerics.UnitTests
var x = Enumerable.Range(1, 6).Select(Convert.ToDouble).ToArray();
var y = new[] {4.986, 2.347, 2.061, -2.995, -2.352, -5.782};
var resp = LeastSquares.FitToLine(x, y);
var resp = Fit.Line(x, y);
Assert.AreEqual(2, resp.Length);
Assert.AreEqual(7.01013, resp[0], 1e-4);
Assert.AreEqual(-2.08551, resp[1], 1e-4);
var resf = LeastSquares.FitToLineFunc(x, y);
var resf = Fit.LineFunc(x, y);
foreach (var z in Enumerable.Range(-3, 10))
{
Assert.AreEqual(7.01013 - 2.08551 * z, resf(z), 1e-4);
@ -86,7 +86,7 @@ namespace MathNet.Numerics.UnitTests
var x = Enumerable.Range(1, 6).Select(Convert.ToDouble).ToArray();
var y = new[] { 4.986, 2.347, 2.061, -2.995, -2.352, -5.782 };
var resp = LeastSquares.FitToPolynomial(x, y, 0);
var resp = Fit.Polynomial(x, y, 0);
Assert.AreEqual(1, resp.Length);
Assert.AreEqual(-0.289167, resp[0], 1e-4);
Assert.AreEqual(y.Mean(), resp[0], 1e-4);
@ -101,12 +101,12 @@ namespace MathNet.Numerics.UnitTests
var x = Enumerable.Range(1, 6).Select(Convert.ToDouble).ToArray();
var y = new[] { 4.986, 2.347, 2.061, -2.995, -2.352, -5.782 };
var resp = LeastSquares.FitToPolynomial(x, y, 1);
var resp = Fit.Polynomial(x, y, 1);
Assert.AreEqual(2, resp.Length);
Assert.AreEqual(7.01013, resp[0], 1e-4);
Assert.AreEqual(-2.08551, resp[1], 1e-4);
var resf = LeastSquares.FitToPolynomialFunc(x, y, 1);
var resf = Fit.PolynomialFunc(x, y, 1);
foreach (var z in Enumerable.Range(-3, 10))
{
Assert.AreEqual(7.01013 - 2.08551 * z, resf(z), 1e-4);
@ -122,13 +122,13 @@ namespace MathNet.Numerics.UnitTests
var x = Enumerable.Range(1, 6).Select(Convert.ToDouble).ToArray();
var y = new[] { 4.986, 2.347, 2.061, -2.995, -2.352, -5.782 };
var resp = LeastSquares.FitToPolynomial(x, y, 2);
var resp = Fit.Polynomial(x, y, 2);
Assert.AreEqual(3, resp.Length);
Assert.AreEqual(6.9703, resp[0], 1e-4);
Assert.AreEqual(-2.05564, resp[1], 1e-4);
Assert.AreEqual(-0.00426786, resp[2], 1e-6);
var resf = LeastSquares.FitToPolynomialFunc(x, y, 2);
var resf = Fit.PolynomialFunc(x, y, 2);
foreach (var z in Enumerable.Range(-3, 10))
{
Assert.AreEqual(Evaluate.Polynomial(z, resp), resf(z), 1e-4);
@ -144,13 +144,13 @@ namespace MathNet.Numerics.UnitTests
var x = Enumerable.Range(1, 6).Select(Convert.ToDouble).ToArray();
var y = new[] { 4.986, 2.347, 2.061, -2.995, -2.352, -5.782 };
var resp = LeastSquares.FitToLinearCombination(x, y, z => 1.0, Math.Sin, Math.Cos);
var resp = Fit.LinearCombination(x, y, z => 1.0, Math.Sin, Math.Cos);
Assert.AreEqual(3, resp.Length);
Assert.AreEqual(-0.287476, resp[0], 1e-4);
Assert.AreEqual(4.02159, resp[1], 1e-4);
Assert.AreEqual(-1.46962, resp[2], 1e-4);
var resf = LeastSquares.FitToLinearCombinationFunc(x, y, z => 1.0, Math.Sin, Math.Cos);
var resf = Fit.LinearCombinationFunc(x, y, z => 1.0, Math.Sin, Math.Cos);
foreach (var z in Enumerable.Range(-3, 10))
{
Assert.AreEqual(4.02159*Math.Sin(z) - 1.46962*Math.Cos(z) - 0.287476, resf(z), 1e-4);

2
src/UnitTests/UnitTests.csproj

@ -144,7 +144,7 @@
<Compile Include="InterpolationTests\EquidistantPolynomialTest.cs" />
<Compile Include="InterpolationTests\NevillePolynomialTest.cs" />
<Compile Include="InterpolationTests\LinearInterpolationCase.cs" />
<Compile Include="LeastSquaresTests.cs" />
<Compile Include="FitTests.cs" />
<Compile Include="LinearAlgebraProviderTests\Complex32\LinearAlgebraProviderTests.cs" />
<Compile Include="LinearAlgebraProviderTests\Complex\LinearAlgebraProviderTests.cs" />
<Compile Include="LinearAlgebraProviderTests\Double\LinearAlgebraProviderTests.cs">

Loading…
Cancel
Save