Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/demo/chart_concentration.R
1433 views
1
#' ---
2
#' title: "chart.Concentration Demo"
3
#' author: "Ross Bennett"
4
#' date: "7/17/2014"
5
#' ---
6
7
#' This script demonstrates how to use chart.Concentration to visualize
8
#' the concentration of the portfolio.
9
10
11
library(PortfolioAnalytics)
12
13
data(edhec)
14
R <- edhec[, 1:8]
15
funds <- colnames(R)
16
17
#' Construct initial portfolio
18
init.portf <- portfolio.spec(assets=funds)
19
init.portf <- add.constraint(portfolio=init.portf,
20
type="leverage",
21
min_sum=0.99,
22
max_sum=1.01)
23
24
init.portf <- add.constraint(portfolio=init.portf,
25
type="box",
26
min=0,
27
max=1)
28
29
init.portf <- add.objective(portfolio=init.portf,
30
type="return",
31
name="mean",
32
multiplier=0)
33
34
init.portf <- add.objective(portfolio=init.portf,
35
type="risk",
36
name="ES")
37
38
#' Construct a risk budget portfolio.
39
rb.portf <- add.objective(portfolio=init.portf,
40
type="risk_budget",
41
name="ES",
42
max_prisk=0.4,
43
arguments=list(p=0.92))
44
45
#' Use random portfolios for optimization.
46
opt <- optimize.portfolio(R=R,
47
portfolio=init.portf,
48
optimize_method="random",
49
search_size=2000,
50
trace=TRUE)
51
52
opt_rb <- optimize.portfolio(R=R,
53
portfolio=rb.portf,
54
optimize_method="random",
55
search_size=2000,
56
trace=TRUE)
57
58
#' This won't work because opt is not a risk budget optimization.
59
#' This should result in an error and not plot anything.
60
#chart.Concentration(opt, conc.type="pct_contrib")
61
62
#' `opt` is minimum ES optimization so we can still chart it using weights as
63
#' the measure of concentration.
64
chart.Concentration(opt, conc.type="weights", chart.assets=TRUE, col=heat.colors(10))
65
chart.Concentration(opt, conc.type="weights", chart.assets=TRUE, col=bluemono)
66
67
#' Here we plot the concentration based on the HHI of the percentage component
68
#' contribution to risk.
69
chart.Concentration(opt_rb, conc.type="pct_contrib")
70
71
#' Here we plot the concentration is based on the HHI of the weights.
72
chart.Concentration(opt_rb, conc.type="weights")
73
74
75