python - 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]
解决方案
推荐阅读
- django - 如何在 Django 中更改语言的本地路径?
- python - Python 3.7 Windows 不支持 dbm.gnu 吗?
- matlab - 如何在 MATLAB 仿真运行时存储特定时间步长的变量值?
- sql - 我无法在 if 语句中放置选择查询。我应该如何更换它?
- python - 如何使用 Python 下载 mpeg/mp3?
- java - Jackson:根据字段类型添加后缀
- server - 虽然给了 REST 参数 autoStart=false,但是当我们重启服务器时,流源开始拉入 Ant Media Server
- java - 在 jsp 中获取 LocalTime 输入
- javascript - 使用 Javascript/jQuery 动态添加行跨度
- javascript - 将数组与javascript中的嵌套数组进行比较