首页 > 解决方案 > Python CVXPY 最小化百分位变量投资组合优化

问题描述

我有一组投资组合回报:

    total
Scenario    
20160608    -131,225.96
20160609    -3,526,055.32
20160610    -23,529,796.59
20160613    -21,268,638.69
20160614    -10,528,552.12
... ...
20210528    5,643,877.68
20210531    236,667.34
20210601    8,244,154.88
20210602    -2,213,117.88
20210603    4,807,859.21

和一组对冲工具记录回报:

        SPY US Equity   EEM US Equity   TLT US Equity   DXY Index   HYG US Equity
Scenario                    
20160608    0.00    0.01    0.01    -0.00   0.00
20160609    -0.00   -0.01   0.01    0.00    -0.00
20160610    -0.01   -0.03   0.00    0.01    -0.00
20160613    -0.01   -0.01   0.00    -0.00   -0.01
20160614    -0.00   -0.00   -0.00   0.01    -0.00
... ... ... ... ... ...
20210528    0.00    0.01    -0.00   0.00    -0.00
20210531    0.00    0.00    0.00    -0.00   0.00
20210601    -0.00   0.02    -0.00   0.00    -0.00
20210602    0.00    0.00    0.00    0.00    0.00
20210603    -0.00   -0.01   -0.00   0.01    -0.00

考虑到这些工具在投资组合中的特定权重,我使用它来计算历史 VaR:

weights = np.array([-0.5, -0.5, 0.5, 0.5, -0.5])
var = np.quantile((hedge_returns*weights).sum(axis=1) + port_returns, confidence)

我正在尝试运行优化以从对冲投资组合中选择最佳工具权重,以生成 var 变量的最小值。

我尝试使用 CVXPY 来优化它,但是我得到了这个错误: NotImplementedError: Strict inequalities are not allowed。

我相信这是因为 CVXPY 没有分位数功能。

n = 5
confidence = 0.05
weights = cp.Variable(n)

var = np.quantile(((hedge_returns*np.array(weights)).sum(axis=1) + port_returns), confidence)

obj = cp.Minimize(var)

prob = cp.Problem(objective=obj)

prob.solve()  # Returns the optimal value.
print("status:", prob.status)
print("optimal value", prob.value)
print("optimal var", x.value, y.value)

这里有替代方法的任何想法吗?

谢谢!

其他想法,对结果矩阵进行排序,并选择正确的间隔,但排序我正在努力。

percentile = 0.05
length = 1302
line = round(percentile * length)

var = np.sort(sum((hedge_returns@weights).T) + port_returns)[line]

标签: pythonoptimizationquantilecvxpy

解决方案


推荐阅读