找到最佳的单调曲线拟合

时间:2016-04-05 10:12:05

标签: matlab

编辑:在我提出这个问题之后的一段时间,一个名为MonoPoly的R包(可用here)出来了,它完全符合我的要求。我强烈推荐它。

我有一组我想要拟合曲线的点。曲线必须是单调的(从不减小值),即曲线只能向上或保持平坦。

我最初一直在对我的结果进行多重拟合,直到找到一个特定的数据集,这一直很有效。此数据集中数据的polyfit是非单调的。

我做了一些研究,并在this帖子中找到了一个可能的解决方案:

  

使用lsqlin。限制一阶导数在两者上都是非负的   感兴趣的领域的目的。

我来自编程而不是数学背景,所以这有点超出我的意义。他说,我不知道如何限制一阶导数是非负的。另外,我认为在我的情况下我需要一条曲线,所以我应该使用lsqcurvefit,但我不知道如何约束它以产生单调曲线。

进一步研究this推荐lsqcurvefit,但我无法弄清楚如何使用重要部分:

  

也尝试这个非线性函数F(x)。你一起使用它   lsqcurvefit但它需要开始猜测参数。但它是   一个很好的分析表达式,作为一个半经验公式给出   论文或报告。

     

%单调函数F(x),c0,c1,c2,c3变量常数F(x)=   c3 + exp(c0 - c1 ^ 2 /(4 * c2)) sqrt(pi) ...         Erfi((c1 + 2 * c2 * x)/(2 * sqrt(c2))))/(2 * sqrt(c2))

     

%Erfi(x)= erf(i * x)(看mathematica)但函数%看起来很像   x ^ 3%导数f(x),概率密度f(x)> = 0   F(X)= DF / DX = EXP(C0 + C1 * X + C2 * X ^ 2)

我必须有一个单调的曲线,但我不知道怎么做,即使有了所有这些信息。一个随机数是否足以让#34;开始猜测"。 lsqcurvefit最好吗?如何使用它来产生最佳拟合单调曲线?

由于

1 个答案:

答案 0 :(得分:2)

以下是使用lsqlin的简单解决方案。衍生约束在每个数据点中强制执行,如果需要,可以很容易地修改。

需要两个系数矩阵,一个(C)用于最小平方误差计算,一个(A)用于数据点中的导数。

% Following lsqlin's notations

%--------------------------------------------------------------------------
% PRE-PROCESSING
%--------------------------------------------------------------------------
% for reproducibility
rng(125)
degree  = 3;
n_data  = 10;
% dummy data
x       = rand(n_data,1);
d       = rand(n_data,1) + linspace(0,1,n_data).';

% limit on derivative - in each data point
b       = zeros(n_data,1);

% coefficient matrix
C       = nan(n_data, degree+1);
% derivative coefficient matrix
A       = nan(n_data, degree);

% loop over polynomial terms
for ii  = 1:degree+1
    C(:,ii) = x.^(ii-1);
    A(:,ii) = (ii-1)*x.^(ii-2);
end

%--------------------------------------------------------------------------
% FIT - LSQ
%--------------------------------------------------------------------------
% Unconstrained
% p1 = pinv(C)*y
p1 = fliplr((C\d).')

p2 = polyfit(x,d,degree)

% Constrained
p3 = fliplr(lsqlin(C,d,-A,b).')

%--------------------------------------------------------------------------
% PLOT
%--------------------------------------------------------------------------
xx = linspace(0,1,100);

plot(x, d, 'x')
hold on
plot(xx, polyval(p1, xx))
plot(xx, polyval(p2, xx),'--')
plot(xx, polyval(p3, xx))

legend('data', 'lsq-pseudo-inv', 'lsq-polyfit', 'lsq-constrained', 'Location', 'southoutside')
xlabel('X')
ylabel('Y')

对于指定的输入,拟合曲线: enter image description here

实际上这段代码比你要求的更通用,因为多项式的程度也可以改变。

编辑:在附加点强制执行派生约束

评论中指出的问题是由于衍生品检查仅在数据点中强制执行。在那些之间没有执行检查。以下是缓解此问题的解决方案。想法:使用惩罚术语将问题转换为无约束优化。

请注意,它使用术语pen来惩罚违反衍生检查的行为,因此结果不是真正的最小平方误差解决方案。此外,结果取决于惩罚函数。

function lsqfit_constr
% Following lsqlin's notations

%--------------------------------------------------------------------------
% PRE-PROCESSING
%--------------------------------------------------------------------------
% for reproducibility
rng(125)
degree  = 3;

% data from comment
x       = [0.2096 -3.5761 -0.6252 -3.7951 -3.3525 -3.7001 -3.7086 -3.5907].';
d       = [95.7750 94.9917 90.8417 62.6917 95.4250 89.2417 89.4333 82.0250].';
n_data  = length(d);

% number of equally spaced points to enforce the derivative
n_deriv = 20;
xd      = linspace(min(x), max(x), n_deriv);

% limit on derivative - in each data point
b       = zeros(n_deriv,1);

% coefficient matrix
C       = nan(n_data, degree+1);
% derivative coefficient matrix
A       = nan(n_deriv, degree);

% loop over polynom terms
for ii  = 1:degree+1
    C(:,ii) = x.^(ii-1);
    A(:,ii) = (ii-1)*xd.^(ii-2);
end

%--------------------------------------------------------------------------
% FIT - LSQ
%--------------------------------------------------------------------------
% Unconstrained
% p1 = pinv(C)*y
p1      = (C\d);
lsqe    = sum((C*p1 - d).^2);

p2      = polyfit(x,d,degree);

% Constrained
[p3, fval] = fminunc(@error_fun, p1);

% correct format for polyval
p1      = fliplr(p1.')
p2
p3      = fliplr(p3.')
fval

%--------------------------------------------------------------------------
% PLOT
%--------------------------------------------------------------------------
xx      = linspace(-4,1,100);

plot(x, d, 'x')
hold on
plot(xx, polyval(p1, xx))
plot(xx, polyval(p2, xx),'--')
plot(xx, polyval(p3, xx))

% legend('data', 'lsq-pseudo-inv', 'lsq-polyfit', 'lsq-constrained', 'Location', 'southoutside')
xlabel('X')
ylabel('Y')

%--------------------------------------------------------------------------
% NESTED FUNCTION
%--------------------------------------------------------------------------
    function e = error_fun(p)
        % squared error 
        sqe = sum((C*p - d).^2);
        der = A*p;

        % penalty term - it is crucial to fine tune it
        pen = -sum(der(der<0))*10*lsqe;

        e   = sqe + pen;
    end
end

enter image description here

通过严格执行衍生约束,可以使用无梯度方法来解决问题,例如:

[p3, fval] = fminsearch(@error_fun, p_ini);

%--------------------------------------------------------------------------
% NESTED FUNCTION
%--------------------------------------------------------------------------
function e = error_fun(p)
    % squared error
    sqe = sum((C*p - d).^2);
    der = A*p;

    if any(der<0)
        pen = Inf;
    else
        pen = 0;
    end

    e   = sqe + pen;
end
具有非线性约束的

fmincon可能是更好的选择。 我让你弄清楚细节并调整算法。我希望这就足够了。