Path: blob/master/demo/chart_concentration.R
1433 views
#' ---1#' title: "chart.Concentration Demo"2#' author: "Ross Bennett"3#' date: "7/17/2014"4#' ---56#' This script demonstrates how to use chart.Concentration to visualize7#' the concentration of the portfolio.8910library(PortfolioAnalytics)1112data(edhec)13R <- edhec[, 1:8]14funds <- colnames(R)1516#' Construct initial portfolio17init.portf <- portfolio.spec(assets=funds)18init.portf <- add.constraint(portfolio=init.portf,19type="leverage",20min_sum=0.99,21max_sum=1.01)2223init.portf <- add.constraint(portfolio=init.portf,24type="box",25min=0,26max=1)2728init.portf <- add.objective(portfolio=init.portf,29type="return",30name="mean",31multiplier=0)3233init.portf <- add.objective(portfolio=init.portf,34type="risk",35name="ES")3637#' Construct a risk budget portfolio.38rb.portf <- add.objective(portfolio=init.portf,39type="risk_budget",40name="ES",41max_prisk=0.4,42arguments=list(p=0.92))4344#' Use random portfolios for optimization.45opt <- optimize.portfolio(R=R,46portfolio=init.portf,47optimize_method="random",48search_size=2000,49trace=TRUE)5051opt_rb <- optimize.portfolio(R=R,52portfolio=rb.portf,53optimize_method="random",54search_size=2000,55trace=TRUE)5657#' This won't work because opt is not a risk budget optimization.58#' This should result in an error and not plot anything.59#chart.Concentration(opt, conc.type="pct_contrib")6061#' `opt` is minimum ES optimization so we can still chart it using weights as62#' the measure of concentration.63chart.Concentration(opt, conc.type="weights", chart.assets=TRUE, col=heat.colors(10))64chart.Concentration(opt, conc.type="weights", chart.assets=TRUE, col=bluemono)6566#' Here we plot the concentration based on the HHI of the percentage component67#' contribution to risk.68chart.Concentration(opt_rb, conc.type="pct_contrib")6970#' Here we plot the concentration is based on the HHI of the weights.71chart.Concentration(opt_rb, conc.type="weights")72737475