有我的数据(x和y列是相关的): https://www.dropbox.com/s/b61a7enhoa0p57p/simple1.csv

我需要的是用折线拟合数据。MATLAB代码这是:

spline_fit.m:
function [score, params] = spline_fit (points, x, y)

min_f = min(x)-1;
max_f = max(x);

points = [min_f points max_f];
params = zeros(length(points)-1, 2);

score = 0;
for i = 1:length(points)-1
    in = (x > points(i)) & (x <= points(i+1));
    if sum(in) > 2
        p = polyfit(x(in), y(in), 1);
        pred = p(1)*x(in) + p(2);
        score = score + norm(pred - y(in));
        params(i, :) = p;
    else
       params(i, :) = nan;
    end
end


test.m:
%Find the parameters
r = [100,250,400];
p = fminsearch('spline_fit', r, [], x, y)
[score, param] = spline_fit(p, x, y)

%Plot the result
y1 = zeros(size(x));
p1 = [-inf, p, inf];
for i = 1:size(param, 1)
    in = (x > p1(i)) & (x <= p1(i+1));
    y1(in) = x(in)*param(i,1) + param(i,2);
end

[x1, I] = sort(x);
y1 = y1(I);

plot(x,y,'x',x1,y1,'k','LineWidth', 2)
.

,这确实可以很好,产生以下优化:[102.9842,191.0006,421.9912]

我在R:

中实现了相同的想法
library(pracma);
spline_fit <- function(x, xx, yy) {

  min_f = min(xx)-1;
  max_f = max(xx);

  points = c(min_f, x, max_f)
  params = array(0, c(length(points)-1, 2));

  score = 0;
  for( i in 1:length(points)-1)
  {
    inn <- (xx > points[i]) & (xx <= points[i+1]);
    if (sum(inn) > 2)
    {
      p <- polyfit(xx[inn], yy[inn], 1);
      pred <- p[1]*xx[inn] + p[2];
      score <- score + norm(as.matrix(pred - yy[inn]),"F");
      params[i,] <- p;
    }
    else
      params[i,] <- NA;
  }  
  score
}
.

但结果非常糟糕:

> fminsearch(spline_fit,c(100,250,400), xx = Simple1$x, yy = Simple1$y)
$xval
[1] 100.1667 250.0000 400.0000

$fval
[1] 4452.761

$niter
[1] 2
.

正如您所看到的,它在2次迭代后停止,不会产生好点。

我很高兴解决解决这个问题的帮助。

此外,如果有人知道如何使用任何免费库中的C#在C#中实现这一点,那将更好。我知道whereto得到polyfit,但不是fminsearch。

有帮助吗?

解决方案

这里的问题是,可能性表面表现得非常严重 - 有多个最小值和不连续的跳跃 - 这将使您使用不同的优化器几乎是任意的结果。我将承认,Matlab的优化器是非常强大的,但我会说这几乎是机会(以及您开始的地方),除非您使用某种形式的随机全局优化,否则优化器将获得全局最小值。如模拟退火。

我选择使用R的内置优化器(默认使用Nelder-Mead)而不是来自fminsearch包的pracma

spline_fit <- function(x, xx = Simple1$x, yy=Simple1$y) {

    min_f = min(xx)-1
    max_f = max(xx)

    points = c(min_f, x, max_f)
    params = array(0, c(length(points)-1, 2))

    score = 0
    for( i in 1:(length(points)-1))
    {
        inn <- (xx > points[i]) & (xx <= points[i+1]);
        if (sum(inn) > 2)
        {
            p <- polyfit(xx[inn], yy[inn], 1);
            pred <- p[1]*xx[inn] + p[2];
            score <- score + norm(as.matrix(pred - yy[inn]),"F");
            params[i,] <- p;
        }
        else
            params[i,] <- NA;
    }  
    score
}

library(pracma) ## for polyfit
Simple1 <- read.csv("Simple1.csv")
opt1 <- optim(fn=spline_fit,c(100,250,400), xx = Simple1$x, yy = Simple1$y)
## [1] 102.4365 201.5835 422.2503
.

这比世代古代替代代码结果更好,但仍然不同于MATLAB结果,而且比它们更糟糕:

## Matlab results:
matlab_fit <- c(102.9842, 191.0006, 421.9912)
spline_fit(matlab_fit, xx = Simple1$x, yy = Simple1$y)
## 3724.3
opt1$val
## 3755.5  (worse)
.

fminsearch封装提供了一种用于探索优化表面的实验/不是非常好的文档工具:

library(bbmle)
ss <- slice2D(fun=spline_fit,opt1$par,nt=51)
library(lattice)
.

bbmle估计参数周围的2D“切片”。圆圈显示出优化拟合(固体)和每个切片内的最小值(打开)。

png("splom1.png")
print(splom(ss))
dev.off()
.

Matlab和Optim Fits之间的“切片”表明表面非常坚固:

ss2 <- bbmle:::slicetrans(matlab_fit,opt1$par,spline_fit)
png("slice1.png")
print(plot(ss2))
dev.off()
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top