我已经尝试了 3 天来了解如何在 python 中使用 cvxopt 模块来计算有效边界。我在以下位置找到的代码片段:https : //github.com/markharley/Markowitz/blob/master/markowitz.py
可能是说明我的问题的最佳方式,我在这篇文章的底部完整地包含了它。
使用现代投资组合理论维基百科页面上的方程式:
https://en.wikipedia.org/wiki/Modern_portfolio_theory#Efficient_frontier_with_no_risk-free_asset
我理解为什么第一次使用solvers.qp 使用:
solvers.qp(S, -q*pbar, G, h, A, b)['x']
Run Code Online (Sandbox Code Playgroud)
其中 q 是风险承受能力因子。然后代码继续使用 polyfit 计算最佳夏普比率的点,我也理解,但我迷路的地方是为什么现在的代码是:
# Calculate the optimal portfolio
wt = solvers.qp(opt.matrix(x1 * S), -pbar, G, h, A, b)['x']
Run Code Online (Sandbox Code Playgroud)
其中 x1 是最佳夏普比率点的回报。我不知道为什么突然间我们可以将 S(协方差矩阵)乘以 x1 并在 -pbar 项中去掉 q。
任何人都可以对此有所了解吗?抱歉,如果我没有正确解释这一点,任何澄清请求也可能有助于我让我的问题更清楚。
谢谢!
def optimal_portfolio(returns):
n = len(returns)
returns = np.asmatrix(returns)
N = 100
qs = [10**(-5.0 * t/N + 1.0) for t in range(N)]
# Convert to cvxopt matrices
S = opt.matrix(np.cov(returns))
# np.mean computes vector of means along
# as mean of each row
pbar = opt.matrix(np.mean(returns, axis=1))
# Create constraint matrices
# -- documentation at http://cvxopt.org/userguide/coneprog.html#quadratic-programming
G = -opt.matrix(np.eye(n)) # negative nxn id
h = opt.matrix(0.0, (n, 1)) # n x 1 zero vector
A = opt.matrix(1.0, (1, n)) # 1 x n 1-vector
b = opt.matrix(1.0) # [1.0]
## Calculate efficient frontier weights using quadratic programming
## https://en.wikipedia.org/wiki/Quadratic_programming
# Minimizes (1/2) w^T * S * w - q pbar^T * w for weights w
# subject to the constrains:
# G * w <= h (component-wise) -- positive weights
# and A*w = b -- Sum of weights == 1
# where S is the covariance matrix,
# pbar are the mean returns,
# q is a measure of risk tolerance
portfolios = [solvers.qp(S, -q*pbar, G, h, A, b)['x'] for q in qs]
## Could replace above with Lagrange multiplier (or convex hull)
# Calculate risks and mean 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 quadratic for 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1441 次 |
| 最近记录: |