You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mycode/盘荣博/线性规划/optimiaze模块/5.4投资的收益和风险模型2.py

27 lines
711 B

import numpy as np
from numpy import ones , zeros , c_,diag
from scipy.optimize import linprog
import matplotlib.pyplot as plt
c = np.append(zeros(5).tolist(),[1]).tolist()
print(c)
A=np.append(zeros(4).reshape(4,1),diag([0.025,0.015,0.055,0.026]),axis=1)
A=np.append(A,ones(4).reshape(4,1)*-1,axis=1).tolist()
Aeq =[[1,1.01,1.02,1.045,1.065,0]] ;beq=[1]
A.append([-0.05,-0.27,-0.19,0.185,-0.185,0])
print(A)
k=0.05;step = 0.005
b=([0]*4);b.append(-k)
print(b)
kk=[];ss=[]
while k<0.28:
res= linprog(c,A,b,Aeq,beq)
kk.append(k)
ss.append(res.fun)
print(res.fun)
k+=step
b[4]=-k
plt.plot(kk,ss,'r*')
plt.xlabel("$k$");plt.ylabel('$R$')
plt.savefig("figures5_1_2.png",dpi=500);plt.show()