Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/testing_fn_map.R
1433 views
1
library(PortfolioAnalytics)
2
3
data(edhec)
4
ret <- edhec[, 1:4]
5
funds <- colnames(ret)
6
7
pspec <- portfolio.spec(assets=funds)
8
9
pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=T)
10
pspec <- add.constraint(portfolio=pspec, type="box", min=0.05, max=0.65, enabled=T)
11
pspec <- add.constraint(portfolio=pspec, type="group", groups=list(1:2,3:4),
12
group_min=c(0.08, 0.05), group_max=c(0.55, 0.85), enabled=T)
13
pspec <- add.constraint(portfolio=pspec, type="turnover", turnover_target=0.4, enabled=F)
14
pspec <- add.constraint(portfolio=pspec, type="diversification", div_target=0.6, enabled=F)
15
pspec <- add.constraint(portfolio=pspec, type="position_limit", max_pos=3, enabled=T)
16
portfolio <- pspec
17
18
19
# leverage and position_limit constraints are violated
20
weights <- c(0.15, 0.25, 0.4, 0.1)
21
sum(weights)
22
23
fn_map(weights, portfolio)
24
25
# box constraints are violated but postion_limit is already satisfied
26
# issue because min vector does not have a zero value and weights[1] = 0
27
# all constraints are satisfied so there should be no transformation
28
# Is it reasonable to expect the user to have a min vector with zeros when using position_limit constraints?
29
# I try to catch this and modify the tmp_min vector so this does not trigger
30
# violation of box constraints
31
weights <- c(0, 0.55, 0.3, 0.15)
32
sum(weights)
33
34
fn_map(weights, portfolio)
35
36
# group and position limit constraints are violated
37
weights <- c(0.1, 0.65, 0.1, 0.15)
38
sum(weights)
39
40
fn_map(weights, portfolio)
41
42
# normalize weights from the equal weights seed portfolio
43
weights <- portfolio$assets
44
sum(weights)
45
46
fn_map(weights, portfolio)
47
48
##### relaxing box constraints #####
49
pspec <- portfolio.spec(assets=funds)
50
51
pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=T)
52
# make min infeasible and too restrictive
53
pspec <- add.constraint(portfolio=pspec, type="box", min=0.3, max=0.75, enabled=T)
54
55
# weights satisfy leverage constraints but not box constraints
56
weights <- c(0.15, 0.05, 0.25, 0.55)
57
sum(weights)
58
59
# min constraint needs to be relaxed
60
# note how min has been changed
61
fn_map(weights, pspec, TRUE)
62
63
##### relaxing group constraints #####
64
pspec <- portfolio.spec(assets=funds)
65
66
pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=T)
67
pspec <- add.constraint(portfolio=pspec, type="box", min=0.05, max=0.7, enabled=T)
68
# Make group constraints too restrictive
69
pspec <- add.constraint(portfolio=pspec, type="group", groups=list(1:2, 3:4),
70
group_min=c(0.05, 0.01), group_max=c(0.45, 0.5), enabled=T)
71
72
# weights satisfy leverage and box constraints, but not group
73
weights <- c(0.15, 0.05, 0.10, 0.7)
74
75
# group constraints needs to be relaxed
76
# note how cLO and cUP have been changed
77
fn_map(weights, pspec, TRUE)
78
79
##### relaxing position limits constraints #####
80
pspec <- portfolio.spec(assets=funds)
81
82
pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=T)
83
pspec <- add.constraint(portfolio=pspec, type="box", min=0.05, max=0.4, enabled=T)
84
# Make position limit constraint too restrictive
85
pspec <- add.constraint(portfolio=pspec, type="position_limit", max_pos=2, enabled=T)
86
87
# weights satisfy leverage and box constraints, but not group
88
weights <- c(0.4, 0.05, 0.15, 0.4)
89
90
# position limit constraint needs to be relaxed
91
# note how max_pos has been increased to 3
92
fn_map(weights, pspec, TRUE)
93
94
##### relaxing leverage exposure constraint #####
95
pspec <- portfolio.spec(assets=funds)
96
pspec <- add.constraint(portfolio=pspec, type="weight_sum",
97
min_sum=0.99, max_sum=1.01)
98
pspec <- add.constraint(portfolio=pspec, type="box", min=-0.4, max=1)
99
pspec <- add.constraint(portfolio=pspec, type="leverage_exposure", leverage=1.6)
100
101
# weights satisfy leverage and box constraints, but not group
102
weights <- c(-0.4, 0.75, 0.25, 0.4)
103
sum(weights)
104
sum(abs(weights))
105
106
# relax leverage exposure constraint
107
fn_map(weights, pspec, TRUE)
108
109
rp_transform(weights, min_sum=0.99, max_sum=1.01,
110
min_box=rep(-0.3, 4), max_box=rep(0.6,4),
111
groups=NULL, cLO=NULL, cUP=NULL,
112
leverage=1.5, max_permutations=10000)
113
114
pspec <- portfolio.spec(assets=funds)
115
pspec <- add.constraint(portfolio=pspec, type="weight_sum",
116
min_sum=0.99, max_sum=1.01)
117
pspec <- add.constraint(portfolio=pspec, type="box", min=-0.4, max=1)
118
pspec <- add.constraint(portfolio=pspec, type="leverage_exposure", leverage=1.6)
119
rp <- random_portfolios(pspec, 5000, eliminate=FALSE)
120
x <- apply(rp, 1, function(x) sum(abs(x)))
121
plot(x)
122
123