使用fPortfolio参与约束下的投资组合优化

时间:2016-10-31 12:44:18

标签: r optimization constraints finance portfolio

在我的一个previous questions on portfolio optimisation之后,我现在尝试解决权重问题,这些权重在一组限制条件下最小化我的投资组合的方差。这似乎在大多数时间都有效,但在某些情况下不会返回任何重量。我隔离了一些这些案例并尝试在excel上解决它们。使用完全相同的数据我得到了excel的解决方案。所以我对此感到困惑。无论如何,我已经发布了一小部分数据样本,其中我的脚本在R中返回零权重向量,但我在excel中得到了一个解决方案。

                     GBP-U         USD-U         EUR-U        JPY-U         CHF-U         USD-H         EUR-H         JPY-H        CHF-H
2016-05-13  0.0013866228 -0.0004015032  0.0023176509  0.004868271  0.0230163636 -0.0046939993  0.0065432710  0.0146679154  0.022040377
2016-05-20  0.0027094242 -0.0063831791 -0.0126106089 -0.006146676 -0.0158351590  0.0031237903  0.0045931268  0.0173505502  0.008964457
2016-05-27  0.0181190929  0.0140661262  0.0099806390 -0.005023782  0.0206840583  0.0223892121  0.0282165690  0.0048453909  0.033676661
2016-06-03 -0.0102711128  0.0081653966  0.0133194563  0.031814348  0.0092257881  0.0009701383 -0.0161380212 -0.0101505102 -0.016812484
2016-06-10 -0.0144639135  0.0160062120 -0.0152727781  0.008429249  0.0015983898 -0.0021260494 -0.0229890046 -0.0054785155 -0.027628141
2016-06-17 -0.0152295990 -0.0183599416 -0.0266861641 -0.039472434 -0.0286395252 -0.0112888146 -0.0217678087 -0.0589147494 -0.026415992
2016-06-24  0.0200253356  0.0317549413  0.0346147227  0.029164922  0.0401491058 -0.0166789670  0.0006268938 -0.0379679346  0.004386684
2016-07-01  0.0696777854  0.0620232077  0.0754752233  0.065465564  0.0704656764  0.0314514909  0.0433540129  0.0379248977  0.041482495
2016-07-08  0.0016753950  0.0369097752  0.0076117981  0.007834512  0.0070165833  0.0130392554 -0.0084350439 -0.0354380321 -0.006380199
2016-07-15  0.0118593292 -0.0037643046  0.0058939005  0.028278185 -0.0020681743  0.0144418213  0.0257127861  0.0888542585  0.015778875
2016-07-22  0.0092918745  0.0130552307  0.0086318585  0.001398931  0.0062780458  0.0067460116  0.0077586182  0.0070486894  0.004685162
2016-07-29 -0.0004646661 -0.0094042916  0.0124831676  0.025141288  0.0018086154 -0.0002180359  0.0040494413 -0.0046691781 -0.006745367
2016-08-05  0.0101311910  0.0160368778  0.0045395343 -0.014712256  0.0081973627  0.0040861320  0.0006639525 -0.0289135714  0.007984259
2016-08-12  0.0177313054  0.0125451367  0.0377475690  0.050109303  0.0322171465  0.0007342288  0.0192744848  0.0332886266  0.014216897
2016-08-19 -0.0086591802 -0.0121869847 -0.0131592756 -0.018946998 -0.0182591489 -0.0003056645 -0.0156054036 -0.0176744155 -0.020491089
2016-08-26 -0.0032629188 -0.0112907806 -0.0099742141 -0.028672792 -0.0178773544 -0.0066076557  0.0061594585 -0.0078421386  0.005324054
2016-09-02  0.0082322936 -0.0063800658 -0.0012434593  0.010783580  0.0006709931  0.0054516933  0.0145201246  0.0429479722  0.015518353
2016-09-09 -0.0172703615 -0.0222186855 -0.0031692051  0.014054238  0.0031503483 -0.0243009678 -0.0119551411  0.0001827415 -0.004089906
2016-09-16 -0.0105444978  0.0257113060 -0.0072348498 -0.002332981 -0.0002596633  0.0054847746 -0.0203191336 -0.0263452760 -0.015320125
2016-09-23  0.0294827487  0.0150126367  0.0358026791  0.041563858  0.0298489405  0.0121889694  0.0268098637  0.0263689159  0.017033142
2016-09-30 -0.0010620124  0.0010033756 -0.0058345738 -0.025319672 -0.0159143301  0.0014163678 -0.0060494835 -0.0215285310 -0.014212323
2016-10-07  0.0213392520  0.0351900327  0.0426486765  0.047394294  0.0333505051 -0.0072195144  0.0034453853  0.0210516899 -0.002539864
2016-10-14 -0.0050982217  0.0095672058  0.0009866843  0.004577971  0.0035956430 -0.0102205657  0.0020303997 -0.0035136735 -0.002926098
2016-10-21  0.0002873986  0.0004041448 -0.0029951394  0.012861688 -0.0135120432  0.0038727554  0.0087025204  0.0127856995 -0.006358163
2016-10-28 -0.0039917961 -0.0029823347  0.0078312950  0.013806532 -0.0053670019 -0.0070482290 -0.0052953175  0.0188646173 -0.014826428

