2017-06-22 548 views
0

嘗試優化投資組合權重分配,通過使用cvxopt模塊限制風險來最大化我的回報函數。我的代碼如下:投資組合優化:如何使用cvxopt.solver.qp受到目標風險的最大回報?

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) 

我應該使用蒙特卡羅模擬,以獲得目標的風險,而收益最大化?解決這個問題的最好方法是什麼?謝謝。

回答

0

我認爲你正在試圖計算夏普投資組合。我相信它可以表明,這是相當於在回報(w'* rets = 1)上帶有等式約束的風險最小化的問題(w'p w)。這在二次程序員qp下更容易指定。

+0

我認爲夏普投資組合是我試圖找到的投資組合之一。但如果我想用盡我寬容的風險來最大化我的回報,我該如何找出體重? –

1

它不像人們想象的那樣簡單。典型的投資組合優化問題是最小化受到目標回報的風險,該目標回報是線性約束問題與二次客觀;即二次程序(QP)。

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

你想要的這裏,最大限度地回報受到目標的風險,是一個quadraticaly約束二次規劃(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有一個非常好的演習,如何爲這個特定的問題做到這一點。