Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/inst/tests/test_rp_sample.R
1433 views
1
2
require(testthat)
3
require(PortfolioAnalytics)
4
5
context("random portfolios sample method")
6
7
data(edhec)
8
ret <- edhec[, 1:4]
9
funds <- colnames(ret)
10
11
init.portf <- portfolio.spec(assets=funds)
12
init.portf <- add.constraint(init.portf, type="weight_sum",
13
min_sum=0.99, max_sum=1.01)
14
init.portf <- add.constraint(init.portf, type="box",
15
min=-0.3, max=0.65)
16
17
# generate portfolios to satisfy weight_sum and box constraints
18
rp1 <- random_portfolios(init.portf, 1000, eliminate=FALSE)
19
test_that("we have created at least 1 feasible portfolio to satisfy weight_sum and box constraints", {
20
expect_that(any(apply(rp1, 1, PortfolioAnalytics:::check_constraints, portfolio=group.portf)), is_true())
21
})
22
23
# portfolio with group constraints
24
group.portf <- add.constraint(init.portf, type="group",
25
groups=list(1:2,3:4),
26
group_min=c(0.08, 0.05),
27
group_max=c(0.55, 0.85),
28
group_pos=c(2,2))
29
30
# generate portfolios to satisfy weight_sum, box, and group constraints
31
rp2 <- random_portfolios(group.portf, 1000, eliminate=FALSE)
32
test_that("we have created at least 1 feasible portfolio to satisfy weight_sum, box, and group constraints", {
33
expect_that(any(apply(rp2, 1, PortfolioAnalytics:::check_constraints, portfolio=group.portf)), is_true())
34
})
35
36
# add leverage exposure constraint
37
lev.portf <- add.constraint(init.portf, type="leverage_exposure",
38
leverage=1.6)
39
40
# generate portfolios to satisfy weight_sum, box, and leverage constraints
41
rp3 <- random_portfolios(lev.portf, 1000, eliminate=FALSE)
42
test_that("we have created at least 1 feasible portfolio to satisfy weight_sum, box, and leverage constraints", {
43
expect_that(any(apply(rp3, 1, PortfolioAnalytics:::check_constraints, portfolio=group.portf)), is_true())
44
})
45
46
# add position limit constraint
47
pos1.portf <- add.constraint(init.portf, type="position_limit",
48
max_pos=3)
49
50
# generate portfolios to satisfy weight_sum, box, and position limit constraints
51
rp4 <- random_portfolios(pos1.portf, 1000, eliminate=FALSE)
52
test_that("we have created at least 1 feasible portfolio to satisfy weight_sum, box, and position limit constraints", {
53
expect_that(any(apply(rp4, 1, PortfolioAnalytics:::check_constraints, portfolio=group.portf)), is_true())
54
})
55
56
# add position limit constraint with long and short position limits
57
pos2.portf <- add.constraint(init.portf, type="position_limit",
58
max_pos_long=3, max_pos_short=1)
59
60
# generate portfolios to satisfy weight_sum, box, and position limit constraints
61
rp5 <- random_portfolios(pos2.portf, 1000, eliminate=FALSE)
62
test_that("we have created at least 1 feasible portfolio to satisfy weight_sum, box, and long/short position limit constraints", {
63
expect_that(any(apply(rp5, 1, PortfolioAnalytics:::check_constraints, portfolio=group.portf)), is_true())
64
})
65
66
67