質問

データがあります(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#でもっと良くなります。私はPolyfitを取得するがfminsearchではないことを知っています。

役に立ちましたか?

解決

問題は非常にひどく振る舞っていることです - 複数の最小値と不連続なジャンプの両方があります - それはあなたがほとんど恣意的な最適化業者で得られる結果を作ります。私はMATLABのオプティマイザーが著しく堅牢であることを認めますが、この場合は最後の地球規模の最適化の形式を使用しない限り、それが最適化されていないかどうか、最適化業者がグローバルな最小になるかどうかということはほとんど(そしてどこで始める)と言うでしょう。シミュレートされたアニーリングなどの。

fminsearchパッケージからのpracmaではなく、Rの組み込みオプティマイザ(デフォルトでnelder-meadを使用する)を使用しました。

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
.

これはfminsearchの結果よりも優れていますが、それでも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)
.

bbmleパッケージは、最適化表面を探索するための実験的/損害のない文書化されていません:

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

optim推定パラメータの周囲の2D "スライス"。円は、各スライス内の最小フィット(固体)と最小値を示します(開いている)。

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

画像の入力ここで

MATLABとOPTIMのフィットの間の「スライス」は、表面がかなり頑丈であることを示しています:

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

Enter Image説明

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top