我的脚本如下:

require(fPortfolio)
spec <- portfolioSpec()
mydata <- as.timeSeries(mydata)

cons <- c("eqsumW=1","eqsumW['GBP-U']=0.6" ,"eqsumW[c('USD-U','USD-H')]=0.20","eqsumW[c('EUR-U','EUR-H')]=0.10","eqsumW[c('JPY-U','JPY-H')]=0.05","eqsumW[c('CHF-U','CHF-H')]=0.05","LongOnly")

minvariancePortfolio(list(mu=apply(mydata,2,function(x) mean(x)),sigma=cov(mydata)), spec = spec, constraints = cons)

在上述数据上运行此操作会得到以下结果:

Title:
 MV Minimum Variance Portfolio 
 Estimator:         covEstimator 
 Solver:            solveRquadprog 
 Optimize:          minRisk 
 Constraints:       LongOnly 

Portfolio Weights:
GBP-U USD-U EUR-U JPY-U CHF-U USD-H EUR-H JPY-H CHF-H 
    0     0     0     0     0     0     0     0     0 

Covariance Risk Budgets:
GBP-U USD-U EUR-U JPY-U CHF-U USD-H EUR-H JPY-H CHF-H 


Target Return and Risk:
[1] 0

Description:
 Mon Oct 31 11:26:35 2016 by user: 43951663 

使用excel解算器运行相同约束和数据的优化时,我得到以下解决方案:

    Constraints                         
GBP 0.6000                          
USD 0.2000                          
EUR 0.1000                          
JPY 0.0500                          
CHF 0.0500                          

Portfolio   1.0000                          


Weights                             
GBP-U   USD-U   EUR-U   JPY-U   CHF-U   USD-H   EUR-H   JPY-H   CHF-H
0.6000  0.0000  0.0000  0.0423  0.0000  0.2000  0.1000  0.0077  0.0500

Portfolio Stdev 0.01562                         
Portfolio Mean  0.00424                         

我知道哪里弄错了?我假设它可能与spec&lt; - portfolioSpec()中的求解器的一些初始设置相关联,但我的想法已经用完......

1 个答案:

答案 0 :(得分:0)

好的我觉得我已经部分解决了这个问题。似乎国内资产的约束率是60%。当我把它取下时,我得到的结果与在excel中运行优化时的结果完全相同。我认为在oprtimiser中有一些向上/向下触发这个...如果有人作为更好的答案,请让论坛知道,因为这可能是其他人感兴趣的。以下是有和没有60%约束的截图。

cons <- c("eqsumW[c('USDU','USDH')]=0.20","eqsumW[c('EURU','EURH')]=0.10","eqsumW[c('JPYU','JPYH')]=0.05","eqsumW[c('CHFU','CHFH')]=0.05")
> minvariancePortfolio(list(mu=apply(mydata,2,function(x) mean(x)),sigma=cov(mydata)), spec = portfolioSpec() , constraints = cons)

Title:
 MV Minimum Variance Portfolio 
 Estimator:         covEstimator 
 Solver:            solveRquadprog 
 Optimize:          minRisk 
 Constraints:       

Portfolio Weights:
  GBPU   USDU   EURU   JPYU   CHFU   USDH   EURH   JPYH   CHFH 
0.6000 0.0000 0.0000 0.0423 0.0000 0.2000 0.1000 0.0077 0.0500 

Covariance Risk Budgets:
  GBPU   USDU   EURU   JPYU   CHFU   USDH   EURH   JPYH   CHFH 
0.6927 0.0000 0.0000 0.0454 0.0000 0.1098 0.0970 0.0082 0.0470 

Target Return and Risk:
[1] 0.0042

Description:
 Tue Nov 01 16:34:52 2016 by user: 43951663 
> 
> cons <- c("eqsumW['GBPU']=0.6" ,"eqsumW[c('USDU','USDH')]=0.20","eqsumW[c('EURU','EURH')]=0.10","eqsumW[c('JPYU','JPYH')]=0.05","eqsumW[c('CHFU','CHFH')]=0.05")
> minvariancePortfolio(list(mu=apply(mydata,2,function(x) mean(x)),sigma=cov(mydata)), spec = portfolioSpec() , constraints = cons)

Title:
 MV Minimum Variance Portfolio 
 Estimator:         covEstimator 
 Solver:            solveRquadprog 
 Optimize:          minRisk 
 Constraints:       

Portfolio Weights:
GBPU USDU EURU JPYU CHFU USDH EURH JPYH CHFH 
   0    0    0    0    0    0    0    0    0 

Covariance Risk Budgets:
GBPU USDU EURU JPYU CHFU USDH EURH JPYH CHFH 


Target Return and Risk:
[1] 0

Description:
 Tue Nov 01 16:35:57 2016 by user: 43951663 
> 
>