在这里,我展示了一个示例,说明如何使用任意数量的参数拟合任意函数,并为每个单独的参数设置上限/下限。就像RexCardan 的示例一样,它是使用MathNet库完成的。
它不是很快,但它确实有效。
为了适应不同的功能改变double gaussian(Vector<double> vectorArg)
方法。如果更改vectorArg
s 的数量还需要调整:
- 和中
lowerBound
的元素数。upperBound
initialGuess
CurveFit
- 改变参数个数
return z => f(new DenseVector(new[] { parameters[0], parameters[1], parameters[2], parameters[3], parameters[4], parameters[5], z }));
- 改变参数个数
t => f(new DenseVector(new[] { p[0], p[1], p[2], p[3], p[4], p[5], t }))
双高斯的示例代码
using MathNet.Numerics;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double;
using System;
using System.Linq;
static class GaussianFit
{
/// <summary>
/// Non-linear least square Gaussian curve fit to data.
/// </summary>
/// <param name="mu1">x position of the first Gaussian.</param>
/// <param name="mu2">x position of the second Gaussian.</param>
/// <returns>Array of the Gaussian profile.</returns>
public Func<double, double> CurveFit(double[] xData, double[] yData, double mu1, double mu2)
{
//Define gaussian function
double gaussian(Vector<double> vectorArg)
{
double x = vectorArg.Last();
double y =
vectorArg[0] * Normal.PDF(vectorArg[1], vectorArg[2], x)
+ vectorArg[3] * Normal.PDF(vectorArg[4], vectorArg[5], x);
return y;
}
var lowerBound = new DenseVector(new[] { 0, mu1 * 0.98, 0.05, 0, mu2 * 0.98, 0.05 });
var upperBound = new DenseVector(new[] { 1e10, mu1 * 1.02, 0.3, 1e10, mu2 * 1.02, 0.3 });
var initialGuess = new DenseVector(new[] { 1000, mu1, 0.2, 1000, mu2, 0.2 });
Func<double, double> fit = CurveFuncConstrained(
xData, yData, gaussian, lowerBound, upperBound, initialGuess
);
return fit;
}
/// <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> CurveFuncConstrained(
double[] x,
double[] y,
Func<Vector<double>, double> f,
Vector<double> lowerBound,
Vector<double> upperBound,
Vector<double> initialGuess,
double gradientTolerance = 1e-5,
double parameterTolerance = 1e-5,
double functionProgressTolerance = 1e-5,
int maxIterations = 1000
)
{
var parameters = CurveConstrained(
x, y, f,
lowerBound, upperBound, initialGuess,
gradientTolerance, parameterTolerance, functionProgressTolerance,
maxIterations
);
return z => f(new DenseVector(new[] { parameters[0], parameters[1], parameters[2], parameters[3], parameters[4], parameters[5], z }));
}
/// <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 Vector<double> CurveConstrained(
double[] x,
double[] y,
Func<Vector<double>, double> f,
Vector<double> lowerBound,
Vector<double> upperBound,
Vector<double> initialGuess,
double gradientTolerance = 1e-5,
double parameterTolerance = 1e-5,
double functionProgressTolerance = 1e-5,
int maxIterations = 1000
)
{
return FindMinimum.OfFunctionConstrained(
(p) => Distance.Euclidean(
Generate.Map(
x,
t => f(new DenseVector(new[] { p[0], p[1], p[2], p[3], p[4], p[5], t }))
),
y),
lowerBound,
upperBound,
initialGuess,
gradientTolerance,
parameterTolerance,
functionProgressTolerance,
maxIterations
);
}
例子
在 x 位置 10 和 20 拟合两个高斯:
Func<double, double> fit = GaussianFit.Curvefit(x_data, y_data, 10, 20);