Path: blob/master/inst/tests/test_demo_min_expected_shortfall.R
1433 views
1##### Load packages #####2require(testthat)3require(PortfolioAnalytics)45##### Source Demo Script #####6source(system.file("demo/demo_min_expected_shortfall.R", package="PortfolioAnalytics"))78context("demo_min_expected_shortfall")910###### ROI, full_investment, long only, min ES ######11context("minES.lo.ROI")1213test_that("minES.lo.ROI contains ES as an objective",14{ expect_that(minES.lo.ROI$portfolio$objectives[[1]]$name == "ES", is_true()) })1516test_that("minES.lo.ROI ES objective p=0.9",17{ expect_equal(minES.lo.ROI$portfolio$objectives[[1]]$arguments$p, 0.9) })1819test_that("minES.lo.ROI objective measure ES = 0.01013571",20{ expect_equal(extractObjectiveMeasures(minES.lo.ROI)$ES, 0.01013571, tolerance=1e-6) })2122test_that("minES.lo.ROI min box constraints are not violated",23{ expect_that(all(extractWeights(minES.lo.ROI) >= minES.lo.ROI$portfolio$constraints[[2]]$min), is_true()) })2425test_that("minES.lo.ROI max box constraints are not violated",26{ expect_that(all(extractWeights(minES.lo.ROI) <= minES.lo.ROI$portfolio$constraints[[2]]$max), is_true()) })2728###### ROI, full_investment, long only, min ES ######29context("minES.box.ROI")3031test_that("minES.box.ROI contains ES as an objective",32{ expect_that(minES.box.ROI$portfolio$objectives[[1]]$name == "ES", is_true()) })3334test_that("minES.box.ROI ES objective p=0.9",35{ expect_equal(minES.box.ROI$portfolio$objectives[[1]]$arguments$p, 0.9) })3637test_that("minES.box.ROI objective measure ES = 0.01477709",38{ expect_equal(as.numeric(extractObjectiveMeasures(minES.box.ROI)$ES), 0.01477709, tolerance=1e-6) })3940test_that("minES.box.ROI min box constraints are not violated",41{ expect_that(all(extractWeights(minES.box.ROI) >= minES.box.ROI$portfolio$constraints[[2]]$min), is_true()) })4243test_that("minES.box.ROI max box constraints are not violated",44{ expect_that(all(extractWeights(minES.box.ROI) <= minES.box.ROI$portfolio$constraints[[2]]$max), is_true()) })4546###### RP, full_investment, box, min ES ######47context("minES.box1.RP")4849test_that("minES.box1.RP contains ES as an objective",50{ expect_that(minES.box1.RP$portfolio$objectives[[1]]$name == "ES", is_true()) })5152test_that("minES.box1.RP ES objective p=0.9",53{ expect_equal(minES.box1.RP$portfolio$objectives[[1]]$arguments$p, 0.9) })5455test_that("minES.box1.RP contains mean as an objective",56{ expect_that(minES.box1.RP$portfolio$objectives[[2]]$name == "mean", is_true()) })5758test_that("minES.box1.RP objective measure ES is numeric",59{ expect_that(is.numeric(extractObjectiveMeasures(minES.box1.RP)$ES), is_true()) })6061test_that("minES.box1.RP objective measure mean is numeric",62{ expect_that(is.numeric(extractObjectiveMeasures(minES.box1.RP)$mean), is_true()) })6364test_that("minES.box1.RP min box constraints are not violated",65{ expect_that(all(extractWeights(minES.box1.RP) >= minES.box1.RP$portfolio$constraints[[2]]$min), is_true()) })6667test_that("minES.box1.RP max box constraints are not violated",68{ expect_that(all(extractWeights(minES.box1.RP) <= minES.box1.RP$portfolio$constraints[[2]]$max), is_true()) })6970###### RP, full_investment, box, min ES ######71context("minES.box2.RP")7273test_that("minES.box2.RP contains ES as an objective",74{ expect_that(minES.box2.RP$portfolio$objectives[[1]]$name == "ES", is_true()) })7576test_that("minES.box2.RP ES objective p=0.9",77{ expect_equal(minES.box2.RP$portfolio$objectives[[1]]$arguments$p, 0.9) })7879test_that("minES.box2.RP contains mean as an objective",80{ expect_that(minES.box2.RP$portfolio$objectives[[2]]$name == "mean", is_true()) })8182test_that("minES.box2.RP objective measure ES is numeric",83{ expect_that(is.numeric(extractObjectiveMeasures(minES.box2.RP)$ES), is_true()) })8485test_that("minES.box2.RP objective measure mean is numeric",86{ expect_that(is.numeric(extractObjectiveMeasures(minES.box2.RP)$mean), is_true()) })8788test_that("minES.box2.RP min box constraints are not violated",89{ expect_that(all(extractWeights(minES.box2.RP) >= minES.box2.RP$portfolio$constraints[[2]]$min), is_true()) })9091test_that("minES.box2.RP max box constraints are not violated",92{ expect_that(all(extractWeights(minES.box2.RP) <= minES.box2.RP$portfolio$constraints[[2]]$max), is_true()) })9394###### DE, full_investment, box, min ES ######95context("minES.box1.DE")9697test_that("minES.box.DE contains ES as an objective",98{ expect_that(minES.box.DE$portfolio$objectives[[1]]$name == "ES", is_true()) })99100test_that("minES.box.DE ES objective p=0.9",101{ expect_equal(minES.box.DE$portfolio$objectives[[1]]$arguments$p, 0.9) })102103test_that("minES.box2.DE contains mean as an objective",104{ expect_that(minES.box.DE$portfolio$objectives[[2]]$name == "mean", is_true()) })105106test_that("minES.box.DE objective measure ES is numeric",107{ expect_that(is.numeric(extractObjectiveMeasures(minES.box.DE)$ES), is_true()) })108109test_that("minES.box.DE objective measure mean is numeric",110{ expect_that(is.numeric(extractObjectiveMeasures(minES.box.DE)$mean), is_true()) })111112test_that("minES.box.DE min box constraints are not violated",113{ expect_that(all(extractWeights(minES.box.DE) >= minES.box.DE$portfolio$constraints[[2]]$min), is_true()) })114115test_that("minES.box.DE max box constraints are not violated",116{ expect_that(all(extractWeights(minES.box.DE) <= minES.box.DE$portfolio$constraints[[2]]$max), is_true()) })117118119120121