给定一个投资组合的初始权重和收益的输入数组,我希望限制在优化过程中实现的交易数量(例如,总体收益的优化)。
import cvxpy as cp
weights = cp.variable(n)
init_weights = df.initial_weights.values
return = mu.T@weights
# I'm looking to constrain a variable 'nb_transactions'
nb_transactions = cp.sum(cp.abs(weights - init_weights) >= 0.00001)
prob = cp.Problem(cp.maximize(return), [nb_transactions <= 30])
prob.solve()
关于如何解决此问题的任何想法?谢谢
答案 0 :(得分:0)
使用整数买卖指标变量。
buys = cp.Variable(shape=(n,), boolean=True)
sells = cp.Variable(shape=(n,), boolean=True)
最大持有数量限制:
[cp.sum(buys) + cp.sum(sells) <= max_number_trades]
设置约束变量:
eps = 1e-5
w_trade = weights - init_weights
[-1 + eps <= w_trade - buys,
w_trade - buys <= 0,
-1 + eps <= -w_trade - sells,
-w_trade - sells <= 0,
0 <= buys + sells,
buys + sells <= 1]
注意,这个问题是一个混合整数问题。 如果问题仍然很小,ECOS BB 求解器可以解决这个问题。否则,您将需要商业级优化器。