读书人

《Practical WPF Charts and Graphics

发布时间: 2012-12-20 09:53:21 作者: rapoo

《Practical WPF Charts and Graphics 》翻译——之11章 曲线拟合(2)

线性回归

让我们首先考虑最小二乘拟合的线性形式

《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)

其中fi(x)是已经定义过的x的函数,叫做基本函数。这种情况下,残差和就是

《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)

相应的梯度方程从《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)简化到

《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)

先前的方程能写成矩阵形式

《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)

这个矩阵形式也叫做最小二乘拟合的标准方程,它可以通过前面章节讨论的Gauss-Jordan方法求解。

实现

使用前面章节提到的算法,我们可以实现线性回归方法。添加一个新的公有静态方法LinearRegressionCurveFittingAlgorithms类里:

图11-3多项式曲线拟合

加权线性回归

我们前面讨论到,线性回归对于通过模型函数展示观察数据是一种有用的技术,可以表示成最小二乘法的最小化问题。在线性最小二乘中,结果分析需要使用Gauss-Jordan方法求解联立方程组。线性回归中的一个重要的假设是所有的错误具有相同的意义。

然而,有些情况下不同的点的精确度的可靠性不一样。例如,有可能存在一个测量的精度漂移,一些错误或多或少比其他更重要。为了考虑到这些因素,我们引入加权因子到每个数据点来最小化加权的残差平方和:

《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)

这个过程强制将拟合函数f(x)朝更大权重的点逼近。

对于最简单的线性回归,例如,直线拟合,它的拟合函数通过f(x)=a+bx给出,前面的方程就变成

《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)

根据最小化约束条件《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2),我们可以确定系数a和b:

《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)

这里,xw和yw是加权平均:

《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)

实现

使用前面提出的方法,我们可以实现加权线性回归。添加一个新的公有静态方法WeightedLinearRegression到CurveFittingAlgorithms类

,当在最小二乘技术用使用时,通常会导致

系数a和b的非线性依赖。然而,如果我们使用logy而不是y,问题就变成了线性回归。这种情况下,拟合函数变成

《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)

注意到数据对数的最小二乘拟合与原始数据的最小二乘拟合不太一样。对数拟合的残差是

《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)

而原始数据拟合的残差是

《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)

我们可以对对数拟合加权来消除这种差异。根据前面的方程,我们有

《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)

因此对数拟合的残差能够重新写成下面的形式:

《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)

在约束《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)中,我们可以使用近似处理《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)。你可以通过最小化《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)来明白这个,我们需要引入权重因子《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)。当对数据点《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)拟合F(x)时如果我们使用权重《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)我们可以这样做。因此,最小化《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)

将变成最小化《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)的近似。

加上我们有下面的一组数据:

x = 1, 2 , 3, 4, 5 , 6, 7,8, 9, 10

y = 1.9398, 2.9836,5.9890, 10.2, 20.7414, 23.232, 69.5855, 82.5836, 98.1779 ,339.3256

我们想要使用指数函数《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)来拟合这些数据。首先我们需要进行一个对数变换log y = log a +bx,然后使用WeightedLinearRegression方法计算loga和b的系数。

添加一个新的WPF Windows应用程序到当前项目中,命名为WeightedLinearRegression.下面是这个例子的XAML文件:

我们可以通过将它们绘制出来来检验拟合结果的好坏(如图11-4).你可以从图11-4中看到加权线性回归确实给出了一个很好的拟合结果。

《Practical WPF Charts and Graphics 》通译——之11章 曲线拟合(2)

图11-4加权线性回归的结果:对数绘图(左)和线性绘图(右)

读书人网 >编程

热点推荐