Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Covariance matrix for Portfolio Optimization in R

I have a question regarding the Portfolio Optimization in R. I am very new to R and have tried to study and look answers but I'm not sure whether it is correct. I hope someone can assist me here.

I have obtained covariance matrix from the asset modelling using econometric model (In here, I use DCC GARCH to model my asset returns). After I do the forecasting, I will get the covariance matrix. So, now, How do I use this covariance matrix for Portfolio Optimization using fPortfolio package? Most of the examples that I found uses only the asset returns to do portfolio optimization. But how about if we use the forecasted mean and variance-covariance of the asset returns in order to create optimal asset allocation models?

I have the following reproducible code.

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

So now I want to apply the covariance that I obtained for the portfolio optimization.

##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)

So, where actually do we input the covariance matrix? And is what I have done correct? Appreciate if anyone can assist me here.

Thanks!

like image 808
NSAA Avatar asked Sep 26 '22 18:09

NSAA


1 Answers

Instead of using the functions in packages zoo, rugarch, rmgarch to create the covariance matrix separately, you could pass your EuroStockRet object as a timeseries to the fPortfolio function fPortfolio::covEstimator (see ?covEstimator) which takes a timeseries object and returns an object in the data argument's format expected by feasiblePortfolio. Something like:

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

There are also various otherways that fPortfiolio can calculate covariances. They are detailed on page 37: fPortfolio Package

like image 62
Mekki MacAulay Avatar answered Oct 13 '22 13:10

Mekki MacAulay