Path: blob/master/inst/tests/test_demo_max_qu.R
1433 views
##### Load packages #####1require(testthat)2require(PortfolioAnalytics)34##### Source Demo Script #####5source(system.file("demo/demo_max_quadratic_utility.R", package="PortfolioAnalytics"))678##### init.portf objectives #####9context("objectives for quadratic utility")1011test_that("init.portf contains mean as an objective",12{ expect_that(init.portf$objectives[[1]]$name == "mean", is_true()) })1314test_that("init.portf contains StdDev as an objective",15{ expect_that(init.portf$objectives[[2]]$name == "StdDev", is_true()) })1617##### ROI, full_investment, long only, max qu ######18context("maxQU.lo.ROI")1920test_that("risk aversion parameter = 4",21{ expect_equal(maxQU.lo.ROI$portfolio$objectives[[2]]$risk_aversion, 4) })2223test_that("maxQU.lo.ROI objective measure mean = 0.007813251",24{ expect_equal(as.numeric(extractObjectiveMeasures(maxQU.lo.ROI)$mean), 0.007813251, tolerance=1e-6) })2526test_that("maxQU.lo.ROI objective measure StdDev = 0.01556929",27{ expect_equal(as.numeric(extractObjectiveMeasures(maxQU.lo.ROI)$StdDev), 0.01556929, tolerance=1e-6) })2829test_that("maxQU.lo.ROI min box constraints are not violated",30{ expect_that(all(extractWeights(maxQU.lo.ROI) >= maxQU.lo.ROI$portfolio$constraints[[2]]$min), is_true()) })3132test_that("maxret.lo.ROI max box constraints are not violated",33{ expect_that(all(extractWeights(maxQU.lo.ROI) <= maxQU.lo.ROI$portfolio$constraints[[2]]$max), is_true()) })3435##### ROI, full_investment, long only, max qu to approximate max return ######36context("maxQU.maxret.ROI")3738test_that("risk aversion parameter = 1e-6",39{ expect_equal(maxQU.maxret.ROI$portfolio$objectives[[2]]$risk_aversion, 1e-6) })4041test_that("maxQU.maxret.ROI objective measure mean = 0.008246053",42{ expect_equal(as.numeric(extractObjectiveMeasures(maxQU.maxret.ROI)$mean), 0.008246053, tolerance=1e-6) })4344test_that("maxQU.maxret.ROI objective measure StdDev = 0.03857144",45{ expect_equal(as.numeric(extractObjectiveMeasures(maxQU.maxret.ROI)$StdDev), 0.03857144, tolerance=1e-6) })4647test_that("maxQU.maxret.ROI min box constraints are not violated",48{ expect_that(all(extractWeights(maxQU.maxret.ROI) >= maxQU.maxret.ROI$portfolio$constraints[[2]]$min), is_true()) })4950test_that("maxQU.maxret.ROI max box constraints are not violated",51{ expect_that(all(extractWeights(maxQU.maxret.ROI) <= maxQU.maxret.ROI$portfolio$constraints[[2]]$max), is_true()) })5253##### ROI, full_investment, long only, max qu to approximate min StdDev ######54context("maxQU.minvol.ROI")5556test_that("risk aversion parameter = 1e6",57{ expect_equal(maxQU.minvol.ROI$portfolio$objectives[[2]]$risk_aversion, 1e6) })5859test_that("maxQU.minvol.ROI objective measure mean = 0.00603498",60{ expect_equal(as.numeric(extractObjectiveMeasures(maxQU.minvol.ROI)$mean), 0.00603498, tolerance=1e-6) })6162test_that("maxQU.minvol.ROI objective measure StdDev = 0.008251084",63{ expect_equal(as.numeric(extractObjectiveMeasures(maxQU.minvol.ROI)$StdDev), 0.008251084, tolerance=1e-6) })6465test_that("maxQU.minvol.ROI min box constraints are not violated",66{ expect_that(all(extractWeights(maxQU.minvol.ROI) >= maxQU.minvol.ROI$portfolio$constraints[[2]]$min), is_true()) })6768test_that("maxQU.minvol.ROI max box constraints are not violated",69{ expect_that(all(extractWeights(maxQU.minvol.ROI) <= maxQU.minvol.ROI$portfolio$constraints[[2]]$max), is_true()) })707172