CVXOPT求解全局最小方差投资组合的异常问题求助
全局最小方差投资组合CVXOPT求解异常问题
我正在求解全局最小方差投资组合问题(仅在权重和为1的约束下最小化投资组合方差),该问题存在已知解析解。但使用Python的CVXOPT库复现结果时遇到异常:
- 使用10x10完整样本协方差矩阵,或8x8及以上的子矩阵求解时,输出结果不正确:权重和不满足等于1的约束,且权重与解析解不符
- 使用7x7及以下子矩阵求解时,结果完全正确:约束满足,权重与解析解一致
无法确定问题根源,恳请帮助。示例代码如下:
%reset -sf #Clear Environment import numpy as np import cvxopt from cvxopt import matrix as dmatrix from cvxopt.solvers import qp, options from numpy.linalg import inv from numpy import ones from numpy import transpose as t # Sample 10x10 covariance matrix sigmafull = np.array([[0.01449082, 0.00846992, 0.00846171, 0.00773097, 0.00878925, 0.00748843, 0.00672341, 0.00665912, 0.0068593 , 0.00827341], [0.00846992, 0.00952205, 0.00766057, 0.00726647, 0.00781524, 0.00672368, 0.00642426, 0.00609368, 0.00617965, 0.00704281], [0.00846171, 0.00766057, 0.00842194, 0.00700168, 0.00772423, 0.0061137 , 0.00612574, 0.00601041, 0.00621007, 0.00712152], [0.00773097, 0.00726647, 0.00700168, 0.00687784, 0.00726901, 0.00573606, 0.00567145, 0.00556391, 0.00575279, 0.00660916], [0.00878925, 0.00781524, 0.00772423, 0.00726901, 0.00860462, 0.00612804, 0.0061301 , 0.00603605, 0.00630947, 0.0075281 ], [0.00748843, 0.00672368, 0.0061137 , 0.00573606, 0.00612804, 0.00634431, 0.0054793 , 0.00513665, 0.00511852, 0.00575049], [0.00672341, 0.00642426, 0.00612574, 0.00567145, 0.0061301 , 0.0054793 , 0.0055722 , 0.0050824 , 0.00512499, 0.00576934], [0.00665912, 0.00609368, 0.00601041, 0.00556391, 0.00603605, 0.00513665, 0.0050824 , 0.00521583, 0.00510142, 0.00576414], [0.0068593 , 0.00617965, 0.00621007, 0.00575279, 0.00630947, 0.00511852, 0.00512499, 0.00510142, 0.00547566, 0.00603528], [0.00827341, 0.00704281, 0.00712152, 0.00660916, 0.0075281 , 0.00575049, 0.00576934, 0.00576414, 0.00603528, 0.00756009]]) # sigma = sigmafull[0:8,0:8] #With this subsample, output is incorrect. n=8 (and for all n>8) sigma = sigmafull[0:7,0:7] #With this subsample, output is correct. n=7 (and for all n<7) n=len(sigma) sigma = dmatrix(sigma) #Formatting sigma to be a dense matrix for cvxopt mu = dmatrix(np.zeros(n)) #We just want to minimize variance, hence vector of zeroes #Format of the equality constraint : Ax = b #We want the sum of x to be equal to 1 Amatrix = dmatrix(ones(n)).T #Vector of ones bmatrix = dmatrix(1.0) #Scalar = 1 sol = qp(sigma, mu, None, None, A=Amatrix, b=bmatrix) #No inequality constraint w_gmv = (inv(sigma)@ones(n))/(t(ones(n))@inv(sigma)@ones(n)) #Analytical solution which indeed does sum to 1 print(t(np.array(sol['x'])) - w_gmv) #If Vector of zeroes -> Weights are equivalent -> OK print(sum(np.array(sol['x']))) #If equal to 1 -> OK
内容的提问来源于stack exchange,提问作者rvanderveken
相关产品推荐
相关产品推荐

