5

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!

NSAA
  • 175
  • 1
  • 3
  • 14

2 Answers2

2

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

Mekki MacAulay
  • 1,727
  • 2
  • 12
  • 23
  • 1
    Thanks! I have managed to input the covariance matrix to the optmization code by creating a function. i.e covtEstimator <- function (x, spec = NULL, ...) {x.mat = as.matrix(x) list(mu = er, Sigma = covmat) } . And, by the way, how do we forecast the mean value from the fitted model? – NSAA Jan 07 '16 at 16:37
  • I used the function fitted(dcc.focast). Is it right? – NSAA Jan 07 '16 at 16:40
  • I'm not sure, but look at page 19 of the `fPortfolio` package PDF. It talks about `targetMean` as a variable in the `@portfolio` slot of the S4 object returned. So you may be able to just access it as `ewPortfolio@portfolio$targetMean`. However, you should verify that that's the mean you're looking for. – Mekki MacAulay Jan 07 '16 at 17:37
  • 1
    Also worth noting that the makers of the `fPortfolio` package and many other related packages have a bunch of free ebooks that explain in detail with many examples how to work through this sort of thing. You can find them here: https://www.rmetrics.org/ebooks-free – Mekki MacAulay Jan 07 '16 at 17:50
  • That solution does not answer the using of DCC covariance. While DCC consider the dynamic correlations to forecast covariance, fPortfolio uses the historical ones. Theoretically DCC has many advantages. http://stats.stackexchange.com/a/173225/77852 – Robert May 05 '16 at 20:07
2

You can be achieve it for fPortfolio package using SetEstimator. Example below:

covtEstimator <- function (x,data,spec) {
x.mat = as.matrix(x)
list(mu=meanreturnfromyourforecast,Sigma=covmat)
}

# Calculate Efficient Frontier
defaultSpec <- portfolioSpec()
setEstimator(defaultSpec) <- 'covtEstimator'
efficientPortfolio(yourreturndata, defaultSpec, constraints = "LongOnly")

Additional reference : Page 293 here

Ajay Verma
  • 21
  • 1