Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/rp_method_comparison.R
1433 views
1
library(PortfolioAnalytics)
2
3
data(edhec)
4
R <- edhec[, 1:4]
5
6
# set up simple portfolio with leverage and box constraints
7
pspec <- portfolio.spec(assets=colnames(R))
8
pspec <- add.constraint(portfolio=pspec, type="leverage", min_sum=0.99, max_sum=1.01)
9
pspec <- add.constraint(portfolio=pspec, type="box", min=0, max=1)
10
11
# generate random portfolios using the 3 methods
12
rp1 <- random_portfolios(portfolio=pspec, permutations=5000, rp_method='sample')
13
rp2 <- random_portfolios(portfolio=pspec, permutations=5000, rp_method='simplex')
14
rp3 <- random_portfolios(portfolio=pspec, permutations=5000, rp_method='grid')
15
16
# show feasible portfolios in mean-StdDev space
17
tmp1.mean <- apply(rp1, 1, function(x) mean(R %*% x))
18
tmp1.StdDev <- apply(rp1, 1, function(x) StdDev(R=R, weights=x))
19
tmp2.mean <- apply(rp2, 1, function(x) mean(R %*% x))
20
tmp2.StdDev <- apply(rp2, 1, function(x) StdDev(R=R, weights=x))
21
tmp3.mean <- apply(rp3, 1, function(x) mean(R %*% x))
22
tmp3.StdDev <- apply(rp3, 1, function(x) StdDev(R=R, weights=x))
23
24
# plot feasible portfolios
25
plot(x=tmp1.StdDev, y=tmp1.mean, col="gray", main="Random Portfolio Methods")
26
points(x=tmp2.StdDev, y=tmp2.mean, col="red", pch=2)
27
points(x=tmp3.StdDev, y=tmp3.mean, col="lightgreen", pch=5)
28
legend("bottomright", legend=c("sample", "simplex", "grid"), col=c("gray", "red", "lightgreen"),
29
pch=c(1, 2, 5), bty="n")
30
31
# sample has pretty even coverage of feasible space
32
# simplex is concentrated around the assets
33
# grid is 'pushed'/concentrated to the interior due to normalization
34
35
# demonstrate how different values of fev influence the random portfolios of
36
# the simplex method
37
# This could be a really good example with Shiny for an interactive example
38
fev <- 0:5
39
par(mfrow=c(2, 3))
40
for(i in 1:length(fev)){
41
rp <- random_portfolios(portfolio=pspec, permutations=2000, rp_method='simplex', fev=fev[i])
42
tmp.mean <- apply(rp, 1, function(x) mean(R %*% x))
43
tmp.StdDev <- apply(rp, 1, function(x) StdDev(R=R, weights=x))
44
plot(x=tmp.StdDev, y=tmp.mean, main=paste("FEV =", fev[i]),
45
ylab="mean", xlab="StdDev", col=rgb(0, 0, 100, 50, maxColorValue=255))
46
}
47
par(mfrow=c(1,1))
48
49
# charts to compare simplex and sample random portfolio generation
50
par(mfrow=c(1, 2))
51
# simplex
52
rp_simplex <- random_portfolios(portfolio=pspec, permutations=2000, rp_method='simplex', fev=0:5)
53
tmp.mean <- apply(rp_simplex, 1, function(x) mean(R %*% x))
54
tmp.StdDev <- apply(rp_simplex, 1, function(x) StdDev(R=R, weights=x))
55
plot(x=tmp.StdDev, y=tmp.mean, main="rp_method=simplex fev=0:5",
56
ylab="mean", xlab="StdDev", col=rgb(0, 0, 100, 50, maxColorValue=255))
57
#sample
58
rp_sample <- random_portfolios(portfolio=pspec, permutations=2000, rp_method='sample')
59
tmp.mean <- apply(rp_sample, 1, function(x) mean(R %*% x))
60
tmp.StdDev <- apply(rp_sample, 1, function(x) StdDev(R=R, weights=x))
61
plot(x=tmp.StdDev, y=tmp.mean, main="rp_method=sample",
62
ylab="mean", xlab="StdDev", col=rgb(0, 0, 100, 50, maxColorValue=255))
63
par(mfrow=c(1,1))
64
65