Path: blob/master/inst/tests/test_demo_group_constraints.R
1433 views
1##### Load packages #####2require(testthat)3require(PortfolioAnalytics)45##### Source Demo Script #####6source(system.file("demo/demo_group_constraints.R", package="PortfolioAnalytics"))78##### Test the constraints #####9context("demo_group_constraints")1011group_constr <- init.portf$constraints[[3]]1213test_that("init.portf contains groups as a constraint",14{ expect_that(inherits(group_constr, "group_constraint"), is_true()) })1516test_that("group constraint for groupA is c(1, 3, 5)",17{ expect_equal(group_constr$groups$groupA, c(1, 3, 5)) })1819test_that("group constraint for groupB is c(2, 4)",20{ expect_equal(group_constr$groups$groupB, c(2, 4)) })2122test_that("group constraint cLO is c(0.05, 0.15)",23{ expect_equal(group_constr$cLO, c(0.05, 0.15)) })2425test_that("group constraint cUP is c(0.7, 0.5)",26{ expect_equal(group_constr$cUP, c(0.7, 0.5)) })2728cLO <- group_constr$cLO29cUP <- group_constr$cUP3031##### ROI Optimization #####32context("demo_group_constraints optimization")3334test_that("minStdDev.ROI weights equal c(4.593895e-03, 2.540430e-01, -1.387779e-17, 4.595703e-02, 6.954061e-01)",35{ expect_equal(as.numeric(extractWeights(minStdDev.ROI)), c(4.593895e-03, 2.540430e-01, -1.387779e-17, 4.595703e-02, 6.954061e-01),36tolerance=1e-6) })3738test_that("minStdDev.ROI objective measure StdDev = 0.01042408",39{ expect_equal(as.numeric(extractObjectiveMeasures(minStdDev.ROI)$StdDev), 0.01042408,40tolerance=1e-6) })4142weights.ROI <- extractWeights(minStdDev.ROI)4344test_that("minStdDev.ROI group weights are calculated correctly",45{ expect_equal(as.numeric(extractGroups(minStdDev.ROI)$group_weights),46c(sum(weights.ROI[c(1, 3, 5)]), sum(weights.ROI[c(2, 4)]))) })4748test_that("minStdDev.ROI group constraint cLO is not violated",49{ expect_that(all(extractGroups(minStdDev.ROI)$group_weights >= cLO), is_true()) })5051test_that("minStdDev.ROI group constraint cUP is not violated",52{ expect_that(all(extractGroups(minStdDev.ROI)$group_weights <= cUP), is_true()) })535455##### RP Optimization #####56context("minStdDev.RP")5758test_that("minStdDev.RP weights is a numeric vector",59{ expect_that(is.numeric(extractWeights(minStdDev.RP)), is_true()) })6061test_that("minStdDev.RP objective measure StdDev is numeric",62{ expect_that(is.numeric(extractObjectiveMeasures(minStdDev.RP)$StdDev), is_true()) })6364weights.RP <- extractWeights(minStdDev.RP)6566test_that("minStdDev.RP group weights are calculated correctly",67{ expect_equal(as.numeric(extractGroups(minStdDev.RP)$group_weights),68c(sum(weights.RP[c(1, 3, 5)]), sum(weights.RP[c(2, 4)]))) })6970test_that("minStdDev.RP group constraint cLO is not violated",71{ expect_that(all(extractGroups(minStdDev.RP)$group_weights >= cLO), is_true()) })7273test_that("minStdDev.RP group constraint cUP is not violated",74{ expect_that(all(extractGroups(minStdDev.RP)$group_weights <= cUP), is_true()) })757677##### DE Optimization #####78context("minStdDev.DE")7980test_that("minStdDev.DE weights is a numeric vector",81{ expect_that(is.numeric(extractWeights(minStdDev.DE)), is_true()) })8283test_that("minStdDev.DE objective measure StdDev is numeric",84{ expect_that(is.numeric(extractObjectiveMeasures(minStdDev.ROI)$StdDev), is_true()) })8586weights.DE <- extractWeights(minStdDev.DE)8788test_that("minStdDev.DE group weights are calculated correctly",89{ expect_equal(as.numeric(extractGroups(minStdDev.DE)$group_weights),90c(sum(weights.DE[c(1, 3, 5)]), sum(weights.DE[c(2, 4)]))) })9192test_that("minStdDev.DE group constraint cLO is not violated",93{ expect_that(all(extractGroups(minStdDev.DE)$group_weights >= cLO), is_true()) })9495test_that("minStdDev.DE group constraint cUP is not violated",96{ expect_that(all(extractGroups(minStdDev.DE)$group_weights <= cUP), is_true()) })979899