• 本文是对 Matlab 中 polyfit 函数进行原理解析,并没介绍该如何具体使用 polyfit 函数,具体方法请自行查阅官方文档。
  • 主要涉及数值分析的相关内容。
  • 简单介绍了数据标准化(Z-score 标准化)、QR 分解、Matlab 中的求逆运算符。
  • 相对详细介绍了线性方程组求解的稳定性问题,并引出条件数的定义。
  • 最后根据 polyfit 的源码对它进行计算流程解析,并分析相关的警告该如何处理。

预备知识

标准化

对向量

equation?tex=%5Cboldsymbol%7Bx%7D 进行标准化(Standardization,Z-score normalization)定义如下:

equation?tex=%5Cboldsymbol%7Bx%7D%27%3D%5Cfrac%7B%5Cboldsymbol%7Bx%7D-%5Cbar%7B%5Cboldsymbol%7Bx%7D%7D%7D%7B%5Csigma%7D%5C%5C

其中

equation?tex=%5Cbar%7B%5Cboldsymbol%7Bx%7D%7D 表示
equation?tex=%5Cboldsymbol%7Bx%7D 的均值,即
equation?tex=%5Cbar%7B%5Cboldsymbol%7Bx%7D%7D%3D%5Ctext%7Bmean%7D%28x%29 ,
equation?tex=%5Csigma 表示标准差,即
equation?tex=%5Csigma%3D%5Ctext%7Bstd%7D%28x%29 。
  • equation?tex=%5Cboldsymbol%7Bx%7D%27 为
    标准分数(standard score)也叫 Z分数(Z-score);
  • Z分数表示原始数据与平均值的差,单位为标准差,也就时给定值距离均值多少个标准差。

QR 分解定理

设

equation?tex=A 是
equation?tex=n 阶实非奇异矩阵,则存在正交矩阵
equation?tex=Q 和上三角矩阵
equation?tex=R 使得
equation?tex=A%3DQR ,当
equation?tex=R 的对角元均为正时,分解唯一。

注:正交矩阵

equation?tex=Q%5E%7BT%7D%3DQ%5E%7B-1%7D%5CLeftrightarrow+Q%5E%7BT%7DQ%3DQQ%5E%7BT%7D%3DI

求逆运算

Y = inv(X) 计算方阵

equation?tex=X 的逆矩阵。
  • X^(-1) 等效于 inv(X)。
  • x = Ab 的计算方式与 x = inv(A)*b 不同,建议用于求解线性方程组。

方程组求解的稳定性

设线性方程组

equation?tex=Ax%3Db ,其中
equation?tex=b 有小扰动
equation?tex=b%2B%5CDelta+b ,得到解
equation?tex=%5Ctilde%7Bx%7D%3Dx%2B%5CDelta+x ,那么:

equation?tex=%5Cbegin%7Bsplit%7D++A%28x%2B%5CDelta+x%29%26%3Db%2B%5CDelta+b%5C%5C++A%5CDelta+x%26%3D%5CDelta+b%5C%5C++%5CDelta+x%26%3DA%5E%7B-1%7D%5CDelta+b++%5Cend%7Bsplit%7D%5C%5C

由

equation?tex=%5C%7Cb%5C%7C%3D%5C%7CAx%5C%7C%5Cleq%5C%7CA%5C%7C%5Ccdot+%5C%7Cx%5C%7C ,可得
equation?tex=%5C%7C%5CDelta+x%5C%7C%5Cleq+%5C%7CA%5E%7B-1%7D%5C%7C%5Ccdot+%5C%7C%5CDelta+b%5C%7C

考虑相对误差:

equation?tex=%5Cfrac%7B%5C%7C%5CDelta+x%5C%7C%7D%7B%5C%7Cx%5C%7C%7D%5Cleq+%5Cfrac%7B%5C%7C%5CDelta+x%5C%7C%7D%7B%5C%7Cb%5C%7C%2F%5C%7CA%5C%7C%7D%3D%5Cfrac%7B%5C%7CA%5C%7C%7D%7B%5C%7Cb%5C%7C%7D%5Ccdot+%5C%7C%5CDelta+x%5C%7C%5Cleq+%5Cfrac%7B%5C%7CA%5C%7C%7D%7B%5C%7Cb%5C%7C%7D%5Ccdot+%5C%7CA%5E%7B-1%7D%5C%7C%5Ccdot+%5C%7C%5CDelta+b%5C%7C%3D%5C%7CA%5C%7C%5Ccdot+%5C%7CA%5E%7B-1%7D%5C%7C%5Cfrac%7B%5C%7C%5CDelta+b%5C%7C%7D%7B%5C%7Cb%5C%7C%7D%5C%5C

即

equation?tex=%5Cfrac%7B%5C%7C%5CDelta+x%5C%7C%7D%7B%5C%7Cx%5C%7C%7D%5Cleq+%5C%7CA%5C%7C%5Ccdot+%5C%7CA%5E%7B-1%7D%5C%7C%5Cfrac%7B%5C%7C%5CDelta+b%5C%7C%7D%7B%5C%7Cb%5C%7C%7D%5C%5C

条件数

  • 称
    equation?tex=%5Ctext%7Bcond%7D%28A%29%5Ctriangleq+%5C%7CA%5C%7C%5Ccdot+%5C%7CA%5E%7B-1%7D%5C%7C 为
    equation?tex=A 的条件数;
  • 表示
    equation?tex=b 变化时解的相对误差灵敏度的度量;
  • 当
    equation?tex=%5Ctext%7Bcond%7D%28A%29 较小时,解对
    equation?tex=A 的扰动不敏感。

注:

equation?tex=%5C%7C%5Ccdot%5C%7C 表示矩阵范数,满足正定性、齐次性以及三角不等式。常见的范数定义有:
  • equation?tex=%5C%7C%5Cboldsymbol%7Bx%7D%5C%7C_1%3D%5Csum%5En%7Bi%3D1%7D%7Cx_i%7C
  • equation?tex=%5C%7C%5Cboldsymbol%7Bx%7D%5C%7C2%3D%28%5Csum%5En%7Bi%3D1%7D%7Cx_i%7C%5E2%29%5E%7B%5Cfrac%7B1%7D%7B2%7D%7D
  • equation?tex=%5C%7C%5Cboldsymbol%7Bx%7D%5C%7C_%5Cinfty%3D%5Cmax%7B%7Cx_1%7C%2C%7Cx_2%7C%2C%5Ccdots%2C+%7Cx_n%7C%7D

polyfit 函数原理

polyfit 使用一维向量

equation?tex=%5Cboldsymbol%7Bx%7D 构造具有
equation?tex=n%2B1 列和
m = length(x) 行的范德蒙(Vandermonde)矩阵
equation?tex=V (见源码55-59行)并生成线性方程组:

51398a7d758ca64a64fff567f21d9fc3.png

拟合多项式

equation?tex=p%28x%29%3Dp_1x%5En%2Bp_2x%5E%7Bn%E2%88%921%7D%2B...%2Bp_nx%2Bp_%7Bn%2B1%7D 转换为求解线性方程组(2)(非上述方程组),具体地,
polyfit 先对
equation?tex=V 进行
QR分解,然后再使用反斜杠运算进行求解(见源码61-69行)。

具体过程如下:

