我想使用Markowitz理论(对于给定的收入,通过Markowitz方法将风险最小化= 15%)和Scipy.minimize优化我的投资组合
我有风险功能
def objective(x):
x1=x[0];x2=x[1];x3=x[2]; x4=x[3]
return 1547.87020*x1**2 + 125.26258*x1*x2 + 1194.3433*x1*x3 + 63.6533*x1*x4 \
+ 27.3176649*x2**2 + 163.28848*x2*x3 + 4.829816*x2*x4 \
+ 392.11819*x3**2 + 56.50518*x3*x4 \
+ 34.484063*x4**2
部分股票的总和(%)= 1
def constraint1(x):
return (x[0]+x[1]+x[2]+x[3]-1.0)
有限制的收入功能
def constraint2(x):
return (-1.37458*x[0] + 0.92042*x[1] + 5.06189*x[2] + 0.35974*x[3] - 15.0)
我用以下方法进行测试:
x0=[0,1,1,0] #Initial value
b=(0.0,1.0)
bnds=(b,b,b,b)
con1={'type':'ineq','fun':constraint1}
con2={'type':'eq','fun':constraint2}
cons=[con1,con2]
sol=minimize(objective,x0,method='SLSQP',\
bounds=bnds,constraints=cons)
我的结果是:
fun: 678.5433939
jac: array([1383.25920868, 222.75363159, 1004.03005219, 130.30312347])
message: 'Positive directional derivative for linesearch'
nfev: 216
nit: 20
njev: 16
status: 8
success: False
x: array([0., 1., 1., 1.])
但是如何?投资组合的部分总和不能超过1(现在股票2的部分=股票3 =股票4 = 100%)。它的约束1。问题出在哪里?
答案 0 :(得分:1)
输出显示“成功:错误” 因此,它告诉您它未能找到解决问题的方法。
还有,你为什么放 con1 = {'type':'ineq','fun':constraint1}
你不想要 con1 = {'type':' eq ','fun':constraint1}
我使用method ='BFGS'成功了
答案 1 :(得分:1)
由于第一个约束(a-b >= 0 => a>b)
的错误定义,您的代码返回的值不符合您的约束,因此在您的情况下,a=1
(不等式的顺序很重要)。另一方面,您的x0
还必须遵守您的约束条件和sum([0,1,1,0]) = 2 > 1
。
我对代码进行了一些改进,并修复了上述问题,但我仍然认为您需要查看第二个约束:
import numpy as np
from scipy.optimize import minimize
def objective(x):
x1, x2, x3, x4 = x[0], x[1], x[2], x[3]
coefficients = np.array([1547.87020, 125.26258, 1194.3433, 63.6533, 27.3176649, 163.28848, 4.829816, 392.11819, 56.50518, 34.484063])
xs = np.array([ x1**2, x1*x2, x1*x3, x1*x4, x2**2, x2*x3, x2*x4, x3**2, x3*x4, x4**2])
return np.dot(xs, coefficients)
const1 = lambda x: 1 - sum(x)
const2 = lambda x: np.dot(np.array([-1.37458, 0.92042, 5.06189, 0.35974]), x) - 15.0
x0 = [0, 0, 0, 0] #Initial value
b = (0.0, 1.0)
bnds = (b, b, b, b)
cons = [{'type':'ineq','fun':const1}, {'type':'eq', 'fun':const2}]
# minimize
sol = minimize(objective,
x0,
method = 'SLSQP',
bounds = bnds,
constraints = cons)
print(sol)
输出:
fun: 392.1181900000138
jac: array([1194.34332275, 163.28847885, 784.23638535, 56.50518036])
message: 'Positive directional derivative for linesearch'
nfev: 92
nit: 11
njev: 7
status: 8
success: False
x: array([0.00000000e+00, 5.56638069e-14, 1.00000000e+00, 8.29371293e-14])