How to use cvxopt to optimize average variance with constraints?

I can use cvxopt to calculate the effective border in documents:

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

However, I cannot figure out how to add a restriction, so there is an upper bound for a certain allowable weight of the asset. Is cvxopt possible?

Here is my code so far, which creates an efficient border without restrictions, except that I believe b, which sets the maximum sum of weights to 1. I'm not sure what G, h, A and mus do, and the documents really don't explain . Where is 10 ** (5.0 * t / N-1.0) in the formula for mus?

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
source share
2

cvxopt, .

, Gx <= h - , Ax = b - . G A , h b .

, 2% 5%, :

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

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

, Gx >= h -1.

, , 0% 100%, .

: mu q , , . , . , , mu. , . , , 0,1, 0.

+3

, ineq eq, , , -1 <= x (i) <= 1 sum (x (i)) = 1. , ineq, 4 ineq, G 4x2 h 4x1. ... G = [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 . .

0

Source: https://habr.com/ru/post/1541795/


All Articles