scipy.optimize问题使用矩阵作为输入,范围,约束 [英] Problems with scipy.optimize using matrix as input, bounds, constraints

查看:371
本文介绍了scipy.optimize问题使用矩阵作为输入,范围,约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我过去曾经使用Python进行优化;但是,我现在尝试使用矩阵作为目标函数的输入,并设置各个元素的值以及矩阵中每一行的值之和的界限,并且遇到了问题.

I have used Python to perform optimization in the past; however, I am now trying to use a matrix as the input for the objective function as well as set bounds on the individual element values and the sum of the value of each row in the matrix, and I am encountering problems.

具体来说,我想传递目标函数ObjFunc三个参数-wpret-然后最小化此函数的值(从技术上讲,我试图通过最小化来最大化该函数通过调整w的值,以w的所有元素应落在[0,1]的范围和w中每一行的总和应加为约束的界限为限,调整w的值到1.

Specifically, I would like to pass the objective function ObjFunc three parameters - w, p, ret - and then minimize the value of this function (technically I am trying to maximize the function by minimizing the value of -1*ObjFunc) by adjusting the value of w subject to the bound that all elements of w should fall within the range [0, 1] and the constraint that sum of each row in w should sum to 1.

我在下面提供了一段简化的示例代码,以演示我遇到的问题.如您所见,我正在使用scipy.opimize中的minimize函数.这些问题始于目标函数x = np.dot(p, w)的第一行,在该行中,优化过程尝试将矩阵展平为一维向量-在不执行优化的情况下调用函数时不会发生此问题. bounds = bconstraints = c也会产生错误.

I have included a simplified piece of example code below to demonstrate the issue I'm encountering. As you can see, I am using the minimize function from scipy.opimize. The problems begin in the first line of objective function x = np.dot(p, w) in which the optimization procedure attempts to flatten the matrix into a one-dimensional vector - a problem that does not occur when the function is called without performing optimization. The bounds = b and constraints = c are both producing errors as well.

我知道我在实现此优化方法方面犯了一个基本错误,希望能提供任何见解.

I know that I am making an elementary mistake in how I am approaching this optimization and would appreciate any insight that can be offered.

import numpy as np
from scipy.optimize import minimize


def objFunc(w, p, ret):
    x = np.dot(p, w)
    y = np.multiply(x, ret)
    z = np.sum(y, axis=1)

    r = z.mean()
    s = z.std()
    ratio = r/s

    return -1 * ratio   


# CREATE MATRICES
# returns, ret, of each of the three assets in the 5 periods
ret = np.matrix([[0.10, 0.05, -0.03], [0.05, 0.05, 0.50], [0.01, 0.05, -0.10], [0.01, 0.05, 0.40], [1.00, 0.05, -0.20]])

# probability, p, of being in each stae {X, Y, Z} in each of the 5 periods
p = np.matrix([[0,0.5,0.5], [0,0.6,0.4], [0.2,0.4,0.4], [0.3,0.3,0.4], [1,0,0]])

# initial equal weights, w
w = np.matrix([[0.33333,0.33333,0.33333],[0.33333,0.33333,0.33333],[0.33333,0.33333,0.33333]])

# OPTIMIZATION
b = [(0, 1)] 
c = ({'type': 'eq', 'fun': lambda w_: np.sum(w, 1) - 1})

result = minimize(objFunc, w, (p, ret), method = 'SLSQP', bounds = b, constraints = c)

推荐答案

稍微深入一下代码. minimize调用optimize._minimize._minimize_slsqp.它要做的第一件事是:

Digging into the code a bit. minimize calls optimize._minimize._minimize_slsqp. One of the first things it does is:

x = asfarray(x0).flatten()

因此,您需要设计objFunc以与w的扁平版本一起使用.在该功能开始时对其进行重塑可能就足够了.

So you need to design your objFunc to work with the flattened version of w. It may be enough to reshape it at the start of that function.

我从IPython会话中读取了代码,但您也可以在scipy目录中找到它:

I read the code from a IPython session, but you can also find it in your scipy directory:

/usr/local/lib/python3.5/dist-packages/scipy/optimize/_minimize.py

这篇关于scipy.optimize问题使用矩阵作为输入,范围,约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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