【发布时间】:2021-08-14 02:44:49
【问题描述】:
我试图找到两个指数(股票和债券)的最佳权重,以尽可能接近地模拟股票回报。 (NOC 库存)。我在设置 PuLP 以最小化平方差之和时遇到问题。
目标函数: (最小化)(加权股票收益之和 + 加权债券收益之和 - 股票收益)^2
股票和债券的权重加到 1 并且都被限制在 (0,1) 之间
问题是我的回报都在每日列表中,所以我需要先计算权重并将列表中的所有回报相乘,然后才能对它们求和,然后在目标函数中使用它们。
from pulp import *
model = LpProblem("Style", LpMinimize)
wSPY = LpVariable("SPYw", lowBound=0,upBound=1, cat ='Continuous') #weights of SPY stock index
wSHY = LpVariable("SHYw", lowBound=0,upBound=1, cat ='Continuous') #weights of SHY bond index
#define Objective Function
model += ((sum(wSPY*SPYretls) + sum(wSHY*SHYretls)) - sum(NOCretls))**2
#SPYretls is the SPY stock index returns in a list
#SHYretls is the SHY stock index returns in a list
#NOCretls is the NOC stock returns in a list
#constraints
model += wSPY >= 0
model += wSHY >= 0
model += wSHY <= 1
model += wSPY <= 1
model.solve()
【问题讨论】:
标签: python optimization finance pulp