How to use the cvxopt.blas.dot function in cvxopt

To help you get started, we’ve selected a few cvxopt examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github manuwhs / Trapyng / libs / MarketModels / CAPM / CAPM_eff.py View on Github external
for mu in mus]
    else:    
        portfolios = [solvers.qp(mu*S, -pbar, G, h, A,b)['x'] 
                      for mu in mus]                  
    # Portfolios contains now the list of optimal allocations               
    ## Transform the cvxopt.base.matrix allocations to nparrays
    
    allocations = []
    for port in portfolios:
        allocations.append(np.array(port))
        
    ###################################################
    ######## CALCULATE Optimal Portfolio  #############
    ###################################################
    ## CALCULATE RISKS AND RETURNS FOR FRONTIER with Rf = 0
    returns = [blas.dot(pbar, x) for x in portfolios]
    risks = [np.sqrt(blas.dot(x, S*x)) for x in portfolios]
    
    ## CALCULATE THE 2ND DEGREE POLYNOMIAL OF THE FRONTIER CURVE
    m1 = np.polyfit(returns, risks, 2)
    x1 = np.sqrt(m1[2] / m1[0])
    
    # CALCULATE THE OPTIMAL PORTFOLIO
    wt = solvers.qp(opt.matrix(x1 * S), -pbar, G, h, A, b)['x']
    return np.asarray(wt).T, allocations
github cogeorg / BlackRhino / examples / qe_casp_many_assets / src / functions / portfolio.py View on Github external
N = 100
	mus = [10**(5.0 * t/N - 1.0) for t in range(N)]
	# Convert to cvxopt matrices
	S = opt.matrix(np.cov(returns))
	pbar = opt.matrix(np.mean(returns, axis=1))
	# Create constraint matrices
	G = -opt.matrix(np.eye(n))   # negative n x n identity matrix
	h = opt.matrix(0.0, (n ,1))
	A = opt.matrix(1.0, (1, n))
	b = opt.matrix(1.0)
	# Calculate efficient frontier weights using quadratic programming
	portfolios = [solvers.qp(mu*S, -pbar, G, h, A, b)['x']
	              for mu in mus]
				  ## CALCULATE RISKS AND RETURNS FOR FRONTIER
	returns = [blas.dot(pbar, x) for x in portfolios]
	risks = [np.sqrt(blas.dot(x, S*x)) for x in portfolios]
	## CALCULATE THE 2ND DEGREE POLYNOMIAL OF THE FRONTIER CURVE
	m1 = np.polyfit(returns, risks, 2)
	x1 = np.sqrt(m1[2] / m1[0])
	# CALCULATE THE OPTIMAL PORTFOLIO
	wt = solvers.qp(opt.matrix(x1 * S), -pbar, G, h, A, b)['x']
	return np.asarray(wt), returns, risks
github cvxopt / cvxopt / examples / doc / chap4 / acent.py View on Github external
for iter in range(MAXITERS):
        
        # Gradient is g = A^T * (1./(b-A*x)).
        d = (b-A*x)**-1
        g = A.T * d

        # Hessian is H = A^T * diag(1./(b-A*x))^2 * A.
        Asc = mul( d[:,n*[0]], A)
        blas.syrk(Asc, H, trans='T')

        # Newton step is v = H^-1 * g.
        v = -g
        lapack.posv(H, v)

        # Directional derivative and Newton decrement.
        lam = blas.dot(g, v)
        ntdecrs += [ sqrt(-lam) ]
        print("%2d.  Newton decr. = %3.3e" %(iter,ntdecrs[-1]))
        if ntdecrs[-1] < TOL: return x, ntdecrs

        # Backtracking line search.
        y = mul(A*v, d)
        step = 1.0
        while 1-step*max(y) < 0: step *= BETA 
        while True:
            if -sum(log(1-step*y)) < ALPHA*step*lam: break
            step *= BETA
        x += step*v
github cvxopt / cvxopt / src / python / misc.py View on Github external
def sdot(x, y, dims, mnl = 0):
    """
    Inner product of two vectors in S.
    """
    
    ind = mnl + dims['l'] + sum(dims['q'])
    a = blas.dot(x, y, n = ind)
    for m in dims['s']:
        a += blas.dot(x, y, offsetx = ind, offsety = ind, incx = m+1, 
            incy = m+1, n = m)
        for j in range(1, m):
            a += 2.0 * blas.dot(x, y, incx = m+1, incy = m+1, 
                offsetx = ind+j, offsety = ind+j, n = m-j)
        ind += m**2
    return a
