5

根据文档,我可以使用 cvxopt 计算有效边界:

http://cvxopt.org/examples/book/portfolio.html

但是,我无法弄清楚如何添加约束,以便对特定资产的最大允许权重设置上限。使用 cvxopt 可以吗?

到目前为止,这是我的代码,它产生了一个没有约束的有效边界,除了我相信 b,它将权重的最大总和设置为 1。我不确定 G、h、A 和 mus 做什么,而文档没有真的不解释。mus的公式中的10**(5.0*t/N-1.0)从何而来?

from math import sqrt
from cvxopt import matrix
from cvxopt.blas import dot 
from cvxopt.solvers import qp, options 

# Number of assets
n = 4
# Convariance matrix
S = matrix( [[ 4e-2,  6e-3, -4e-3,   0.0 ], 
             [ 6e-3,  1e-2,  0.0,    0.0 ],
             [-4e-3,  0.0,   2.5e-3, 0.0 ],
             [ 0.0,   0.0,   0.0,    0.0 ]] )
# Expected return
pbar = matrix([.12, .10, .07, .03])

# nxn matrix of 0s
G = matrix(0.0, (n,n))
# Convert G to negative identity matrix
G[::n+1] = -1.0
# nx1 matrix of 0s
h = matrix(0.0, (n,1))
# 1xn matrix of 1s
A = matrix(1.0, (1,n))
# scalar of 1.0
b = matrix(1.0)

N = 100
mus = [ 10**(5.0*t/N-1.0) for t in range(N) ]
options['show_progress'] = False
xs = [ qp(mu*S, -pbar, G, h, A, b)['x'] for mu in mus ]
returns = [ dot(pbar,x) for x in xs ]
risks = [ sqrt(dot(x, S*x)) for x in xs ]

#Efficient frontier
plt.plot(risks, returns)
4

4 回答 4

5

You are using the quadratic programming solver of the cvxopt package, check out the documentation.

As you can see from the formula there, Gx <= h are the inequality constraints and Ax = b are the equality constraints. G and A are matrices, while h and b and are vectors.

Let's assume you want to restrict you're first asset to weights between 2% and 5%, you would formulate this as follows:

G = matrix([[-1, 0, 0, 0],
            [ 1, 0, 0, 0]])

h = matrix([[-0.02],
            [0.05]])

Note that the first row is turning the inequality condition into Gx >= h by multiplying by -1.

The conditions you have above set all assets to bounds between 0% and 100%, the common no short selling condition.

The formula your using is explained here: your muis qin the Wikipedia article and therefore a risk-tolerance parameter. It doesn't really matter if you attach it to the covariance or return part of the target function. It just means that you are either walking from the top right corner to the bottom left or the other way round for each value of mu. So it's just a function that gives you a risk-tolerance from very small to very big. I think there is a mistake though in that the series start at 0.1 but should really start at 0.

于 2014-09-05T07:32:24.703 回答
1

我知道这很旧,但是很难在任何地方找到一个好的约束均值方差示例;

那么约束部分不是那样工作的;

在这种情况下,G Mat 应为 4 X 4,h 应为 1 X 4,如下所示如果需要最小阈值,则可以输入“h”矩阵,如下面的示例所示(20%)。然而,我无法设法添加最大约束。

G = matrix([[-1.0, 0.0, 0.0, 0.0],
       [0.0, -1.0, 0.0, 0.0],
       [0.0, 0.0, -1.0, 0.0],
       [0.0, 0.0, 0.0, -1.0]])


 h = matrix([-0.2, 0.0, 0.0, 0.0])
于 2020-06-10T16:38:59.817 回答
0

主要是组织您的 ineq 和 eq 约束,例如,如果您想允许卖空 2 种资产,则 -1<=x(i)<=1 和 sum(x(i))=1。因此解决 ineq,一个有 4 个 ineq 所以 G 必须是 4x2 而 h 是 4x1。检查它... G=[I/-I] 其中 I 是 G=[[1,0],[0,1],[-1,0],[0,-1]] xs [x1 ,x2] 和 h=[1,1,1,1] 如果你使用 Gx<=h 你得到 x1<=1, x2<=1 x1>=-1 和 x2>=-1/。相等时间是 Ax=b, b=1 和 A=[1,1]..aagin 检查它,x1 +x2 = 1 匹配约束。P 和 Q 你已经有了。您现在可以解决问题了。

于 2015-07-07T10:58:39.217 回答
0

我认为标记为正确的回复实际上给出了一个不正确的 example.covxopt 矩阵是列专业的。所以样本 G 和 h 需要转置。

于 2020-04-29T04:29:31.893 回答