Python 3.x 带矩阵乘法的Scipy优化
我曾尝试使用Python 3.x 带矩阵乘法的Scipy优化,python-3.x,scipy-optimize,Python 3.x,Scipy Optimize,我曾尝试使用spick.optimize.minimize来解决矩阵乘法优化问题,但是,结果给了我一个维数错误,有人能帮我解决吗 import numpy as np from scipy.optimize import minimize # define known variables, mu, sigma, rf mu = np.matrix([[0.12], [0.08], [0.05]]) sigma = np.ma
spick.optimize.minimize
来解决矩阵乘法优化问题,但是,结果给了我一个维数错误,有人能帮我解决吗
import numpy as np
from scipy.optimize import minimize
# define known variables, mu, sigma, rf
mu = np.matrix([[0.12],
[0.08],
[0.05]])
sigma = np.matrix([[0.5, 0.05, 0.03],
[0.05, 0.4, 0.01],
[0.03, 0.01, 0.2]])
rf = 0.02
def objective_fun(x):
'''
This is the objective function
'''
s = np.sqrt(x.T * sigma * x)/(mu.T * x - rf)
return s
def constraint(x):
con = 1
for i in np.arange(0,3):
con = con - x[i]
return con
# set up the boundaries for x
bound_i = (0, np.Inf)
bnds = (bound_i, bound_i, bound_i)
#set up the constraints for x
con = {'type':'eq', 'fun':constraint}
# initial guess for variable x
x = np.matrix([[0.5],
[0.3],
[0.2]])
sol = minimize(objective_fun, x, method = 'SLSQP', bounds = bnds, constraints = con)
这个错误给了我:
这意味着我的函数可以正确地进行矩阵乘法,那么这里的代码有什么问题呢?最小化()的文档说
x0
应该是一个(n,)
形状的数组,但您试图将其视为(3,1)
数组。我不确定minimize()
的内部工作原理,但我怀疑当它跨过fit参数的不同值时,它会转换为它认为需要的格式。无论如何,下面的一些小的修正使得代码能够正常工作
import numpy as np
from scipy.optimize import minimize
# define known variables, mu, sigma, rf
mu = np.matrix([[0.12],
[0.08],
[0.05]])
sigma = np.matrix([[0.5, 0.05, 0.03],
[0.05, 0.4, 0.01],
[0.03, 0.01, 0.2]])
rf = 0.02
def objective_fun(x):
'''
This is the objective function
'''
x = np.expand_dims(x, 1) # convert the (3,) shape to (3,1). Then we can do our normal matrix math on it
s = np.sqrt(x.T * sigma * x)/(mu.T * x - rf) # Transposes so the shapes are correct
return s
def constraint(x):
con = 1
for i in np.arange(0,3):
con = con - x[i]
return con
# set up the boundaries for x
bound_i = (0, np.Inf)
bnds = (bound_i, bound_i, bound_i)
#set up the constraints for x
con = {'type':'eq', 'fun':constraint}
# initial guess for variable x
x = np.array([0.5, 0.3, 0.2]) # Defining the initial guess as an (3,) array)
sol = minimize(objective_fun, x, method = 'SLSQP', bounds = bnds, constraints = con)
print(sol) # and the solution looks reasonable
输出
fun:5.86953830952583
jac:数组([-1.70555401,-1.70578796,-1.70573896])
消息:“优化已成功终止。”
nfev:32
nit:6
njev:6
状态:0
成功:真的
x:阵列([0.42809911,0.29522438,0.27667651])
看一看我对你需要做什么的解释 始终包括完整的回溯这部分
x.T*sigma*x
是(在维度上)[(1,3)x(3,3)x(1,3)],从左到右产生(1,3)x(3,3)>>(1,3),现在它尝试将(1,3)乘以(1,3)
optimize_fun(x)
matrix([[5.90897598]])
import numpy as np
from scipy.optimize import minimize
# define known variables, mu, sigma, rf
mu = np.matrix([[0.12],
[0.08],
[0.05]])
sigma = np.matrix([[0.5, 0.05, 0.03],
[0.05, 0.4, 0.01],
[0.03, 0.01, 0.2]])
rf = 0.02
def objective_fun(x):
'''
This is the objective function
'''
x = np.expand_dims(x, 1) # convert the (3,) shape to (3,1). Then we can do our normal matrix math on it
s = np.sqrt(x.T * sigma * x)/(mu.T * x - rf) # Transposes so the shapes are correct
return s
def constraint(x):
con = 1
for i in np.arange(0,3):
con = con - x[i]
return con
# set up the boundaries for x
bound_i = (0, np.Inf)
bnds = (bound_i, bound_i, bound_i)
#set up the constraints for x
con = {'type':'eq', 'fun':constraint}
# initial guess for variable x
x = np.array([0.5, 0.3, 0.2]) # Defining the initial guess as an (3,) array)
sol = minimize(objective_fun, x, method = 'SLSQP', bounds = bnds, constraints = con)
print(sol) # and the solution looks reasonable