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