使用协方差矩阵进行R中的投资组合优化

我对R中的投资组合优化有疑问.我对R很新,并试图研究和寻找答案,但我不确定它是否正确.我希望有人可以在这里帮助我.

我使用计量经济模型从资产建模中获得了协方差矩阵(在这里,我使用DCC GARCH来模拟我的资产回报).在进行预测之后,我将获得协方差矩阵.那么,现在,如何使用此协方差矩阵使用fPortfolio包进行投资组合优化?我发现的大多数示例仅使用资产回报来进行投资组合优化.但是,如果我们使用资产收益的预测均值和方差 – 协方差来创建最优资产配置模型呢?

我有以下可重现的代码.

library(zoo)
library(rugarch)
library(rmgarch)
data("EuStockMarkets")
EuStockLevel <- as.zoo(EuStockMarkets)[,c("DAX","CAC","FTSE")]
EuStockRet <- diff(log(EuStockLevel))

## GARCH-DCC
    uspec = ugarchspec(mean.model = list(armaOrder = c(0,0)), variance.model = list(garchOrder = c(1,1), model = "sGARCH"), distribution.model = "norm")
    spec1 = dccspec(uspec = multispec( replicate(3, uspec) ),  dccOrder = c(1,1),  distribution = "mvnorm")
    fit1 = dccfit(spec1, data = EuStockRet, fit.control = list(eval.se=T))

#Forecasting 
    dcc.focast=dccforecast(fit1, n.ahead = 1, n.roll = 0)
    print(dcc.focast)


    covmat.focast = rcov(dcc.focast)
    covmat = covmat.focast$`1975-02-03`[,,1]  ##The Covariance matrix

          DAX          CAC         FTSE
DAX  0.0002332114 0.0001624446 0.0001321865
CAC  0.0001624446 0.0001799988 0.0001139339
FTSE 0.0001321865 0.0001139339 0.0001372812

所以现在我想应用我为投资组合优化获得的协方差.

##Optimization (Use the forecasted variance covariance matrix!!!)
##You must convert your dataset into "timeSeries" object for R to be able to read it in fportfolio. 

library(fPortfolio)
##To compute efficient portfolio
    All.Data <- as.timeSeries(100* EuStockRet) 

##Equal weight portfolio
    ewPortfolio <- feasiblePortfolio(data = All.Data,spec = ewSpec,constraints = "LongOnly")  
    print(ewPortfolio)

##Minimum risk efficient portfolio
    minriskSpec <- portfolioSpec()
    targetReturn <- getTargetReturn(ewPortfolio@portfolio)["mean"]
    setTargetReturn(minriskSpec) <- targetReturn

#Now, we optimize the portfolio for the specified target return :-
    minriskPortfolio <- efficientPortfolio(data = All.Data,spec = minriskSpec,constraints = "LongOnly")
    print(minriskPortfolio)

那么,我们实际上在哪里输入协方差矩阵?我做的正确吗?感谢是否有人可以在这里帮助我.

谢谢!

最佳答案 您可以将您的EuroStockRet对象作为时间序列传递给fPortfolio函数fPortfolio :: covEstimator(参见?covEstimator),它接受一个timeseries对象并返回一个对象,而不是使用包zoo,rugarch,rmgarch中的函数来分别创建协方差矩阵.在possiblePortfolio期望的data参数格式中.就像是:

EuStockRet_with_cov <- covEstimator(x=EuStockRet);
ewPortfolio <- feasiblePortfolio(data = EuStockRet_with_cov, spec = ewSpec, constraints = "LongOnly");

还有其他各种方法,fPortfiolio可以计算协方差.它们详见第37页:fPortfolio Package

点赞