带有lsqcurvefit的加权曲线拟合 [英] weighted curve fitting with lsqcurvefit
问题描述
我想为我的数据集添加一个任意函数.因此,我在MATLAB中使用了 lsqcurvefit
.现在,我想对拟合过程赋予权重,这意味着当曲线拟合函数( lsqcurvefit
)计算拟合的残差时,某些数据点比其他数据点更重要.更具体地说,我想使用统计加权方法.
I wanted to fit an arbitrary function to my data set. Therefore, I used lsqcurvefit
in MATLAB. Now I want to give weight to the fit procedure, meaning when curve fitting function (lsqcurvefit
) is calculating the residue of the fit, some data point are more important than the others. To be more specific I want to use statistical weighting method.
w=1/y(x),
其中 w
是一个矩阵,其中包含每个数据点的权重,而 y
是数据集.
where w
is a matrix contains the weight of each data point and y
is the data set.
我仍然找不到使用 lsqcurvefit
进行加权曲线拟合的方法.有什么我应该遵循的技巧或者是不是为我做的除了 lsqcurvefit
之外的其他任何功能?
I cannot find anyway to make weighted curve fitting with lsqcurvefit
. Is there any trick I should follow or is there any other function rather than lsqcurvefit
which do it for me?
推荐答案
For doing weighting, I find it much easier to use lsqnonlin
which is the function that lsqcurvefit
calls to do the actual fitting.
您首先必须定义一个要最小化的函数,即.成本函数.您需要将加权函数作为额外参数作为矢量的功能:
You first have to define a function that you are trying to minimize, ie. a cost function. You need to pass in your weighting function as an extra parameter to your function as a vector:
x = yourIndependentVariable;
y = yourData;
weightVector = sqrt(abs(1./y));
costFunction = @(A) weightVector.*(yourModelFunction(A) - y);
aFit = lsqnonlin(costFunction,aGuess);
加权函数定义中平方根的原因是 lsqnonlin
需要残差,而不是平方残差或它们的和,因此您需要对权重进行预平方.
The reason for the square root in the weighting function definition is that lsqnonlin
requires the residuals, not the squared residuals or their sum, so you need to pre-unsquare the weights.
或者,如果您有统计工具箱,则可以使用 nlinfit
,它将接受加权矢量/矩阵作为可选输入之一.
Alternatively, if you have the Statistics Toolbox, you can use nlinfit
which will accept a weighting vector/matrix as one of the optional inputs.
这篇关于带有lsqcurvefit的加权曲线拟合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!