Path: blob/master/inst/tests/test_backwards_compat.R
1433 views
1##### Load packages #####2require(testthat)3require(PortfolioAnalytics)45##### Source Demo Script #####6source(system.file("demo/backwards_compat.R", package="PortfolioAnalytics"))78context("Backwards compatibility is maintained")910# class11test_that("Class of gen.constr is v1_constraint",12{ expect_that(inherits(gen.constr, "v1_constraint"), is_true()) })1314# assets15test_that("Initial assets form an equal weight portfolio",16{ expect_that(all.equal(as.numeric(gen.constr$assets), rep(1/4, 4)), is_true()) })1718# min19test_that("Box constraints min vector is all 0s",20{ expect_that(all.equal(as.numeric(gen.constr$min), rep(0, 4)), is_true()) })2122# max23test_that("Box constraints max vector is all 0.55",24{ expect_that(all.equal(as.numeric(gen.constr$max), rep(0.55, 4)), is_true()) })2526# min_mult27test_that("min_mult is null",28{ expect_that(is.null(gen.constr$min_mult), is_true()) })2930# max_mult31test_that("max_mult is null",32{ expect_that(is.null(gen.constr$max_mult), is_true()) })3334# min_sum35test_that("min_sum is 0.99",36{ expect_that(all.equal(gen.constr$min_sum, 0.99), is_true()) })3738# max_sum39test_that("min_sum is 1.01",40{ expect_that(all.equal(gen.constr$max_sum, 1.01), is_true()) })4142# mean objective43test_that("The objective name is 'mean'",44{ expect_that(all.equal(gen.constr$objectives[[1]]$name, "mean"), is_true()) })4546context("Optimization output")4748# Not sure how to test for exact values of optimization results for DEoptim49# and random portfolios50# - use a specific data set of rp weights5152# random portfolios optimization53test_that("random portfolios updated portfolio object",54{ expect_that(inherits(optrpv1$portfolio, "portfolio.spec"), is_true()) })5556test_that("random portfolios returns optimal weights",57{ expect_that(is.numeric(extractWeights(optrpv1)), is_true()) })5859test_that("random portfolios returns an objective measure",60{ expect_that(is.numeric(extractObjectiveMeasures(optrpv1)$mean), is_true()) })6162# DEoptim optimization63test_that("DE optim updated portfolio object",64{ expect_that(inherits(optdev1$portfolio, "portfolio.spec"), is_true()) })6566test_that("DE optim returns optimal weights",67{ expect_that(is.numeric(extractWeights(optdev1)), is_true()) })6869test_that("DE optim returns an objective measure",70{ expect_that(is.numeric(extractObjectiveMeasures(optdev1)$mean), is_true()) })7172# ROI optimization73test_that("ROI updated portfolio object",74{ expect_that(inherits(optroiv1$portfolio, "portfolio.spec"), is_true()) })7576test_that("ROI returns optimal weights equal to c(0, 0, 0.46, 0.55)",77{ expect_equal(as.numeric(extractWeights(optroiv1)), c(0, 0, 0.46, 0.55)) })7879test_that("ROI returns an objective measure mean=0.008193842",80{ expect_equal(as.numeric(extractObjectiveMeasures(optroiv1)$mean), 0.008193842) })81828384