equation?tex=%5Cbegin%7Bsplit%7D++Vp%26%3Dy+%5Cquad%5Cquad%5Cquad%5C%3B+%281%29%5C%5C++%5CLongleftrightarrow+QRp%26%3Dy%5C%5C+%5CLongleftrightarrow+Rp%26%3DQ%5ETy%5Cquad%5Cquad+%282%29%5C%5C+%5CLongleftrightarrow+p%26%3DR%5E%7B-1%7DQ%5ETy++%5Cend%7Bsplit%7D%5C%5C 转化为 Matlab 运算即为
p = R(Q'*y)。

异常处理逻辑

共以下4种情况

  • MATLAB:polyfit:XYSizeMismatch
  • MATLAB:polyfit:PolyNotUnique
  • MATLAB:polyfit:RepeatedPoints
  • MATLAB:polyfit:RepeatedPointsOrRescale

输入判断

  • polyfit函数只用于拟合(x,y)型二维数据,因此输入数据应是两两对应的,即向量
    equation?tex=%5Cboldsymbol%7Bx%7D%2C%5Cboldsymbol%7By%7D 长度相等(见源码43-45行)。
  • 若提示MATLAB:polyfit:XYSizeMismatch,请检查上述问题。

欠定方程

  • 若提示MATLAB:polyfit:PolyNotUnique 表示线性方程组
    equation?tex=Rp%3DQ%5ETy 欠定(矩阵
    equation?tex=R 的列数大于行数)

考虑

  1. 添加更多的不同的拟合点;
  2. 减少多项式的次数.

条件数过大

由于 Vandermonde 矩阵中的列是向量

equation?tex=%5Cboldsymbol%7Bx%7D 的幂,因此
equation?tex=V 的
条件数对于高阶拟合来说通常较大,生成一个奇异系数矩阵。在这些情况下,中心化和缩放可改善系统的数值属性以产生更可靠的拟合。而数据的标准化由函数调用时输出情况决定,当且仅当调用[P,S,MU] = POLYFIT(X,Y,N) 才执行数据标准化(见源码50-53行)。

当

equation?tex=%5Ctext%7Bcond%7D%28R%29 过大时才会提示以下其中一个警告:
  • 若提示 MATLAB:polyfit:RepeatedPoints,考虑添加更多的不同的拟合点
  • 若提示 MATLAB:polyfit:RepeatedPointsOrRescale,考虑1、添加更多的不同的拟合点;2、减少多项式的次数

源码阅读

function [p,S,mu] = polyfit(x,y,n)
%POLYFIT Fit polynomial to data.
%   P = POLYFIT(X,Y,N) finds the coefficients of a polynomial P(X) of
%   degree N that fits the data Y best in a least-squares sense. P is a
%   row vector of length N+1 containing the polynomial coefficients in
%   descending powers, P(1)*X^N + P(2)*X^(N-1) +...+ P(N)*X + P(N+1).
%
%   [P,S] = POLYFIT(X,Y,N) returns the polynomial coefficients P and a
%   structure S for use with POLYVAL to obtain error estimates for
%   predictions.  S contains fields for the triangular factor (R) from a QR
%   decomposition of the Vandermonde matrix of X, the degrees of freedom
%   (df), and the norm of the residuals (normr).  If the data Y are random,
%   an estimate of the covariance matrix of P is (Rinv*Rinv')*normr^2/df,
%   where Rinv is the inverse of R.
%
%   [P,S,MU] = POLYFIT(X,Y,N) finds the coefficients of a polynomial in
%   XHAT = (X-MU(1))/MU(2) where MU(1) = MEAN(X) and MU(2) = STD(X). This
%   centering and scaling transformation improves the numerical properties
%   of both the polynomial and the fitting algorithm.
%
%   Warning messages result if N is >= length(X), if X has repeated, or
%   nearly repeated, points, or if X might need centering and scaling.
%
%   Example: simple linear regression with polyfit
%
%     % Fit a polynomial p of degree 1 to the (x,y) data:
%       x = 1:50;
%       y = -0.3*x + 2*randn(1,50);
%       p = polyfit(x,y,1);
%
%     % Evaluate the fitted polynomial p and plot:
%       f = polyval(p,x);
%       plot(x,y,'o',x,f,'-')
%       legend('data','linear fit')
%
%   Class support for inputs X,Y:
%      float: double, single
%
%   See also POLY, POLYVAL, ROOTS, LSCOV.

%   Copyright 1984-2018 The MathWorks, Inc.

if ~isequal(size(x),size(y))
    error(message('MATLAB:polyfit:XYSizeMismatch'))
end

x = x(:);
y = y(:);

if nargout > 2
    mu = [mean(x); std(x)];
    x = (x - mu(1))/mu(2);
end

% Construct the Vandermonde matrix V = [x.^n ... x.^2 x ones(size(x))]
V(:,n+1) = ones(length(x),1,class(x));
for j = n:-1:1
    V(:,j) = x.*V(:,j+1);
end

% Solve least squares problem p = Vy to get polynomial coefficients p.
[Q,R] = qr(V,0);
oldws = warning('off','all');   % Turn all warnings off before solving
try
    p = R(Q'*y);               % Same as p = Vy
catch ME
    warning(oldws);             % Restore initial warning state
    throw(ME);
end
warning(oldws);                 % Restore initial warning state

% Issue warnings.
if size(R,2) > size(R,1)
    warning(message('MATLAB:polyfit:PolyNotUnique'))
elseif warnIfLargeConditionNumber(R)
    if nargout > 2
        warning(message('MATLAB:polyfit:RepeatedPoints'));
    else
        warning(message('MATLAB:polyfit:RepeatedPointsOrRescale'));
    end
end

if nargout > 1
    r = y - V*p;
    % S is a structure containing three elements: the triangular factor
    % from a QR decomposition of the Vandermonde matrix, the degrees of
    % freedom and the norm of the residuals.
    S.R = R;
    S.df = max(0,length(y) - (n+1));
    S.normr = norm(r);
end

p = p.'; % Polynomial coefficients are row vectors by convention.

function flag = warnIfLargeConditionNumber(R)
if isa(R, 'single')
    flag = (condest(R) > 1e+05);
else
    flag = (condest(R) > 1e+10);
end

参考资料

  1. Matlab inv
  2. Matlab @polyfit
  3. Stoer J, Bulirsch R. Introduction to Numerical Analysis[M]. Third Edition. New York, NY: Springer, 2002.
  4. 我的原始语雀文档

更多推荐