Python 中的约束最小二乘估计
我正在尝试使用 Scipy 执行约束最小二乘估计,以便所有系数都在 (0,1)
范围内,并且总和为 1
(此功能在 Matlab 的 LSQLIN
函数中实现)。
有人有使用 Python/Scipy 设置此计算的提示吗?我相信我应该使用 scipy.optimize.fmin_slsqp(),但我不完全确定我应该传递给它哪些参数。[1]
非常感谢您的帮助, Nick
[1] 如果没有引用的文本,fmin_slsqp
文档中的一个示例对我来说有点难以解析——而且我是 Scipy 的新手。
I'm trying to perform a constrained least-squares estimation using Scipy such that all of the coefficients are in the range (0,1)
and sum to 1
(this functionality is implemented in Matlab's LSQLIN
function).
Does anybody have tips for setting up this calculation using Python/Scipy. I believe I should be using scipy.optimize.fmin_slsqp()
, but am not entirely sure what parameters I should be passing to it.[1]
Many thanks for the help,
Nick
[1] The one example in the documentation for fmin_slsqp
is a bit difficult for me to parse without the referenced text -- and I'm new to using Scipy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
scipy-optimize-leastsq-with-bound-constraints 上 SO 给出
leastsq_bounds
,即leastsq
具有边界约束,例如 0 <= x_i <= 1。
可以用同样的方式添加它们总和为 1 的约束。
(我发现
leastsq_bounds
/ MINPACK 在 5d、10d、20d 的综合测试函数上表现良好;你有多少个变量?)
scipy-optimize-leastsq-with-bound-constraints on SO gives
leastsq_bounds
, which isleastsq
with bound constraints such as 0 <= x_i <= 1.
The constraint that they sum to 1 can be added in the same way.
(I've found
leastsq_bounds
/ MINPACK to be good on synthetic test functions in 5d, 10d, 20d;how many variables do you have ?)
看看这个教程,看来很清楚。
Have a look at this tutorial, it seems pretty clear.
由于 MATLAB 的
lsqlin
是有界线性最小二乘解算器,因此您需要查看scipy.optimize.lsq_线性。Since MATLAB's
lsqlin
is a bounded linear least squares solver, you would want to check out scipy.optimize.lsq_linear.使用
scipy.optimize.nnls
是一种可靠的方法。请注意,如果系数被限制为正且总和为 1,则它们会自动限制在区间 [0,1] 内,即不需要从上面额外约束它们。scipy.optimize.nnls
使用Lawson 和 Hanson 算法<自动使变量为正值/a>,而总和约束可以按照 this 中讨论的方式处理线程和这个。Scipy nnls 使用旧的 Fortran 后端,该后端显然广泛用于其他软件的 nnls 的等效实现中。
Non-negative least squares optimization using
scipy.optimize.nnls
is a robust way of doing it. Note that, if the coefficients are constrained to be positive and sum to unity, they are automatically limited to interval [0,1], that is one need not additionally constrain them from above.scipy.optimize.nnls
automatically makes variables positive using Lawson and Hanson algorithm, whereas the sum constraint can be taken care of as discussed in this thread and this one.Scipy
nnls
uses an old fortran backend, which is apparently widely used in equivalent implementations of nnls by other software.