简体   繁体   English

Scipy.optimize.minimize 目标 function 必须返回一个标量

[英]Scipy.optimize.minimize Objective function must return a scalar

I try to optimize my function:我尝试优化我的 function:

def get_ret(weights):
   weights = np.array(weights)
   ret = np.sum(log_ret.mean() * weights)*252
   vol = np.sqrt(np.dot(weights.T,np.dot(log_ret.cov()*252,weights)))
   sr = ret / vol
   return [ret,vol,sr]

def neg_sharp(weights):
   return get_ret(weights[2]) * -1

and my constraints is:我的限制是:

def check_sum(weights):
   return np.sum(weights) -1


cons = ({"type":"eq", "fun":check_sum})

my bonds is: bounds = ((0,1),(0,1),(0,1),(0,1))我的债券是:bounds = ((0,1),(0,1),(0,1),(0,1))

and:和:

init_guess = np.array([.25,.25,.25,.25]) init_guess = np.array([.25,.25,.25,.25])

so i run this:所以我运行这个:

opt_res = minimize(fun=neg_sharp,x0=init_guess.flatten(),
         method="SLSQP",bounds=bounds,constraints=cons)

and got this error:并得到这个错误:

ValueError: Objective function must return a scalar ValueError: Objective function 必须返回一个标量

It seems like the problem is here return get_ret(weights)[2] * -1似乎问题出在这里return get_ret(weights)[2] * -1
get_ret returns a list, I guess you want to take one element out of this list (the 3rd one?) and multiply by -1. get_ret返回一个列表,我猜你想从这个列表中取出一个元素(第三个?)并乘以 -1。

Try:尝试:

def get_ret(weights):
   weights = np.array(weights)
   ret = np.sum(log_ret.mean() * weights)*252
   vol = np.sqrt(np.dot(weights.T,np.dot(log_ret.cov()*252,weights)))
   sr = ret / vol
   return [ret,vol,sr]

def neg_sharp(weights):
   return get_ret(weights)[2] * -1

ok.好的。 I'm sorry.对不起。 this is my fault.这是我的错。

return get_ret(weights[2]) * -1

must change to:必须改为:

return get_ret(weights)[2] * -1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Scipy.optimize.minimize目标函数ValueError - Scipy.optimize.minimize objective function ValueError 将约束应用于scipy.optimize.minimize?中的目标函数? 如0 &lt;=目标&lt;= 1 - Apply constraints to the objective function in scipy.optimize.minimize? Such as 0 <= objective <= 1 scipy.optimize.minimize 跟踪目标函数 - scipy.optimize.minimize keep track of objective function scipy.optimize.minimize 具有 2 个返回值的处理函数 - scipy.optimize.minimize handling function with 2 return values 函数scipy.optimize.minimize的选项 - options of the function scipy.optimize.minimize 当我使用 scipy.optimize.minimize() 最小化它时,为什么我的目标 function 的矩阵参数发生了变化? - Why is a matrix argument of my objective function changed when I minimize it with scipy.optimize.minimize()? scipy.optimize.minimize无法使用2D或标量边界 - scipy.optimize.minimize cannot use 2D or scalar bounds scipy.minimize ValueError:用户提供的目标 function 必须返回一个标量值 - scipy.minimize ValueError: The user-provided objective function must return a scalar value 当你想要计算梯度和目标函数时,如何使用scipy.optimize.minimize函数? - How to use scipy.optimize.minimize function when you want to compute gradient along with the objective function? 除了自变量外,如何为scipy.optimize.minimize的目标函数提供附加输入 - How to give additional input to objective function of scipy.optimize.minimize other than independent variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM