凸优化和python中

时间:2018-09-21 19:59:18

标签: python optimization scipy finance cvxopt

我使用excel来最小化变量,最近我开始使用cvxopt。我试图弄清楚在给定两个约束的情况下如何最小化一个值。我有两个return数据帧,并将权重w1w2乘以return并减去它们。我发现通过更改权重来最小化回报差异的夏普比率。这里的约束是w1 = 1sum of w2= 1

的总和

在Excel中,我使用solver添加并添加约束$S$4 = 1$s$5= 1。我试图弄清楚如何在python cvxopt中做到这一点。以下是我为cvxopt写的创建高效边界的代码。我真的很感谢您的帮助。

'import numpy as np import matplotlib.pyplot as plt import cvxopt as opt from cvxopt import blas, solvers import pandas as pd'

`

def random_portfolio(returns1, returns2):

#Returns the mean and standard deviation of returns for a random portfolio

p1 = np.asmatrix(np.nanmean(returns1, axis=1))
w1 = np.asmatrix(rand_weights(returns1.shape[0]))
mu1 = w 1* p1.T




p2 = np.asmatrix(np.nanmean(returns2, axis=1))
w2 = np.asmatrix(rand_weights(returns2.shape[0]))
mu2 = w 1* p1.T



final = mu1- mu2

mean_ret = mean(final)
voltality = std(final)
sharpe = mean_ret/voltality
n = len(returns1)

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)

portfolios = solvers.qp(-sharpe, G, h, A, b)['x'] 
returns = [blas.dot(mu, x) for x in portfolios]
risks = [np.sqrt(blas.dot(x, C*x)) for x in portfolios]

return mean_ret, voltality, sharpe

`

0 个答案:

没有答案