Browse Source

Fitting: faster algorithm for fitting to a line

v2
Christoph Ruegg 13 years ago
parent
commit
324082c90d
  1. 31
      src/Numerics/Fit.cs

31
src/Numerics/Fit.cs

@ -33,7 +33,6 @@ using System.Linq;
using MathNet.Numerics.LinearAlgebra.Double; using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.LinearAlgebra.Generic.Factorization; using MathNet.Numerics.LinearAlgebra.Generic.Factorization;
using MathNet.Numerics.Properties; using MathNet.Numerics.Properties;
using MathNet.Numerics.Statistics;
namespace MathNet.Numerics namespace MathNet.Numerics
{ {
@ -53,22 +52,28 @@ namespace MathNet.Numerics
if (x.Length != y.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength); if (x.Length != y.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength);
if (x.Length <= 1) throw new ArgumentException(string.Format(Resources.ArrayTooSmall, 2)); if (x.Length <= 1) throw new ArgumentException(string.Format(Resources.ArrayTooSmall, 2));
var mx = ArrayStatistics.Mean(x); // First Pass: Mean (Less robust but faster than ArrayStatistics.Mean)
var my = ArrayStatistics.Mean(y); double mx = 0.0;
double my = 0.0;
double xsum = x[0]; for (int i = 0; i < x.Length; i++)
double xvariance = 0;
double covariance = (x[0] - mx)*(y[0] - my);
for (int i = 1; i < x.Length; i++)
{ {
covariance += (x[i] - mx)*(y[i] - my); mx += x[i];
my += y[i];
}
mx /= x.Length;
my /= y.Length;
xsum += x[i]; // Second Pass: Covariance/Variance
double diff = (i + 1)*x[i] - xsum; double covariance = 0.0;
xvariance += (diff*diff)/((i + 1)*i); double variance = 0.0;
for (int i = 0; i < x.Length; i++)
{
double diff = x[i] - mx;
covariance += diff*(y[i] - my);
variance += diff*diff;
} }
var b = covariance/xvariance; var b = covariance/variance;
return new[] {my - b*mx, b}; return new[] {my - b*mx, b};
// General Solution: // General Solution:

Loading…
Cancel
Save