gpt4 book ai didi

r - R 中的 fminsearch 比 Matlab 中的差

转载 作者:太空宇宙 更新时间:2023-11-03 20:10:13 26 4
gpt4 key购买 nike

这是我的数据(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# 中实现它,那就更好了。我知道从哪里获得 polyfit,但不知道 fminsearch。

最佳答案

这里的问题是似然面的表现非常糟糕——存在多个最小值和不连续的跳跃——这将使您使用不同的优化器获得的结果几乎是任意的。我承认 MATLAB 的优化器非常强大,但我要说的是,优化器是否会在这种情况下达到全局最小值几乎是一个偶然的问题(以及你从哪里开始),除非你使用某种形式的随机全局优化例如模拟退火。

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

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 估计参数周围的二维“切片”。圆圈显示最佳拟合(实心)和每个切片内的最小值(空心)。

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

enter image description here

matlab 和优化拟合之间的“切片”显示表面非常粗糙:

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

enter image description here

关于r - R 中的 fminsearch 比 Matlab 中的差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20033993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com