Path: blob/master/inst/tests/test_roi_gmv_toc.R
1433 views
1library(testthat)2library(ROI)3library(ROI.plugin.quadprog)4library(quadprog)5library(corpcor)6library(PerformanceAnalytics)78data(edhec)9R <- edhec[, 1:5]10m <- ncol(R)1112constraints <- list()13constraints$min_sum <- 114constraints$max_sum <- 115constraints$min <- rep(0, m)16constraints$max <- rep(1, m)17constraints$turnover_target <- 51819moments <- list()20moments$mean <- colMeans(R)2122lambda <- 123target <- NA2425# Modify the returns matrix. This is done because there are 3 sets of26# variables 1) w.initial, 2) w.buy, and 3) w.sell27R0 <- matrix(0, ncol=ncol(R), nrow=nrow(R))28returns <- cbind(R, R0, R0)29V <- cov(returns)3031# number of assets32N <- ncol(R)3334# initial weights for solver35init_weights <- rep(1/ N, N)3637# check for a target return constraint38if(!is.na(target)) {39# If var is the only objective specified, then moments$mean won't be calculated40if(all(moments$mean==0)){41tmp_means <- colMeans(R)42} else {43tmp_means <- moments$mean44}45} else {46tmp_means <- rep(0, N)47target <- 048}49Amat <- c(tmp_means, rep(0, 2*N))50dir <- "=="51rhs <- target52meq <- N + 15354# Amat for initial weights55# Amat <- cbind(diag(N), matrix(0, nrow=N, ncol=N*2))56Amat <- rbind(Amat, cbind(diag(N), -1*diag(N), diag(N)))57rhs <- c(rhs, init_weights)58dir <- c(dir, rep("==", N))5960# Amat for turnover constraints61Amat <- rbind(Amat, c(rep(0, N), rep(-1, N), rep(-1, N)))62rhs <- c(rhs, -constraints$turnover_target)63dir <- c(dir, ">=")6465# Amat for positive weights66Amat <- rbind(Amat, cbind(matrix(0, nrow=N, ncol=N), diag(N), matrix(0, nrow=N, ncol=N)))67rhs <- c(rhs, rep(0, N))68dir <- c(dir, rep(">=", N))6970# Amat for negative weights71Amat <- rbind(Amat, cbind(matrix(0, nrow=N, ncol=2*N), diag(N)))72rhs <- c(rhs, rep(0, N))73dir <- c(dir, rep(">=", N))7475# Amat for full investment constraint76Amat <- rbind(Amat, rbind(c(rep(1, N), rep(0,2*N)), c(rep(-1, N), rep(0,2*N))))77rhs <- c(rhs, constraints$min_sum, -constraints$max_sum)78dir <- c(dir, ">=", ">=")7980# Amat for lower box constraints81Amat <- rbind(Amat, cbind(diag(N), diag(0, N), diag(0, N)))82rhs <- c(rhs, constraints$min)83dir <- c(dir, rep(">=", N))8485# Amat for upper box constraints86Amat <- rbind(Amat, cbind(-diag(N), diag(0, N), diag(0, N)))87rhs <- c(rhs, -constraints$max)88dir <- c(dir, rep(">=", N))8990d <- rep(tmp_means, 3)9192Amat <- Amat[!is.infinite(rhs), ]93rhs <- rhs[!is.infinite(rhs)]9495result <- solve.QP(Dmat=make.positive.definite(2*lambda*V),96dvec=d, Amat=t(Amat), bvec=rhs, meq=meq)97result98wts <- result$solution99wts.final <- wts[(1:N)]100101##### ROI #####102ROI_objective <- Q_objective(Q=make.positive.definite(2*lambda*V),103L=rep(-tmp_means, 3))104105opt.prob <- OP(objective=ROI_objective,106constraints=L_constraint(L=Amat, dir=dir, rhs=rhs))107108roi.result <- ROI_solve(x=opt.prob, solver="quadprog")109print.default(roi.result)110weights <- result$solution[(1:N)]111112context("Test solve.QP and ROI_solve for gmv with turnover constraint")113114test_that("Objective values are equal", {115expect_equal(roi.result$objval, result$value)116})117118test_that("Solutions (optimal weights) are equal", {119expect_equal(roi.result$solution[1:m], result$solution[1:m])120})121122123124