Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/test2_rp_transform2.R
1433 views
1
2
min_sum <- 0.99
3
max_sum <- 1.01
4
min_box <- rep(-0.15, length(weights))
5
max_box <- rep(0.6, length(weights))
6
7
# violate min_sum and box constraint
8
weights <- c(0.2, -0.2, 0.4, 0.5)
9
sum(weights)
10
11
rp_transform(w=weights,
12
min_sum=min_sum,
13
max_sum=max_sum,
14
min_box=min_box,
15
max_box=max_box)
16
17
18
# violate max_sum and box constraints
19
weights <- c(0.35, 0.05, 0.7, 0.1)
20
sum(weights)
21
22
rp_transform(w=weights,
23
min_sum=min_sum,
24
max_sum=max_sum,
25
min_box=min_box,
26
max_box=max_box)
27
28
29
# violate box constraints and leverage
30
weights <- c(-0.45, 0.45, 0.55, 0.45)
31
sum(weights)
32
sum(abs(weights))
33
leverage <- 1.6
34
35
rp_transform(w=weights,
36
min_sum=min_sum,
37
max_sum=max_sum,
38
min_box=min_box,
39
max_box=max_box,
40
leverage=leverage)
41
42
43
# violate max position limit constraint
44
weights <- c(0.15, 0.25, 0.4, 0.2)
45
sum(weights)
46
max_pos <- 3
47
48
rp_transform(w=weights,
49
min_sum=min_sum,
50
max_sum=max_sum,
51
min_box=min_box,
52
max_box=max_box,
53
max_pos=max_pos)
54
55
# violate position limit constraint
56
weights <- c(-0.05, -0.05, 0.4, 0.7)
57
sum(weights)
58
59
max_pos_short <- 1
60
61
rp_transform(w=weights,
62
min_sum=min_sum,
63
max_sum=max_sum,
64
min_box=min_box,
65
max_box=max_box,
66
max_pos_short=max_pos_short)
67
68
# violate position limit constraint
69
weights <- c(-0.05, -0.05, 0.4, 0.7)
70
sum(weights)
71
72
max_pos_long <- 3
73
max_pos_short <- 1
74
75
rp_transform(w=weights,
76
min_sum=min_sum,
77
max_sum=max_sum,
78
min_box=min_box,
79
max_box=max_box,
80
max_pos_long=max_pos_long,
81
max_pos_short=max_pos_short)
82
83
84
# violate position limit constraint
85
weights <- c(-0.05, -0.05, 0.4, 0.7)
86
sum(weights)
87
88
max_pos_long <- 3
89
max_pos_short <- 1
90
max_pos <- 3
91
92
rp_transform(w=weights,
93
min_sum=min_sum,
94
max_sum=max_sum,
95
min_box=min_box,
96
max_box=max_box,
97
max_pos=max_pos,
98
max_pos_long=max_pos_long,
99
max_pos_short=max_pos_short)
100
101
# violate position limit and leverage constraint
102
weights <- c(-0.25, -0.15, 0.4, 0.7)
103
sum(weights)
104
sum(abs(weights))
105
106
max_pos_long <- 3
107
max_pos_short <- 1
108
max_pos <- 3
109
leverage <- 1.3
110
111
rp_transform(w=weights,
112
min_sum=min_sum,
113
max_sum=max_sum,
114
min_box=min_box,
115
max_box=max_box,
116
max_pos=max_pos,
117
max_pos_long=max_pos_long,
118
max_pos_short=max_pos_short,
119
leverage=leverage)
120
121
# The second group is above cUP and the fourth group is below cLO
122
weights <- c(0.06, 0.1, 0.07, 0.2, 0.22, 0.10, 0.05, 0.08, 0.05, 0.04, 0.03)
123
sum(weights[1:2])
124
sum(weights[3:6])
125
sum(weights[7:10])
126
sum(weights[10:11])
127
sum(weights)
128
129
groups <- list(1:2,
130
3:6,
131
7:10,
132
10:11)
133
# group_pos <- c(2, 3, 2, 2)
134
group_pos <- NULL
135
cLO <- c(0.05, 0.10, 0.05, 0.08)
136
cUP <- c(0.4, 0.55, 0.65, 0.45)
137
min_sum <- 0.99
138
max_sum <- 1.01
139
min_box <- rep(0.05, length(weights))
140
max_box <- rep(0.65, length(weights))
141
142
group_fail(weights, groups, cLO, cUP, group_pos)
143
144
w <- rp_transform(weights, min_sum, max_sum, min_box, max_box, groups, cLO, cUP)
145
PortfolioAnalytics:::group_fail(w, groups, cLO, cUP, group_pos)
146
147
148
# Note that this was typically not working with max_permutations=200
149
# Relax constraints or increase max_permutations
150
151
# max_pos <- 3
152
# max_pos_long <- 4
153
# max_pos_short <- 4
154
# leverage <- Inf
155
# max_permutations <- 200
156
#
157
# rp_transform2(weights=weights,
158
# min_sum=min_sum,
159
# max_sum=max_sum,
160
# min_box=min_box,
161
# max_box=max_box,
162
# max_pos=max_pos,
163
# max_pos_long=max_pos_long,
164
# max_pos_short=max_pos_short,
165
# leverage=leverage,
166
# max_permutations=max_permutations)
167
168
169
170