用线性/非线性回归拟合两条曲线 [英] Fitting two curves with linear/non-linear regression

查看:222
本文介绍了用线性/非线性回归拟合两条曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用JuMP将两条曲线(都应属于三次函数)拟合到一组点中.

I need to fit two curves(which both should belong to cubic functions) into a set of points with JuMP.

我已经完成了一条曲线的拟合,但是我正在努力将2条曲线拟合到同一数据集中.

I've done fitting one curve, but I'm struggling at fitting 2 curves into same dataset.

我认为,如果可以将点分布到曲线上-如果每个点只能使用一次,则可以像下面那样进行操作,但是没有用. (我知道我可以使用更复杂的东西,我想保持简单.)

I thought that if I can distribute points to curves - so if each point can only be used once - I can do it like below, but it didn't work. (I know that I can use much more complicated things, I want to keep it simple.)

这是我当前代码的一部分:

This is a part of my current code:

# cubicFunc is a two dimensional array which accepts cubicFunc[x,degree]

@variable(m, mult1[1:4]) // 0:3 because it's cubic
@variable(m, mult2[1:4]) // 0:3 because it's cubic

@variable(m, 0 <= includeIn1[1:numOfPoints] <= 1, Int)
@variable(m, 0 <= includeIn2[1:numOfPoints] <= 1, Int)

# some kind of hack to force one of them to 0 and other one to 1
@constraint(m, loop[i in 1:numOfPoints], includeIn1[i] + includeIn2[i] == 1)

@objective(m, Min, sum( (yPoints - cubicFunc*mult1).*includeIn1 .^2 ) + sum( (yPoints - cubicFunc*mult2).*includeIn2 .^2 ))

但是它会根据我的尝试给出各种错误; *includeIn1并且.*includeIn1不起作用,我尝试通过@NLobjective进行操作,但是它给了我多达50行的错误等.

But it gives various errors depending on what I'm trying; *includeIn1 and, .*includeIn1 doesn't work, I've tried to do it via @NLobjective but it gave me whooping ~50 lines of errors etc.

我的想法现实吗?我可以将其写入代码吗?

Is my idea realistic? Can I make it into the code?

任何帮助将不胜感激.非常感谢.

Any help will be highly appreciated. Thank you very much.

推荐答案

您可以写下问题,例如像这样:

You can write down the problem e.g. like this:

using JuMP, Ipopt

m = Model(with_optimizer(Ipopt.Optimizer))

@variable(m, mult1[1:4])
@variable(m, mult2[1:4])
@variable(m, 0 <= includeIn1[1:numOfPoints] <= 1)
@variable(m, 0 <= includeIn2[1:numOfPoints] <= 1)

@NLconstraint(m, loop[i in 1:numOfPoints], includeIn1[i] + includeIn2[i] == 1)

@NLobjective(m, Min, sum(includeIn1[i] * (yPoints[i] - sum(cubicFunc[i,j]*mult1[j] for j in 1:4)) ^2 for i in 1:numOfPoints) +
                     sum(includeIn2[i] * (yPoints[i] - sum(cubicFunc[i,j]*mult2[j] for j in 1:4)) ^2 for i in 1:numOfPoints))

optimize!(m)

鉴于约束includeIn1includeIn2的最佳值为10(如果不是,这意味着将点分配给哪个组都没有关系),所以我们没有限制它们为二进制.另外,我使用非线性求解器,因为似乎无法将其重新表述为线性或二次优化任务.

Given the constraints includeIn1 and includeIn2 will be 1 or 0 in optimum (if they are not this means that it does not matter to which group you assign the point), so we do not have to constrain them to be binary. Also I use non-linear solver as the problem does not not seem to be possible to reformulate as linear or quadratic optimization task.

但是,我仅以上面的代码为例,说明如何将其记录下来.您制定的任务没有唯一的局部最小值(即全局最小值),而是多个局部最小值.因此,使用JuMP支持的标准非线性凸型求解器只能找到一个局部最优值(不一定是全局最优值).为了寻找全局最优解,您需要切换到全局求解器,例如 https://github.com/robertfeldt/BlackBoxOptim.jl .

However, I give the above code only as an example how you can write it down. The task you have formulated does not have a unique local minimum (that is a global one then), but several local minima. Therefore using standard non-linear convex solvers that JuMP supports will only find one local optimum (not necessarily a global one). In order to look for global optima you need to switch to global solvers like e.g. https://github.com/robertfeldt/BlackBoxOptim.jl.

这篇关于用线性/非线性回归拟合两条曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