-
Notifications
You must be signed in to change notification settings - Fork 905
/
Copy pathFit.cs
476 lines (435 loc) · 25.6 KB
/
Fit.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// <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
//
// Copyright (c) 2009-2018 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>
using System;
using System.Linq;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearRegression;
using MathNet.Numerics.Providers.LinearAlgebra;
namespace MathNet.Numerics
{
/// <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) tuple,
/// where a is the intercept and b the slope.
/// </summary>
public static (double A, double B) Line(double[] x, double[] y)
{
return SimpleRegression.Fit(x, y);
}
/// <summary>
/// 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> LineFunc(double[] x, double[] y)
{
(double intercept, double slope) = SimpleRegression.Fit(x, y);
return z => intercept + slope * z;
}
/// <summary>
/// Least-Squares fitting the points (x,y) to a line through origin y : x -> b*x,
/// returning its best fitting parameter b,
/// where the intercept is zero and b the slope.
/// </summary>
public static double LineThroughOrigin(double[] x, double[] y)
{
return SimpleRegression.FitThroughOrigin(x, y);
}
/// <summary>
/// Least-Squares fitting the points (x,y) to a line through origin y : x -> b*x,
/// returning a function y' for the best fitting line.
/// </summary>
public static Func<double, double> LineThroughOriginFunc(double[] x, double[] y)
{
double slope = SimpleRegression.FitThroughOrigin(x, y);
return z => slope * z;
}
/// <summary>
/// Deming/Orthogonal regression, least-Squares fitting the points in
/// the 2D dataset (x,y) to a line
/// <code>
/// a*x + b*y + c = 0
/// </code>
/// For <paramref name="delta"/> equal 1 (the default value), this is
/// performing orthogonal regression, minimizing the sum of squared
/// perpendicular distances from the data points to the regression line.
/// <para>
/// Orthogonal regression is a special case of Deming regression,
/// and is assuming equal error variances on the x and y data,
/// and applied by the argument <paramref name="delta"/> default value of 1.0.
/// </para>
/// <para>
/// The parameters (a,b,c) are scaled such that a and b
/// in absolute values are always less than one.
/// </para>
/// </summary>
/// <param name="x">X data</param>
/// <param name="y">Y data</param>
/// <param name="delta">Ratio of variances of x and y data, var(y)/var(x). Default value is 1.0.</param>
/// <returns> returning its best fitting parameters as (a, b, c) tuple.</returns>
public static (double A, double B, double C) Line2D(double[] x, double[] y, double delta = 1.0)
{
return DemingRegression.Fit(x, y, delta);
}
/// <summary>
/// Convert line coefficients on the form
/// <code>
/// a*x + b*y + c = 0
/// </code>
/// to coefficients on the form
/// <code>
/// y = ay*x + by
/// </code>
/// If <paramref name="b"/> is zero, the ay will return <see cref="double.PositiveInfinity"/>.
/// </summary>
public static (double ay, double by) StandardLineToYxLine(double a, double b, double c)
{
if (Math.Abs(b) > (Math.Abs(a) + Math.Abs(c)) * 1e-10)
{
double ay = -a / b;
double by = -c / b;
return (ay, by);
}
return (double.PositiveInfinity, -c);
}
/// <summary>
/// Least-Squares fitting the points (x,y) to an exponential y : x -> a*exp(r*x),
/// returning its best fitting parameters as (a, r) tuple.
/// </summary>
public static (double A, double R) Exponential(double[] x, double[] y, DirectRegressionMethod method = DirectRegressionMethod.QR)
{
// Transformation: y_h := ln(y) ~> y_h : x -> ln(a) + r*x;
double[] lny = Generate.Map(y, Math.Log);
double[] p = LinearCombination(x, lny, method, _ => 1.0, t => t);
return (Math.Exp(p[0]), p[1]);
}
/// <summary>
/// Least-Squares fitting the points (x,y) to an exponential y : x -> a*exp(r*x),
/// returning a function y' for the best fitting line.
/// </summary>
public static Func<double, double> ExponentialFunc(double[] x, double[] y, DirectRegressionMethod method = DirectRegressionMethod.QR)
{
(double a, double r) = Exponential(x, y, method);
return z => a * Math.Exp(r * z);
}
/// <summary>
/// Least-Squares fitting the points (x,y) to a logarithm y : x -> a + b*ln(x),
/// returning its best fitting parameters as (a, b) tuple.
/// </summary>
public static (double A, double B) Logarithm(double[] x, double[] y, DirectRegressionMethod method = DirectRegressionMethod.QR)
{
double[] lnx = Generate.Map(x, Math.Log);
double[] p = LinearCombination(lnx, y, method, _ => 1.0, t => t);
return (p[0], p[1]);
}
/// <summary>
/// Least-Squares fitting the points (x,y) to a logarithm y : x -> a + b*ln(x),
/// returning a function y' for the best fitting line.
/// </summary>
public static Func<double, double> LogarithmFunc(double[] x, double[] y, DirectRegressionMethod method = DirectRegressionMethod.QR)
{
(double a, double b) = Logarithm(x, y, method);
return z => a + b * Math.Log(z);
}
/// <summary>
/// Least-Squares fitting the points (x,y) to a power y : x -> a*x^b,
/// returning its best fitting parameters as (a, b) tuple.
/// </summary>
public static (double A, double B) Power(double[] x, double[] y, DirectRegressionMethod method = DirectRegressionMethod.QR)
{
// Transformation: y_h := ln(y) ~> y_h : x -> ln(a) + b*ln(x);
double[] lny = Generate.Map(y, Math.Log);
double[] p = LinearCombination(x, lny, method, _ => 1.0, Math.Log);
return (Math.Exp(p[0]), p[1]);
}
/// <summary>
/// Least-Squares fitting the points (x,y) to a power y : x -> a*x^b,
/// returning a function y' for the best fitting line.
/// </summary>
public static Func<double, double> PowerFunc(double[] x, double[] y, DirectRegressionMethod method = DirectRegressionMethod.QR)
{
(double a, double b) = Power(x, y, method);
return z => a * Math.Pow(z, b);
}
/// <summary>
/// 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 Polynomial.Evaluate.
/// A polynomial with order/degree k has (k+1) coefficients and thus requires at least (k+1) samples.
/// </summary>
public static double[] Polynomial(double[] x, double[] y, int order, DirectRegressionMethod method = DirectRegressionMethod.QR)
{
var design = Matrix<double>.Build.Dense(x.Length, order + 1, (i, j) => Math.Pow(x[i], j));
return MultipleRegression.DirectMethod(design, Vector<double>.Build.Dense(y), method).ToArray();
}
/// <summary>
/// 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.
/// A polynomial with order/degree k has (k+1) coefficients and thus requires at least (k+1) samples.
/// </summary>
public static Func<double, double> PolynomialFunc(double[] x, double[] y, int order, DirectRegressionMethod method = DirectRegressionMethod.QR)
{
var parameters = Polynomial(x, y, order, method);
return z => Numerics.Polynomial.Evaluate(z, parameters);
}
/// <summary>
/// Weighted Least-Squares fitting the points (x,y) and weights w 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 Polynomial.Evaluate.
/// A polynomial with order/degree k has (k+1) coefficients and thus requires at least (k+1) samples.
/// </summary>
public static double[] PolynomialWeighted(double[] x, double[] y, double[] w, int order)
{
var design = Matrix<double>.Build.Dense(x.Length, order + 1, (i, j) => Math.Pow(x[i], j));
return WeightedRegression.Weighted(design, Vector<double>.Build.Dense(y), Matrix<double>.Build.Diagonal(w)).ToArray();
}
/// <summary>
/// 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[] LinearCombination(double[] x, double[] y, params Func<double,double>[] functions)
{
var design = Matrix<double>.Build.Dense(x.Length, functions.Length, (i, j) => functions[j](x[i]));
return MultipleRegression.QR(design, Vector<double>.Build.Dense(y)).ToArray();
}
/// <summary>
/// Least-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> LinearCombinationFunc(double[] x, double[] y, params Func<double, double>[] functions)
{
var parameters = LinearCombination(x, y, functions);
return z => functions.Zip(parameters, (f, p) => p*f(z)).Sum();
}
/// <summary>
/// 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[] LinearCombination(double[] x, double[] y, DirectRegressionMethod method, params Func<double, double>[] functions)
{
var design = Matrix<double>.Build.Dense(x.Length, functions.Length, (i, j) => functions[j](x[i]));
return MultipleRegression.DirectMethod(design, Vector<double>.Build.Dense(y), method).ToArray();
}
/// <summary>
/// Least-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> LinearCombinationFunc(double[] x, double[] y, DirectRegressionMethod method, params Func<double, double>[] functions)
{
var parameters = LinearCombination(x, y, method, functions);
return z => functions.Zip(parameters, (f, p) => p*f(z)).Sum();
}
/// <summary>
/// Least-Squares fitting the points (X,y) = ((x0,x1,..,xk),y) to a linear surface y : X -> p0*x0 + p1*x1 + ... + pk*xk,
/// returning its best fitting parameters as [p0, p1, p2, ..., pk] array.
/// If an intercept is added, its coefficient will be prepended to the resulting parameters.
/// </summary>
public static double[] MultiDim(double[][] x, double[] y, bool intercept = false, DirectRegressionMethod method = DirectRegressionMethod.NormalEquations)
{
return MultipleRegression.DirectMethod(x, y, intercept, method);
}
/// <summary>
/// Least-Squares fitting the points (X,y) = ((x0,x1,..,xk),y) to a linear surface y : X -> p0*x0 + p1*x1 + ... + pk*xk,
/// returning a function y' for the best fitting combination.
/// If an intercept is added, its coefficient will be prepended to the resulting parameters.
/// </summary>
public static Func<double[], double> MultiDimFunc(double[][] x, double[] y, bool intercept = false, DirectRegressionMethod method = DirectRegressionMethod.NormalEquations)
{
var parameters = MultipleRegression.DirectMethod(x, y, intercept, method);
return z => LinearAlgebraControl.Provider.DotProduct(parameters, z);
}
/// <summary>
/// Weighted Least-Squares fitting the points (X,y) = ((x0,x1,..,xk),y) and weights w to a linear surface y : X -> p0*x0 + p1*x1 + ... + pk*xk,
/// returning its best fitting parameters as [p0, p1, p2, ..., pk] array.
/// </summary>
public static double[] MultiDimWeighted(double[][] x, double[] y, double[] w)
{
return WeightedRegression.Weighted(x, y, w);
}
/// <summary>
/// Least-Squares fitting the points (X,y) = ((x0,x1,..,xk),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[] LinearMultiDim(double[][] x, double[] y, params Func<double[], double>[] functions)
{
var design = Matrix<double>.Build.Dense(x.Length, functions.Length, (i, j) => functions[j](x[i]));
return MultipleRegression.QR(design, Vector<double>.Build.Dense(y)).ToArray();
}
/// <summary>
/// Least-Squares fitting the points (X,y) = ((x0,x1,..,xk),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> LinearMultiDimFunc(double[][] x, double[] y, params Func<double[], double>[] functions)
{
var parameters = LinearMultiDim(x, y, functions);
return z => functions.Zip(parameters, (f, p) => p * f(z)).Sum();
}
/// <summary>
/// Least-Squares fitting the points (X,y) = ((x0,x1,..,xk),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[] LinearMultiDim(double[][] x, double[] y, DirectRegressionMethod method, params Func<double[], double>[] functions)
{
var design = Matrix<double>.Build.Dense(x.Length, functions.Length, (i, j) => functions[j](x[i]));
return MultipleRegression.DirectMethod(design, Vector<double>.Build.Dense(y), method).ToArray();
}
/// <summary>
/// Least-Squares fitting the points (X,y) = ((x0,x1,..,xk),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> LinearMultiDimFunc(double[][] x, double[] y, DirectRegressionMethod method, params Func<double[], double>[] functions)
{
var parameters = LinearMultiDim(x, y, method, functions);
return z => functions.Zip(parameters, (f, p) => p * f(z)).Sum();
}
/// <summary>
/// Least-Squares fitting the points (T,y) = (T,y) to an arbitrary linear combination y : X -> p0*f0(T) + p1*f1(T) + ... + pk*fk(T),
/// returning its best fitting parameters as [p0, p1, p2, ..., pk] array.
/// </summary>
public static double[] LinearGeneric<T>(T[] x, double[] y, params Func<T, double>[] functions)
{
var design = Matrix<double>.Build.Dense(x.Length, functions.Length, (i, j) => functions[j](x[i]));
return MultipleRegression.QR(design, Vector<double>.Build.Dense(y)).ToArray();
}
/// <summary>
/// Least-Squares fitting the points (T,y) = (T,y) to an arbitrary linear combination y : X -> p0*f0(T) + p1*f1(T) + ... + pk*fk(T),
/// returning a function y' for the best fitting combination.
/// </summary>
public static Func<T, double> LinearGenericFunc<T>(T[] x, double[] y, params Func<T, double>[] functions)
{
var parameters = LinearGeneric(x, y, functions);
return z => functions.Zip(parameters, (f, p) => p * f(z)).Sum();
}
/// <summary>
/// Least-Squares fitting the points (T,y) = (T,y) to an arbitrary linear combination y : X -> p0*f0(T) + p1*f1(T) + ... + pk*fk(T),
/// returning its best fitting parameters as [p0, p1, p2, ..., pk] array.
/// </summary>
public static double[] LinearGeneric<T>(T[] x, double[] y, DirectRegressionMethod method, params Func<T, double>[] functions)
{
var design = Matrix<double>.Build.Dense(x.Length, functions.Length, (i, j) => functions[j](x[i]));
return MultipleRegression.DirectMethod(design, Vector<double>.Build.Dense(y), method).ToArray();
}
/// <summary>
/// Least-Squares fitting the points (T,y) = (T,y) to an arbitrary linear combination y : X -> p0*f0(T) + p1*f1(T) + ... + pk*fk(T),
/// returning a function y' for the best fitting combination.
/// </summary>
public static Func<T, double> LinearGenericFunc<T>(T[] x, double[] y, DirectRegressionMethod method, params Func<T, double>[] functions)
{
var parameters = LinearGeneric(x, y, method, functions);
return z => functions.Zip(parameters, (f, p) => p * f(z)).Sum();
}
/// <summary>
/// Non-linear least-squares fitting the points (x,y) to an arbitrary function y : x -> f(p, x),
/// returning its best fitting parameter p.
/// </summary>
public static double Curve(double[] x, double[] y, Func<double, double, double> f, double initialGuess, double tolerance = 1e-8, int maxIterations = 1000)
{
return FindMinimum.OfScalarFunction(p => Distance.Euclidean(Generate.Map(x, t => f(p, t)), y), initialGuess, tolerance, maxIterations);
}
/// <summary>
/// Non-linear least-squares fitting the points (x,y) to an arbitrary function y : x -> f(p0, p1, x),
/// returning its best fitting parameter p0 and p1.
/// </summary>
public static (double P0, double P1) Curve(double[] x, double[] y, Func<double, double, double, double> f, double initialGuess0, double initialGuess1, double tolerance = 1e-8, int maxIterations = 1000)
{
return FindMinimum.OfFunction((p0, p1) => Distance.Euclidean(Generate.Map(x, t => f(p0, p1, t)), y), initialGuess0, initialGuess1, tolerance, maxIterations);
}
/// <summary>
/// Non-linear least-squares fitting the points (x,y) to an arbitrary function y : x -> f(p0, p1, p2, x),
/// returning its best fitting parameter p0, p1 and p2.
/// </summary>
public static (double P0, double P1, double P2) Curve(double[] x, double[] y, Func<double, double, double, double, double> f, double initialGuess0, double initialGuess1, double initialGuess2, double tolerance = 1e-8, int maxIterations = 1000)
{
return FindMinimum.OfFunction((p0, p1, p2) => Distance.Euclidean(Generate.Map(x, t => f(p0, p1, p2, t)), y), initialGuess0, initialGuess1, initialGuess2, tolerance, maxIterations);
}
/// <summary>
/// Non-linear least-squares fitting the points (x,y) to an arbitrary function y : x -> f(p0, p1, p2, p3, x),
/// returning its best fitting parameter p0, p1, p2 and p3.
/// </summary>
public static (double P0, double P1, double P2, double P3) Curve(double[] x, double[] y, Func<double, double, double, double, double, double> f, double initialGuess0, double initialGuess1, double initialGuess2, double initialGuess3, double tolerance = 1e-8, int maxIterations = 1000)
{
return FindMinimum.OfFunction((p0, p1, p2, p3) => Distance.Euclidean(Generate.Map(x, t => f(p0, p1, p2, p3, t)), y), initialGuess0, initialGuess1, initialGuess2, initialGuess3, tolerance, maxIterations);
}
/// <summary>
/// Non-linear least-squares fitting the points (x,y) to an arbitrary function y : x -> f(p0, p1, p2, p3, p4, x),
/// returning its best fitting parameter p0, p1, p2, p3 and p4.
/// </summary>
public static (double P0, double P1, double P2, double P3, double P4) Curve(double[] x, double[] y, Func<double, double, double, double, double, double, double> f, double initialGuess0, double initialGuess1, double initialGuess2, double initialGuess3, double initialGuess4, double tolerance = 1e-8, int maxIterations = 1000)
{
return FindMinimum.OfFunction((p0, p1, p2, p3, p4) => Distance.Euclidean(Generate.Map(x, t => f(p0, p1, p2, p3, p4, t)), y), initialGuess0, initialGuess1, initialGuess2, initialGuess3, initialGuess4, tolerance, maxIterations);
}
/// <summary>
/// Non-linear least-squares fitting the points (x,y) to an arbitrary function y : x -> f(p, x),
/// returning a function y' for the best fitting curve.
/// </summary>
public static Func<double, double> CurveFunc(double[] x, double[] y, Func<double, double, double> f, double initialGuess, double tolerance = 1e-8, int maxIterations = 1000)
{
var parameters = Curve(x, y, f, initialGuess, tolerance, maxIterations);
return z => f(parameters, z);
}
/// <summary>
/// Non-linear least-squares fitting the points (x,y) to an arbitrary function y : x -> f(p0, p1, x),
/// returning a function y' for the best fitting curve.
/// </summary>
public static Func<double, double> CurveFunc(double[] x, double[] y, Func<double, double, double, double> f, double initialGuess0, double initialGuess1, double tolerance = 1e-8, int maxIterations = 1000)
{
var (p0, p1) = Curve(x, y, f, initialGuess0, initialGuess1, tolerance, maxIterations);
return z => f(p0, p1, z);
}
/// <summary>
/// Non-linear least-squares fitting the points (x,y) to an arbitrary function y : x -> f(p0, p1, p2, x),
/// returning a function y' for the best fitting curve.
/// </summary>
public static Func<double, double> CurveFunc(double[] x, double[] y, Func<double, double, double, double, double> f, double initialGuess0, double initialGuess1, double initialGuess2, double tolerance = 1e-8, int maxIterations = 1000)
{
var (p0, p1, p2) = Curve(x, y, f, initialGuess0, initialGuess1, initialGuess2, tolerance, maxIterations);
return z => f(p0, p1, p2, z);
}
/// <summary>
/// Non-linear least-squares fitting the points (x,y) to an arbitrary function y : x -> f(p0, p1, p2, p3, x),
/// returning a function y' for the best fitting curve.
/// </summary>
public static Func<double, double> CurveFunc(double[] x, double[] y, Func<double, double, double, double, double, double> f, double initialGuess0, double initialGuess1, double initialGuess2, double initialGuess3, double tolerance = 1e-8, int maxIterations = 1000)
{
var (p0, p1, p2, p3) = Curve(x, y, f, initialGuess0, initialGuess1, initialGuess2, initialGuess3, tolerance, maxIterations);
return z => f(p0, p1, p2, p3, z);
}
/// <summary>
/// Non-linear least-squares fitting the points (x,y) to an arbitrary function y : x -> f(p0, p1, p2, p3, p4, x),
/// returning a function y' for the best fitting curve.
/// </summary>
public static Func<double, double> CurveFunc(double[] x, double[] y, Func<double, double, double, double, double, double, double> f, double initialGuess0, double initialGuess1, double initialGuess2, double initialGuess3, double initialGuess4, double tolerance = 1e-8, int maxIterations = 1000)
{
(double p0, double p1, double p2, double p3, double p4) = Curve(x, y, f, initialGuess0, initialGuess1, initialGuess2, initialGuess3, initialGuess4, tolerance, maxIterations);
return z => f(p0, p1, p2, p3, p4, z);
}
}
}