python - 投资组合优化 : how to maximize return while subject to target risk using cvxopt.solver.qp?

标签 python optimization portfolio cvxopt

尝试在这里优化投资组合权重分配,通过使用 cvxopt 模块限制风险来最大化我的返回函数。我的代码如下:

from cvxopt import matrix, solvers, spmatrix, sparse
from cvxopt.blas import dot
import numpy
import pandas as pd
import numpy as np
from datetime import datetime

solvers.options['show_progress'] = False
# solves the QP, where x is the allocation of the portfolio:
# minimize   x'Px + q'x
# subject to Gx <= h
#            Ax == b
#
# Input:  n       - # of assets
#         avg_ret - nx1 matrix of average returns
#         covs    - nxn matrix of return covariance
#         r_min   - the minimum expected return that you'd
#                   like to achieve
# Output: sol - cvxopt solution object

dates = pd.date_range('2000-01-01', periods=6)
industry = ['industry', 'industry', 'utility', 'utility', 'consumer']
symbols = ['A', 'B', 'C', 'D', 'E']
zipped = list(zip(industry, symbols))
index = pd.MultiIndex.from_tuples(zipped)

noa = len(symbols)

data = np.array([[10, 11, 12, 13, 14, 10],
                 [10, 11, 10, 13, 14, 9],
                 [10, 10, 12, 13, 9, 11],
                 [10, 11, 12, 13, 14, 8],
                 [10, 9, 12, 13, 14, 9]])

market_to_market_price = pd.DataFrame(data.T, index=dates, columns=index)
rets = market_to_market_price / market_to_market_price.shift(1) - 1.0
rets = rets.dropna(axis=0, how='all')

# covariance of asset returns
P    = matrix(rets.cov().values)


n = len(symbols)
q = matrix(np.zeros((n, 1)), tc='d')
G = matrix(-np.eye(n), tc='d')
h = matrix(-np.zeros((n, 1)), tc='d')
A = matrix(1.0, (1, n))
b = matrix(1.0)
sol = solvers.qp(P, q, G, h, A, b)

我应该使用蒙特卡罗模拟来获得目标风险,同时最大化返回吗?解决这个问题的最佳方法是什么?谢谢。

最佳答案

这并不像人们想象的那么简单。典型的投资组合优化问题是在目标返回的前提下最小化风险,这是一个具有二次目标线性约束问题;即二次规划(QP)。

minimize        x^T.P.x
subject to      sum(x_i) = 1
                avg_ret^T.x >= r_min
                x >= 0 (long-only)

您在这里想要的是一个二次约束二次规划(QCQP),以在目标风险的情况下最大化返回,如下所示:

maximize        avg_ret^T.x
subject to      sum(x_i) = 1
                x^T.P.x <= risk_max
                x >= 0 (long-only)

通过凸二次约束,优化发生在包含二阶锥体因子的更复杂的锥体上。如果要继续使用 cvxopt,则必须将 QCQP 转换为二阶锥程序 (SOCP),因为 cvxopt 没有针对 QCQP 的显式求解器。带有 cvxopt 的 SOCP 具有与典型 QP 不同的矩阵语法,如您从 documentation 中看到的那样。这个blog post关于如何针对这个特定问题执行此操作有一个非常好的演练。

关于python - 投资组合优化 : how to maximize return while subject to target risk using cvxopt.solver.qp?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44701092/

相关文章:

r - 投资组合分析包中的自定义预期返回

c# - 连通分量标记算法优化

python - 在优化的同时保持代码可读性

r - 在 R 中使用协方差矩阵进行投资组合优化

python - 为什么我的查询变成 bool 值,如何防止这种情况发生,以便我可以迭代它?

javascript - 使用循环墙时间优化函数性能

mysql - 支持股票拆分/合并的股票投资组合数据库设计

python - 安装 django-tracking 时遇到错误

python - 堆叠标题,而不是两列

python - 如何从 Pandas 中丝网打印两列值?