Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/leverage_transformation_testing.R
1433 views
1
2
library(PortfolioAnalytics)
3
4
# Use random_portfolios to generate weights that do not meet the full
5
# investment constraint where the sum of the weights range from 0.8 to 1.2
6
7
# Note: slow using random portfolios
8
9
sum_seq <- seq(from=0.8, to=1.5, by=0.1)
10
11
##### Random Portfolios: 50 assets 5,000 portfolios
12
nassets <- 50
13
npermutations <- 500
14
min <- rep(0, nassets)
15
# random_index <- sample(1:nassets, 5)
16
# min[random_index] <- 0.01
17
max <- rep(0.5, nassets)
18
rp <- list()
19
for(i in 1:10){
20
min_sum <- sample(sum_seq, 1)
21
max_sum <- min_sum + 0.01
22
23
cset <- constraint(assets=nassets, min=min, max=max,
24
min_sum=min_sum, max_sum=max_sum,
25
weight_seq=generatesequence(min=0, max=0.5, by=0.005))
26
27
rp[[i]] <- random_portfolios(rpconstraints=cset, permutations=npermutations)
28
}
29
30
rp <- do.call(rbind, rp)
31
32
# transform the entire vector to meet leverage constraints
33
tmp_rp <- t(apply(rp, 1, txfrm_weight_sum_constraint, min_sum=0.99, max_sum=1.01))
34
35
# percentage of portfolios that satisfy box constraints after the simple transformation
36
sum(apply(tmp_rp, 1, function(x) all(x >= min & x <= max))) / (nrow(tmp_rp)) * 100
37
38
# only works if I relax min and
39
new_rp <- t(apply(tmp_rp, 1, rp_transform, min=rep(-0.05, nassets), max=rep(0.5, nassets),
40
groups=NULL, cLO=NULL, cUP=NULL,
41
max_permutations=500))
42
43
##### Random Portfolios: 250 assets 5,000 portfolios
44
nassets <- 250
45
npermutations <- 500
46
min <- rep(0, nassets)
47
random_index <- sample(1:nassets, 10)
48
min[random_index] <- 0.01
49
max <- rep(0.5, nassets)
50
rp <- list()
51
for(i in 1:10){
52
min_sum <- sample(sum_seq, 1)
53
max_sum <- min_sum + 0.01
54
55
cset <- constraint(assets=nassets, min=min, max=max,
56
min_sum=min_sum, max_sum=max_sum,
57
weight_seq=generatesequence(min=0, max=0.5, by=0.005))
58
59
rp[[i]] <- random_portfolios(rpconstraints=cset, permutations=npermutations)
60
}
61
62
rp <- do.call(rbind, rp)
63
64
# transform the entire vector to meet leverage constraints
65
tmp_rp <- t(apply(rp, 1, txfrm_weight_sum_constraint, min_sum=0.99, max_sum=1.01))
66
67
# percentage of portfolios that satisfy box constraints after the simple transformation
68
sum(apply(tmp_rp, 1, function(x) all(x >= min & x <= max))) / (nrow(tmp_rp)) * 100
69
70
new_rp <- t(apply(tmp_rp, 1, rp_transform, min=min, max=max, groups=NULL, cLO=NULL, cUP=NULL))
71
72
73
# generate portfolios of uniform random numbers that satisfy box constraints,
74
# but will violate leverage constraints
75
N <- 500
76
k <- 10000
77
min <- -0.01
78
max <- 0.15
79
80
set.seed(123)
81
tmp <- runif(N*k, min, max)
82
tmp_mat <- matrix(tmp, nrow=k)
83
84
summary(rowSums(tmp_mat))
85
86
# transform the entire vector to meet leverage constraints
87
tmp_rp <- t(apply(tmp_mat, 1, txfrm_weight_sum_constraint, min_sum=0.99, max_sum=1.01))
88
89
min <- c(rep(-0.01, 200), 0.01, rep(-0.01, 299))
90
max <- rep(0.15, 500)
91
# percentage of portfolios that satisfy box constraints after the simple transformation
92
sum(apply(tmp_rp, 1, function(x) all(x >= min & x <= max))) / (nrow(tmp_rp)) * 100
93
94
# All portfolios seem to satisfy box constraints if min is a vector of all 0s or
95
# all elements are less than 0 and the sum of the weights is greater than 1
96
97
# If elements of the min vector are positive, then 0 portfolios satisfy constraints
98
99
# very sensitive to box constraint parameters and sum of the weights
100
101
102