投资组合优化:如何在使用 cvxopt.solver.qp 承受目标风险的同时最大化 return?
portfolio optimization: how to maximize return while subject to target risk using cvxopt.solver.qp?
尝试在此处优化投资组合权重分配,通过使用 cvxopt 模块限制风险来最大化我的 return 函数。我的代码如下:
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)
我是否应该使用 Monte Carlo 模拟来获得目标风险,同时最大化 return?解决这个问题的最佳方法是什么?谢谢。
我认为您正在尝试计算夏普投资组合。我相信可以证明这是一个等价于在 return (w'*rets = 1) 上具有等式约束的风险最小化 (w'Pw) 的问题.这将更容易在二次编程器 qp 下指定。
它并不像人们想象的那么简单。典型的投资组合优化问题是根据目标 return 最小化风险,这是一个 线性约束 问题,具有 二次 objective;即,二次程序(QP)。
minimize x^T.P.x
subject to sum(x_i) = 1
avg_ret^T.x >= r_min
x >= 0 (long-only)
你在这里想要的是 return 最大化目标风险,是一个 二次约束 二次规划 (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. This blog post 中看到的那样,它有一个非常好的演练,说明如何针对这个特定问题执行此操作。
尝试在此处优化投资组合权重分配,通过使用 cvxopt 模块限制风险来最大化我的 return 函数。我的代码如下:
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)
我是否应该使用 Monte Carlo 模拟来获得目标风险,同时最大化 return?解决这个问题的最佳方法是什么?谢谢。
我认为您正在尝试计算夏普投资组合。我相信可以证明这是一个等价于在 return (w'*rets = 1) 上具有等式约束的风险最小化 (w'Pw) 的问题.这将更容易在二次编程器 qp 下指定。
它并不像人们想象的那么简单。典型的投资组合优化问题是根据目标 return 最小化风险,这是一个 线性约束 问题,具有 二次 objective;即,二次程序(QP)。
minimize x^T.P.x
subject to sum(x_i) = 1
avg_ret^T.x >= r_min
x >= 0 (long-only)
你在这里想要的是 return 最大化目标风险,是一个 二次约束 二次规划 (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. This blog post 中看到的那样,它有一个非常好的演练,说明如何针对这个特定问题执行此操作。