Reputation: 185
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!
Upvotes: 5
Views: 3399
Reputation: 21
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
Upvotes: 2
Reputation: 1727
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
Upvotes: 2