Path: blob/master/demo/higher_moments_boudt.R
1433 views
#' ---1#' title: "Higher Moment Estimates Demo"2#' author: Ross Bennett3#' date: "7/17/2014"4#' ---56#' This script demonstrates how to solve a constrained portfolio optimization7#' problem using moments estimated via a factor model based on the work of8#' Kris Boudt.910#' Load package and data.11library(PortfolioAnalytics)12data(edhec)13R <- edhec[, 1:10]14funds <- colnames(R)1516#' Construct initial portfolio with basic constraints.17init.portf <- portfolio.spec(assets=funds)18init.portf <- add.constraint(portfolio=init.portf, type="weight_sum",19min_sum=0.99, max_sum=1.01)20init.portf <- add.constraint(portfolio=init.portf, type="long_only")21init.portf <- add.objective(portfolio=init.portf, type="risk", name="ES",22arguments=list(p=0.9, clean="boudt"))2324#' This is not necessary for the optimization, but demonstrates how the25#' moments are estimated using portfolio.moments.boudt.26cleanR <- Return.clean(R, "boudt")27fit <- statistical.factor.model(cleanR, 3)2829#' Here we extract the moments.30sigma <- extractCovariance(fit)31m3 <- extractCoskewness(fit)32m4 <- extractCokurtosis(fit)3334# Here we compute the moments with portfolio.moments.boudt and show that the35#' result is equivalent to fitting the factor model and extracting the moment36#' estimates.37moments.boudt <- portfolio.moments.boudt(R, init.portf, k=3)38all.equal(moments.boudt$sigma, sigma)39all.equal(moments.boudt$m3, m3)40all.equal(moments.boudt$m4, m4)4142#' Generate set of random portfolios to use for the optimization.43rp <- random_portfolios(init.portf, 5000)4445#' Here we run the optimization using sample estimates for the moments.46#' The default for momentFUN is set.portfolio.moments which computes47#' the sample estimates of the moments.48minES.lo.sample <- optimize.portfolio(R=R, portfolio=init.portf,49rp=rp, optimize_method="random",50trace=TRUE)5152#' Now we run the optimization with statistical factor model estimates of the53#' moments.54minES.lo.boudt <- optimize.portfolio(R=R, portfolio=init.portf,55momentFUN=portfolio.moments.boudt,56k=3, rp=rp,57optimize_method="random",58trace=TRUE)5960616263