Path: blob/master/sandbox/leverage_transformation_testing.R
1433 views
1library(PortfolioAnalytics)23# Use random_portfolios to generate weights that do not meet the full4# investment constraint where the sum of the weights range from 0.8 to 1.256# Note: slow using random portfolios78sum_seq <- seq(from=0.8, to=1.5, by=0.1)910##### Random Portfolios: 50 assets 5,000 portfolios11nassets <- 5012npermutations <- 50013min <- rep(0, nassets)14# random_index <- sample(1:nassets, 5)15# min[random_index] <- 0.0116max <- rep(0.5, nassets)17rp <- list()18for(i in 1:10){19min_sum <- sample(sum_seq, 1)20max_sum <- min_sum + 0.012122cset <- constraint(assets=nassets, min=min, max=max,23min_sum=min_sum, max_sum=max_sum,24weight_seq=generatesequence(min=0, max=0.5, by=0.005))2526rp[[i]] <- random_portfolios(rpconstraints=cset, permutations=npermutations)27}2829rp <- do.call(rbind, rp)3031# transform the entire vector to meet leverage constraints32tmp_rp <- t(apply(rp, 1, txfrm_weight_sum_constraint, min_sum=0.99, max_sum=1.01))3334# percentage of portfolios that satisfy box constraints after the simple transformation35sum(apply(tmp_rp, 1, function(x) all(x >= min & x <= max))) / (nrow(tmp_rp)) * 1003637# only works if I relax min and38new_rp <- t(apply(tmp_rp, 1, rp_transform, min=rep(-0.05, nassets), max=rep(0.5, nassets),39groups=NULL, cLO=NULL, cUP=NULL,40max_permutations=500))4142##### Random Portfolios: 250 assets 5,000 portfolios43nassets <- 25044npermutations <- 50045min <- rep(0, nassets)46random_index <- sample(1:nassets, 10)47min[random_index] <- 0.0148max <- rep(0.5, nassets)49rp <- list()50for(i in 1:10){51min_sum <- sample(sum_seq, 1)52max_sum <- min_sum + 0.015354cset <- constraint(assets=nassets, min=min, max=max,55min_sum=min_sum, max_sum=max_sum,56weight_seq=generatesequence(min=0, max=0.5, by=0.005))5758rp[[i]] <- random_portfolios(rpconstraints=cset, permutations=npermutations)59}6061rp <- do.call(rbind, rp)6263# transform the entire vector to meet leverage constraints64tmp_rp <- t(apply(rp, 1, txfrm_weight_sum_constraint, min_sum=0.99, max_sum=1.01))6566# percentage of portfolios that satisfy box constraints after the simple transformation67sum(apply(tmp_rp, 1, function(x) all(x >= min & x <= max))) / (nrow(tmp_rp)) * 1006869new_rp <- t(apply(tmp_rp, 1, rp_transform, min=min, max=max, groups=NULL, cLO=NULL, cUP=NULL))707172# generate portfolios of uniform random numbers that satisfy box constraints,73# but will violate leverage constraints74N <- 50075k <- 1000076min <- -0.0177max <- 0.157879set.seed(123)80tmp <- runif(N*k, min, max)81tmp_mat <- matrix(tmp, nrow=k)8283summary(rowSums(tmp_mat))8485# transform the entire vector to meet leverage constraints86tmp_rp <- t(apply(tmp_mat, 1, txfrm_weight_sum_constraint, min_sum=0.99, max_sum=1.01))8788min <- c(rep(-0.01, 200), 0.01, rep(-0.01, 299))89max <- rep(0.15, 500)90# percentage of portfolios that satisfy box constraints after the simple transformation91sum(apply(tmp_rp, 1, function(x) all(x >= min & x <= max))) / (nrow(tmp_rp)) * 1009293# All portfolios seem to satisfy box constraints if min is a vector of all 0s or94# all elements are less than 0 and the sum of the weights is greater than 19596# If elements of the min vector are positive, then 0 portfolios satisfy constraints9798# very sensitive to box constraint parameters and sum of the weights99100101102