github cajohnst / Optimized_FX_Portfolio / Optimize_FX_Portfolio.py View on Github external
mus = [mus_min + i*mus_step for i in range(N)]
	# cvxopt constraints: Gx <= h, Ax <= b
	G = opt.matrix(np.concatenate((-np.transpose(pbar),-np.identity(n)),0))
	A = opt.matrix(1.0, (1, n))
	b = opt.matrix(1.0)

	# Calculate efficient frontier weights using quadratic programming
	portfolios=[]
	for r_min in mus:
		h=opt.matrix(np.concatenate((-np.ones((1,1))*r_min,np.zeros((n,1))),0))
		sol = solvers.qp(S, -pbar, G, h, A, b)['x']
	   
		portfolios.append(sol)

	# Calculate risks and returns for the frontier
	returns = [blas.dot(pbar, x) for x in portfolios]
	risks = [np.sqrt(blas.dot(x, S*x)) for x in portfolios]

	return risks, returns
github cvxopt / cvxopt / src / python / cvxprog.py View on Github external
ind += m
            ind2 += m*m
        misc.scale(s, W, trans = 'T')

        blas.copy(lmbda, z, n = mnl + dims['l'] + sum(dims['q']))
        ind = mnl + dims['l'] + sum(dims['q'])
        ind2 = ind
        for m in dims['s']:
            blas.scal(0.0, z, offset = ind2)
            blas.copy(lmbda, z, offsetx = ind, offsety = ind2, n = m, 
                incy = m+1)
            ind += m
            ind2 += m*m
        misc.scale(z, W, inverse = 'I')

        gap = blas.dot(lmbda, lmbda) 
github cvxopt / cvxopt / examples / doc / chap8 / qcl1.py View on Github external
def f(x, y, z):

            # z := - W**-T * z 
            z[:n] = -div( z[:n], d1 )
            z[n:2*n] = -div( z[n:2*n], d2 )
            z[2*n:] -= 2.0*v*( v[0]*z[2*n] - blas.dot(v[1:], z[2*n+1:]) ) 
            z[2*n+1:] *= -1.0
            z[2*n:] /= beta

            # x := x - G' * W**-1 * z
            x[:n] -= div(z[:n], d1) - div(z[n:2*n], d2) + As.T * z[-(m+1):]
            x[n:] += div(z[:n], d1) + div(z[n:2*n], d2) 

            # Solve for x[:n]:
            #
            #    S*x[:n] = x[:n] - (W1**2 - W2**2)(W1**2 + W2**2)^-1 * x[n:]
            
            x[:n] -= mul( div(d1**2 - d2**2, d1**2 + d2**2), x[n:]) 
            lapack.potrs(S, x)
            
            # Solve for x[n:]:
            #
github cvxopt / cvxopt / src / python / cvxprog.py View on Github external
if kktsolver in defaultsolvers:
         if kktsolver == 'ldl': 
             factor = misc.kkt_ldl(G, dims, A, mnl, kktreg = KKTREG)
         elif kktsolver == 'ldl2': 
             factor = misc.kkt_ldl2(G, dims, A, mnl)
         elif kktsolver == 'chol':
             factor = misc.kkt_chol(G, dims, A, mnl)
         else: 
             factor = misc.kkt_chol2(G, dims, A, mnl)
         def kktsolver(x, z, W):
             f, Df, H = F(x, z)
             return factor(W, H, Df)             


    if xnewcopy is None: xnewcopy = matrix 
    if xdot is None: xdot = blas.dot
    if xaxpy is None: xaxpy = blas.axpy 
    if xscal is None: xscal = blas.scal 
    def xcopy(x, y): 
        xscal(0.0, y) 
        xaxpy(x, y)
    if ynewcopy is None: ynewcopy = matrix 
    if ydot is None: ydot = blas.dot 
    if yaxpy is None: yaxpy = blas.axpy 
    if yscal is None: yscal = blas.scal
    def ycopy(x, y): 
        yscal(0.0, y) 
        yaxpy(x, y)
             

    # Initial points
    x = xnewcopy(x0)
github cajohnst / Optimized_FX_Portfolio / Optimize_FX_Portfolio.py View on Github external
# cvxopt constraints: Gx <= h, Ax <= b
	G = opt.matrix(np.concatenate((-np.transpose(pbar),-np.identity(n)),0))
	A = opt.matrix(1.0, (1, n))
	b = opt.matrix(1.0)

	# Calculate efficient frontier weights using quadratic programming
	portfolios=[]
	for r_min in mus:
		h=opt.matrix(np.concatenate((-np.ones((1,1))*r_min,np.zeros((n,1))),0))
		sol = solvers.qp(S, -pbar, G, h, A, b)['x']
	   
		portfolios.append(sol)

	# Calculate risks and returns for the frontier
	returns = [blas.dot(pbar, x) for x in portfolios]
	risks = [np.sqrt(blas.dot(x, S*x)) for x in portfolios]

	return risks, returns