Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/testing_constrained_group.R
1433 views
1
2
3
##### EX1 #####
4
# first group exceeds cUP
5
groups <- c(2, 1)
6
cLO <- c(0.2, 0.10)
7
cUP <- c(0.4, 0.55)
8
9
weights <- c(0.15, 0.35, 0.50)
10
sum(weights)
11
12
(w <- constrained_group_tmp(groups, cLO, cUP, weights, 1, 1, TRUE))
13
sum(w[1:2])
14
sum(w)
15
16
(w <- constrained_group_tmp(groups, cLO, cUP, weights, 1, 1, FALSE))
17
# The group 1 cUP is met exactly, but the sume of weights are not equal to 1
18
sum(w[1:2])
19
sum(w)
20
21
22
##### EX2 #####
23
# The assets are grouped into 3 groups of 2
24
# The sum of the weights for the first group assets must be between 0.05 and 0.35
25
# The sum of the weights for the second group of assets must be between 0.10 and 0.45
26
# The sum of the weights for the last group of assets must be between 0.05 and 0.25
27
28
# first group exceeds cUP
29
groups <- c(2, 2, 2)
30
cLO <- c(0.05, 0.10, 0.05)
31
cUP <- c(0.4, 0.45, 0.25)
32
33
weights <- c(0.15, 0.30, 0.15, 0.25, 0.05, 0.10)
34
sum(weights)
35
36
(w <- constrained_group_tmp(groups, cLO, cUP, weights, 1, 1, TRUE))
37
sum(w)
38
39
##### Ex3 #####
40
# The second group is below cLO
41
groups <- c(2, 1, 3)
42
cLO <- c(0.05, 0.10, 0.05)
43
cUP <- c(0.4, 0.45, 0.65)
44
45
weights <- c(0.15, 0.25, 0.08, 0.2, 0.22, 0.10)
46
sum(weights)
47
48
(w <- constrained_group_tmp(groups, cLO, cUP, weights, 1, 1, TRUE))
49
sum(w)
50
51
##### Ex4 #####
52
# The second group is above cUP and the fourth group is below cLO
53
groups <- c(2, 4, 3, 2)
54
cLO <- c(0.05, 0.10, 0.05, 0.08)
55
cUP <- c(0.4, 0.5, 0.65, 0.45)
56
57
weights <- c(0.05, 0.1, 0.07, 0.2, 0.22, 0.10, 0.05, 0.08, 0.05, 0.04, 0.04)
58
sum(weights[1:2])
59
sum(weights[3:6])
60
sum(weights[7:10])
61
sum(weights[10:11])
62
sum(weights)
63
64
(w <- constrained_group_tmp(groups, cLO, cUP, weights, 1, 1, TRUE))
65
sum(w[1:2])
66
sum(w[3:6])
67
sum(w[7:10])
68
sum(w[10:11])
69
70
# Group 2 cUP is being violated. Appears that normalizing at the end of the
71
# function is causing some of the group constraints to be violated
72
73