SciPy以单调递减Xs约束最小化

时间:2016-09-21 15:01:14

标签: python optimization scipy

我希望进行一项艰苦的优化,我使用SciPy来优化债券现金流的贴现因子(应用程序不那么重要,但如果感兴趣的话)。因此,基本上我采用多个已知值'P',其中P [i]是C [i]已知常数的函数,并且数组X(X [j] = x(t),其中x是时间的函数)。其中C [i]和X = P的总和。

希望这是有道理的,但基本上为了得到一个明智的结果,我想设置一个约束,其中X(我的x值数组)具有x [j]<的约束。 x [j-1],即x是单调递减的。

以下是优化功能的代码片段:

在[400]中:

import numpy as np
import pandas as pd
import scipy as s

def MyOptimization(X):
     P=np.array([99.,100.,105.,110.]) #just example known "P" array, in reality closer to 40 values
     c=np.array([1.25,4.,3.1,2.5]) #Cash flows for each P
     t=np.array([[1.2,2.,4.,10.0],[0.5,1.],[2.3,5.,10.5],[1.7]])   #time t of each cash flow, multiple per 'P'
                                                        #remember P=X(t)*c[i] and x(t) where x[i+1]<x[i]

     tlist=[] #t's will be used as index, so pulling individual values
     for i in t:
         for j in i:
             tlist.append(j)

     df=pd.DataFrame(data=X,index=tlist).drop_duplicates().sort() #dataframe to hold t (index) and x, x(t), and P(x,c) where c is known
     #print df
     sse=0
     for i in range(0,len(P)):
         pxi = np.sum(df.loc[t[i],0].values*c[i])+100*df.loc[t[i][-1],0]

         sse=sse+(pxi-P[i])**2 #want to minimize sum squared errors between calculated P(x,c) and known P
     return sse

cons=({'type':'ineq','fun': lambda x: x[1] < x[0]}) #trying to define constraint that x is decreasing with t

opti=s.optimize.minimize(MyOptimization,x0=[0.90,0.89,0.88,0.87,0.86,0.85,0.84,0.83,0.82,0.81],bounds=([0,1],)*10,constraints=cons)

在[401]中:

opti

缺货[401]:

status: 0
success: True
njev: 4
nfev: 69
 fun: 5.445290696814009e-15
   x: array([ 0.90092322,  0.89092322,  0.88092322,  0.94478062,  0.86301329,
    0.92834564,  0.84444848,  0.83444848,  0.96794781,  1.07317073])
message: 'Optimization terminated successfully.'
 jac: array([ -7.50609263e-05,  -7.50609263e-05,  -7.50609263e-05,
    -5.92906077e-03,   3.46914830e-04,   9.17475767e-03,
    -4.89504256e-04,  -4.89504256e-04,  -1.61263312e-02,
     8.35321580e-03,   0.00000000e+00])
 nit: 4

很明显,在结果中x数组没有减少的位置。 (尝试添加(0,1)边界但结果失败,所以暂时关注这个。

这里对于我真正不确定的约束的重要路线是:

cons=({'type':'ineq','fun': lambda x: x[1] < x[0]})

我尝试按照文档进行操作,但显然它没有用。

任何想法都非常感激。

2 个答案:

答案 0 :(得分:0)

试试

def con(x):
    for i in range(len(x)-1):
        if x[i] <= x[i+1]:
            return -1
    return 1

cons=({'type':'ineq','fun': con})

这应该拒绝那些没有像你想要的那样设置的列表,但我不确定scipy会不会喜欢它。

答案 1 :(得分:0)

我无法在下面的帖子中发表评论,但是您需要在其中放置from tkinter.messagebox import* ... i=i