'边界'不能在scipy.stats.minimize函数中工作

时间:2017-11-04 08:13:51

标签: python scipy

我在最小化函数中遇到了一个非常奇怪的问题。我写了下面的代码,希望从(0,1)输出结果。

cons = ({'type':'eq','fun':lambda x: 1-sum(x)})
bnds = [(0,1)]*len(w)
minS = minimize(min_S_function, w, method = 'SLSQP', bounds = bnds, constraints = cons)

然而,结果输出了许多非常小的数字而不是零,即使我将界限设置在0到1之间。为什么会这样?

In [68]:minS.x
Out[68]: 
array([  2.18674802e-14,  -2.31905438e-14,   4.05696128e-01,
     1.61295198e-14,   4.98954818e-02,  -2.75073615e-14,
     3.97195447e-01,   1.09796187e-14,  -4.33297358e-15,
     2.38805100e-14,   7.73037793e-15,   3.21824430e-14,
    -1.42202909e-14,  -1.08110329e-14,  -1.83513297e-14,
    -1.37745269e-14,   3.37854385e-14,   4.69473932e-14,
    -1.09088800e-15,  -1.57169147e-14,   7.47784562e-02,
     1.32782180e-02,   1.64441640e-14,   2.72140153e-15,
     5.23069695e-14,   5.91562687e-02,   2.16467506e-15,
    -6.50672519e-15,   2.53337977e-15,  -6.68019297e-14])

1 个答案:

答案 0 :(得分:0)

这是一个可以接受的解决方案!

那些迭代求解器只能保证局部最优近似解以及你得到的东西!

检查你的数字,你会看到,到下限(你的负向越界值)的距离在10^-14 = 0,00000000000001之内,这是一个可接受的错误(我们毕竟使用浮点数学) )。