Example Qbit to optimize the portfolio weights of hedge fund returns (EDHEC) based on Expecated Shortfall (ES) and the Symphony optimizer.
library(PortfolioAnalytics)
library(ROI)
library(ROI.plugin.symphony)
library(ggplot2)
optimize_porfolio <- function(R, long_only = FALSE) {
#' Set up initial portfolio with basic constraints.
init.portf <- portfolio.spec(assets=funds)
init.portf <- add.constraint(portfolio=init.portf, type="full_investment")
if (long_only) {
init.portf <- add.constraint(portfolio=init.portf, type="long_only")
}
#' Add objectives.
mines.portf <- add.objective(portfolio=init.portf, type="risk", name="ES")
opt.mines.symphony <- optimize.portfolio(R, mines.portf, optimize_method="symphony")
opt.mines.symphony
weights_df <- data.frame(
assets = colnames(R),
weights = opt.mines.symphony$weights)
p <- ggplot(weights_df) +
geom_col(aes(assets, weights)) +
coord_flip()
plot(p)
weights_df
}
data(edhec)
R <- edhec
funds <- colnames(R)
optimize_porfolio(R)
optimize_porfolio(R, TRUE)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.