Sha*_*kov 3 optimization matlab r fminsearch
有我的数据(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)
Run Code Online (Sandbox Code Playgroud)
这确实很好,产生了以下优化:[102.9842,191.0006,41.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
}
Run Code Online (Sandbox Code Playgroud)
但是我得到了非常糟糕的结果:
> 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
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,它在2次迭代后停止并且不会产生好点.
我很乐意为解决这个问题提供帮助.
此外,如果有人知道如何使用任何免费库在C#中实现它,它会更好.我知道哪里可以获得polyfit,但不知道fminsearch.
Ben*_*ker 14
这里的问题是可能性表面非常糟糕 - 有多个最小值和不连续跳跃 - 这将使得不同优化器得到的结果几乎是任意的.我承认MATLAB的优化器非常强大,但我想说,除非你使用某种形式的随机全局优化,否则优化器在这种情况下是否会达到全局最小值几乎是偶然(以及你从哪里开始)例如模拟退火.
我选择使用的r内置的优化器(使用内尔德-米德默认情况下),而不是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
Run Code Online (Sandbox Code Playgroud)
这比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)
Run Code Online (Sandbox Code Playgroud)
该bbmle软件包提供了一组用于探索优化表面的实验性/非常好记录的工具集:
library(bbmle)
ss <- slice2D(fun=spline_fit,opt1$par,nt=51)
library(lattice)
Run Code Online (Sandbox Code Playgroud)
围绕optim-estimated参数的2D"切片" .圆圈显示每个切片(打开)中的最佳拟合(实线)和最小值.
png("splom1.png")
print(splom(ss))
dev.off()
Run Code Online (Sandbox Code Playgroud)

matlab和optim fit之间的"切片"表明表面非常坚固:
ss2 <- bbmle:::slicetrans(matlab_fit,opt1$par,spline_fit)
png("slice1.png")
print(plot(ss2))
dev.off()
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
1820 次 |
| 最近记录: |