Path: blob/master/inst/tests/test_demo_leverage.R
1433 views
1##### Load packages #####2require(testthat)3require(PortfolioAnalytics)45##### Source Demo Script #####6source(system.file("demo/demo_leverage_exposure_constraint.R", package="PortfolioAnalytics"))789context("dollar neutral portfolio")1011test_that("dollar.neutral.portf min_sum constraint is -0.01",12{ expect_equal(dollar.neutral.portf$constraints[[1]]$min_sum, -0.01) })1314test_that("dollar.neutral.portf max_sum constraint is 0.01",15{ expect_equal(dollar.neutral.portf$constraints[[1]]$max_sum, 0.01) })1617test_that("dollar.neutral.portf leverage exposure constraint is 2",18{ expect_equal(dollar.neutral.portf$constraints[[3]]$leverage, 2) })1920test_that("dollar.neutral.portf weights is a numeric vector",21{ expect_that(is.numeric(extractWeights(dollar.neutral.opt)), is_true()) })2223test_that("dollar.neutral.portf leverage exposure constraint is not violated",24{ expect_that(sum(abs(extractWeights(dollar.neutral.opt))) <= 2, is_true()) })2526test_that("dollar.neutral.portf objective measure mean is numeric",27{ expect_that(is.numeric(extractObjectiveMeasures(dollar.neutral.opt)$mean), is_true()) })2829test_that("dollar.neutral.portf objective measure ES is numeric",30{ expect_that(is.numeric(extractObjectiveMeasures(dollar.neutral.opt)$ES), is_true()) })313233context("leveraged portfolio")3435test_that("leveraged.portf min_sum constraint is 0.99",36{ expect_equal(leveraged.portf$constraints[[1]]$min_sum, 0.99) })3738test_that("leveraged.portf max_sum constraint is 1.01",39{ expect_equal(leveraged.portf$constraints[[1]]$max_sum, 1.01) })4041test_that("leveraged.portf leverage exposure constraint is 1.6",42{ expect_equal(leveraged.portf$constraints[[3]]$leverage, 1.6) })4344test_that("leveraged.opt weights is a numeric vector",45{ expect_that(is.numeric(extractWeights(leveraged.opt)), is_true()) })4647test_that("leveraged.opt leverage exposure constraint is not violated",48{ expect_that(sum(abs(extractWeights(leveraged.opt))) <= 1.6, is_true()) })4950test_that("leveraged.opt objective measure mean is numeric",51{ expect_that(is.numeric(extractObjectiveMeasures(leveraged.opt)$mean), is_true()) })5253test_that("leveraged.opt objective measure ES is numeric",54{ expect_that(is.numeric(extractObjectiveMeasures(leveraged.opt)$ES), is_true()) })555657