Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/inst/tests/test_backwards_compat.R
1433 views
1
2
##### Load packages #####
3
require(testthat)
4
require(PortfolioAnalytics)
5
6
##### Source Demo Script #####
7
source(system.file("demo/backwards_compat.R", package="PortfolioAnalytics"))
8
9
context("Backwards compatibility is maintained")
10
11
# class
12
test_that("Class of gen.constr is v1_constraint",
13
{ expect_that(inherits(gen.constr, "v1_constraint"), is_true()) })
14
15
# assets
16
test_that("Initial assets form an equal weight portfolio",
17
{ expect_that(all.equal(as.numeric(gen.constr$assets), rep(1/4, 4)), is_true()) })
18
19
# min
20
test_that("Box constraints min vector is all 0s",
21
{ expect_that(all.equal(as.numeric(gen.constr$min), rep(0, 4)), is_true()) })
22
23
# max
24
test_that("Box constraints max vector is all 0.55",
25
{ expect_that(all.equal(as.numeric(gen.constr$max), rep(0.55, 4)), is_true()) })
26
27
# min_mult
28
test_that("min_mult is null",
29
{ expect_that(is.null(gen.constr$min_mult), is_true()) })
30
31
# max_mult
32
test_that("max_mult is null",
33
{ expect_that(is.null(gen.constr$max_mult), is_true()) })
34
35
# min_sum
36
test_that("min_sum is 0.99",
37
{ expect_that(all.equal(gen.constr$min_sum, 0.99), is_true()) })
38
39
# max_sum
40
test_that("min_sum is 1.01",
41
{ expect_that(all.equal(gen.constr$max_sum, 1.01), is_true()) })
42
43
# mean objective
44
test_that("The objective name is 'mean'",
45
{ expect_that(all.equal(gen.constr$objectives[[1]]$name, "mean"), is_true()) })
46
47
context("Optimization output")
48
49
# Not sure how to test for exact values of optimization results for DEoptim
50
# and random portfolios
51
# - use a specific data set of rp weights
52
53
# random portfolios optimization
54
test_that("random portfolios updated portfolio object",
55
{ expect_that(inherits(optrpv1$portfolio, "portfolio.spec"), is_true()) })
56
57
test_that("random portfolios returns optimal weights",
58
{ expect_that(is.numeric(extractWeights(optrpv1)), is_true()) })
59
60
test_that("random portfolios returns an objective measure",
61
{ expect_that(is.numeric(extractObjectiveMeasures(optrpv1)$mean), is_true()) })
62
63
# DEoptim optimization
64
test_that("DE optim updated portfolio object",
65
{ expect_that(inherits(optdev1$portfolio, "portfolio.spec"), is_true()) })
66
67
test_that("DE optim returns optimal weights",
68
{ expect_that(is.numeric(extractWeights(optdev1)), is_true()) })
69
70
test_that("DE optim returns an objective measure",
71
{ expect_that(is.numeric(extractObjectiveMeasures(optdev1)$mean), is_true()) })
72
73
# ROI optimization
74
test_that("ROI updated portfolio object",
75
{ expect_that(inherits(optroiv1$portfolio, "portfolio.spec"), is_true()) })
76
77
test_that("ROI returns optimal weights equal to c(0, 0, 0.46, 0.55)",
78
{ expect_equal(as.numeric(extractWeights(optroiv1)), c(0, 0, 0.46, 0.55)) })
79
80
test_that("ROI returns an objective measure mean=0.008193842",
81
{ expect_equal(as.numeric(extractObjectiveMeasures(optroiv1)$mean), 0.008193842) })
82
83
84