为什么解决.QP返回不可行的解决方案

时间:2014-09-20 09:28:25

标签: r optimization

我想在R中尝试一个均值方差优化的例子。首先,我使用了这个简单的例子:

> data(edhec)
> col.mean <- colMeans(edhec)
> Dmat <- cov(edhec)
> Amat <- matrix(1,nrow=nrow(Dmat))
> Amat <- cbind(Amat,col.mean)
> Amat <- cbind(Amat,diag(nrow(Dmat)))
> bvec <- c(1,0.005,rep(0,nrow(Dmat)))
> sol <- solve.QP(Dmat = 2*Dmat,dvec = 0.5*col.mean,Amat = Amat,bvec = bvec,meq = 1)
> sol$solution
 [1]  8.882101e-17  0.000000e+00  6.661437e-01  1.990021e-17  1.900749e-15 -3.685111e-17  2.419712e-16  3.338563e-01
 [9] -3.903081e-16  7.237917e-18  1.093498e-15  0.000000e+00  1.243608e-15
> 

然而,似乎这种解决方案不可行。有两个权重是负面矛盾的一个约束:

> Amat
                            col.mean                          
Convertible Arbitrage  1 0.006408553 1 0 0 0 0 0 0 0 0 0 0 0 0
CTA Global             1 0.006489474 0 1 0 0 0 0 0 0 0 0 0 0 0
Distressed Securities  1 0.007953289 0 0 1 0 0 0 0 0 0 0 0 0 0
Emerging Markets       1 0.008246053 0 0 0 1 0 0 0 0 0 0 0 0 0
Equity Market Neutral  1 0.006002632 0 0 0 0 1 0 0 0 0 0 0 0 0
Event Driven           1 0.007622368 0 0 0 0 0 1 0 0 0 0 0 0 0
Fixed Income Arbitrage 1 0.004230921 0 0 0 0 0 0 1 0 0 0 0 0 0
Global Macro           1 0.007672368 0 0 0 0 0 0 0 1 0 0 0 0 0
Long/Short Equity      1 0.007759868 0 0 0 0 0 0 0 0 1 0 0 0 0
Merger Arbitrage       1 0.006784868 0 0 0 0 0 0 0 0 0 1 0 0 0
Relative Value         1 0.006701316 0 0 0 0 0 0 0 0 0 0 1 0 0
Short Selling          1 0.004161184 0 0 0 0 0 0 0 0 0 0 0 1 0
Funds of Funds         1 0.005918421 0 0 0 0 0 0 0 0 0 0 0 0 1

第一列对应加权和的约束应为1.第二列是目标返回约束。其余列正好是一个,不包括负权重。关于solve.QP的帮助,约束被称为A ^ T b> = b_0。转置后的Amat看起来像

> t(Amat)
         Convertible Arbitrage  CTA Global Distressed Securities Emerging Markets Equity Market Neutral Event Driven
                   1.000000000 1.000000000           1.000000000      1.000000000           1.000000000  1.000000000
col.mean           0.006408553 0.006489474           0.007953289      0.008246053           0.006002632  0.007622368
                   1.000000000 0.000000000           0.000000000      0.000000000           0.000000000  0.000000000
                   0.000000000 1.000000000           0.000000000      0.000000000           0.000000000  0.000000000
                   0.000000000 0.000000000           1.000000000      0.000000000           0.000000000  0.000000000
                   0.000000000 0.000000000           0.000000000      1.000000000           0.000000000  0.000000000
                   0.000000000 0.000000000           0.000000000      0.000000000           1.000000000  0.000000000
                   0.000000000 0.000000000           0.000000000      0.000000000           0.000000000  1.000000000
                   0.000000000 0.000000000           0.000000000      0.000000000           0.000000000  0.000000000
                   0.000000000 0.000000000           0.000000000      0.000000000           0.000000000  0.000000000
                   0.000000000 0.000000000           0.000000000      0.000000000           0.000000000  0.000000000
                   0.000000000 0.000000000           0.000000000      0.000000000           0.000000000  0.000000000
                   0.000000000 0.000000000           0.000000000      0.000000000           0.000000000  0.000000000
                   0.000000000 0.000000000           0.000000000      0.000000000           0.000000000  0.000000000
                   0.000000000 0.000000000           0.000000000      0.000000000           0.000000000  0.000000000
         Fixed Income Arbitrage Global Macro Long/Short Equity Merger Arbitrage Relative Value Short Selling
                    1.000000000  1.000000000       1.000000000      1.000000000    1.000000000   1.000000000
col.mean            0.004230921  0.007672368       0.007759868      0.006784868    0.006701316   0.004161184
                    0.000000000  0.000000000       0.000000000      0.000000000    0.000000000   0.000000000
                    0.000000000  0.000000000       0.000000000      0.000000000    0.000000000   0.000000000
                    0.000000000  0.000000000       0.000000000      0.000000000    0.000000000   0.000000000
                    0.000000000  0.000000000       0.000000000      0.000000000    0.000000000   0.000000000
                    0.000000000  0.000000000       0.000000000      0.000000000    0.000000000   0.000000000
                    0.000000000  0.000000000       0.000000000      0.000000000    0.000000000   0.000000000
                    1.000000000  0.000000000       0.000000000      0.000000000    0.000000000   0.000000000
                    0.000000000  1.000000000       0.000000000      0.000000000    0.000000000   0.000000000
                    0.000000000  0.000000000       1.000000000      0.000000000    0.000000000   0.000000000
                    0.000000000  0.000000000       0.000000000      1.000000000    0.000000000   0.000000000
                    0.000000000  0.000000000       0.000000000      0.000000000    1.000000000   0.000000000
                    0.000000000  0.000000000       0.000000000      0.000000000    0.000000000   1.000000000
                    0.000000000  0.000000000       0.000000000      0.000000000    0.000000000   0.000000000
         Funds of Funds
            1.000000000
col.mean    0.005918421
            0.000000000
            0.000000000
            0.000000000
            0.000000000
            0.000000000
            0.000000000
            0.000000000
            0.000000000
            0.000000000
            0.000000000
            0.000000000
            0.000000000
            1.000000000

并且向量b0似乎也是正确的:

> bvec
 [1] 1.000 0.005 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
> 

问题是,为什么solve.QP会返回一个不可行的解决方案?

1 个答案:

答案 0 :(得分:1)

+ + - 3e-16的值在数值上为零,在浮点表示的范围内。您的解决方案中基本上有两个非零权重,两者都是正数。

您可能对此感兴趣:
What Every Computer Scientist Should Know About Floating-Point Arithmetic