Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/inst/tests/test_demo_min_expected_shortfall.R
1433 views
1
2
##### Load packages #####
3
require(testthat)
4
require(PortfolioAnalytics)
5
6
##### Source Demo Script #####
7
source(system.file("demo/demo_min_expected_shortfall.R", package="PortfolioAnalytics"))
8
9
context("demo_min_expected_shortfall")
10
11
###### ROI, full_investment, long only, min ES ######
12
context("minES.lo.ROI")
13
14
test_that("minES.lo.ROI contains ES as an objective",
15
{ expect_that(minES.lo.ROI$portfolio$objectives[[1]]$name == "ES", is_true()) })
16
17
test_that("minES.lo.ROI ES objective p=0.9",
18
{ expect_equal(minES.lo.ROI$portfolio$objectives[[1]]$arguments$p, 0.9) })
19
20
test_that("minES.lo.ROI objective measure ES = 0.01013571",
21
{ expect_equal(extractObjectiveMeasures(minES.lo.ROI)$ES, 0.01013571, tolerance=1e-6) })
22
23
test_that("minES.lo.ROI min box constraints are not violated",
24
{ expect_that(all(extractWeights(minES.lo.ROI) >= minES.lo.ROI$portfolio$constraints[[2]]$min), is_true()) })
25
26
test_that("minES.lo.ROI max box constraints are not violated",
27
{ expect_that(all(extractWeights(minES.lo.ROI) <= minES.lo.ROI$portfolio$constraints[[2]]$max), is_true()) })
28
29
###### ROI, full_investment, long only, min ES ######
30
context("minES.box.ROI")
31
32
test_that("minES.box.ROI contains ES as an objective",
33
{ expect_that(minES.box.ROI$portfolio$objectives[[1]]$name == "ES", is_true()) })
34
35
test_that("minES.box.ROI ES objective p=0.9",
36
{ expect_equal(minES.box.ROI$portfolio$objectives[[1]]$arguments$p, 0.9) })
37
38
test_that("minES.box.ROI objective measure ES = 0.01477709",
39
{ expect_equal(as.numeric(extractObjectiveMeasures(minES.box.ROI)$ES), 0.01477709, tolerance=1e-6) })
40
41
test_that("minES.box.ROI min box constraints are not violated",
42
{ expect_that(all(extractWeights(minES.box.ROI) >= minES.box.ROI$portfolio$constraints[[2]]$min), is_true()) })
43
44
test_that("minES.box.ROI max box constraints are not violated",
45
{ expect_that(all(extractWeights(minES.box.ROI) <= minES.box.ROI$portfolio$constraints[[2]]$max), is_true()) })
46
47
###### RP, full_investment, box, min ES ######
48
context("minES.box1.RP")
49
50
test_that("minES.box1.RP contains ES as an objective",
51
{ expect_that(minES.box1.RP$portfolio$objectives[[1]]$name == "ES", is_true()) })
52
53
test_that("minES.box1.RP ES objective p=0.9",
54
{ expect_equal(minES.box1.RP$portfolio$objectives[[1]]$arguments$p, 0.9) })
55
56
test_that("minES.box1.RP contains mean as an objective",
57
{ expect_that(minES.box1.RP$portfolio$objectives[[2]]$name == "mean", is_true()) })
58
59
test_that("minES.box1.RP objective measure ES is numeric",
60
{ expect_that(is.numeric(extractObjectiveMeasures(minES.box1.RP)$ES), is_true()) })
61
62
test_that("minES.box1.RP objective measure mean is numeric",
63
{ expect_that(is.numeric(extractObjectiveMeasures(minES.box1.RP)$mean), is_true()) })
64
65
test_that("minES.box1.RP min box constraints are not violated",
66
{ expect_that(all(extractWeights(minES.box1.RP) >= minES.box1.RP$portfolio$constraints[[2]]$min), is_true()) })
67
68
test_that("minES.box1.RP max box constraints are not violated",
69
{ expect_that(all(extractWeights(minES.box1.RP) <= minES.box1.RP$portfolio$constraints[[2]]$max), is_true()) })
70
71
###### RP, full_investment, box, min ES ######
72
context("minES.box2.RP")
73
74
test_that("minES.box2.RP contains ES as an objective",
75
{ expect_that(minES.box2.RP$portfolio$objectives[[1]]$name == "ES", is_true()) })
76
77
test_that("minES.box2.RP ES objective p=0.9",
78
{ expect_equal(minES.box2.RP$portfolio$objectives[[1]]$arguments$p, 0.9) })
79
80
test_that("minES.box2.RP contains mean as an objective",
81
{ expect_that(minES.box2.RP$portfolio$objectives[[2]]$name == "mean", is_true()) })
82
83
test_that("minES.box2.RP objective measure ES is numeric",
84
{ expect_that(is.numeric(extractObjectiveMeasures(minES.box2.RP)$ES), is_true()) })
85
86
test_that("minES.box2.RP objective measure mean is numeric",
87
{ expect_that(is.numeric(extractObjectiveMeasures(minES.box2.RP)$mean), is_true()) })
88
89
test_that("minES.box2.RP min box constraints are not violated",
90
{ expect_that(all(extractWeights(minES.box2.RP) >= minES.box2.RP$portfolio$constraints[[2]]$min), is_true()) })
91
92
test_that("minES.box2.RP max box constraints are not violated",
93
{ expect_that(all(extractWeights(minES.box2.RP) <= minES.box2.RP$portfolio$constraints[[2]]$max), is_true()) })
94
95
###### DE, full_investment, box, min ES ######
96
context("minES.box1.DE")
97
98
test_that("minES.box.DE contains ES as an objective",
99
{ expect_that(minES.box.DE$portfolio$objectives[[1]]$name == "ES", is_true()) })
100
101
test_that("minES.box.DE ES objective p=0.9",
102
{ expect_equal(minES.box.DE$portfolio$objectives[[1]]$arguments$p, 0.9) })
103
104
test_that("minES.box2.DE contains mean as an objective",
105
{ expect_that(minES.box.DE$portfolio$objectives[[2]]$name == "mean", is_true()) })
106
107
test_that("minES.box.DE objective measure ES is numeric",
108
{ expect_that(is.numeric(extractObjectiveMeasures(minES.box.DE)$ES), is_true()) })
109
110
test_that("minES.box.DE objective measure mean is numeric",
111
{ expect_that(is.numeric(extractObjectiveMeasures(minES.box.DE)$mean), is_true()) })
112
113
test_that("minES.box.DE min box constraints are not violated",
114
{ expect_that(all(extractWeights(minES.box.DE) >= minES.box.DE$portfolio$constraints[[2]]$min), is_true()) })
115
116
test_that("minES.box.DE max box constraints are not violated",
117
{ expect_that(all(extractWeights(minES.box.DE) <= minES.box.DE$portfolio$constraints[[2]]$max), is_true()) })
118
119
120
121