title: Using Covariance matrix for Portfolio Optimization in R
body: <p>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.</p>
  
  <p>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? </p>
  
  <p>I have the following reproducible code. </p>
  
  <pre><code>library(zoo)
  library(rugarch)
  library(rmgarch)
  data("EuStockMarkets")
  EuStockLevel &lt;- as.zoo(EuStockMarkets)[,c("DAX","CAC","FTSE")]
  EuStockRet &lt;- 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
  </code></pre>
  
  <p>So now I want to apply the covariance that I obtained for the portfolio optimization. </p>
  
  <pre><code>##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 &lt;- as.timeSeries(100* EuStockRet) 
  
  ##Equal weight portfolio
      ewPortfolio &lt;- feasiblePortfolio(data = All.Data,spec = ewSpec,constraints = "LongOnly")  
      print(ewPortfolio)
  
  ##Minimum risk efficient portfolio
      minriskSpec &lt;- portfolioSpec()
      targetReturn &lt;- getTargetReturn(ewPortfolio@portfolio)["mean"]
      setTargetReturn(minriskSpec) &lt;- targetReturn
  
  #Now, we optimize the portfolio for the specified target return :-
      minriskPortfolio &lt;- efficientPortfolio(data = All.Data,spec = minriskSpec,constraints = "LongOnly")
      print(minriskPortfolio)
  </code></pre>
  
  <p>So, where actually do we input the covariance matrix? And is what I have done correct? Appreciate if anyone can assist me here. </p>
  
  <p>Thanks!</p>