Path: blob/master/sandbox/testing_fn_map.R
1433 views
library(PortfolioAnalytics)12data(edhec)3ret <- edhec[, 1:4]4funds <- colnames(ret)56pspec <- portfolio.spec(assets=funds)78pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=T)9pspec <- add.constraint(portfolio=pspec, type="box", min=0.05, max=0.65, enabled=T)10pspec <- add.constraint(portfolio=pspec, type="group", groups=list(1:2,3:4),11group_min=c(0.08, 0.05), group_max=c(0.55, 0.85), enabled=T)12pspec <- add.constraint(portfolio=pspec, type="turnover", turnover_target=0.4, enabled=F)13pspec <- add.constraint(portfolio=pspec, type="diversification", div_target=0.6, enabled=F)14pspec <- add.constraint(portfolio=pspec, type="position_limit", max_pos=3, enabled=T)15portfolio <- pspec161718# leverage and position_limit constraints are violated19weights <- c(0.15, 0.25, 0.4, 0.1)20sum(weights)2122fn_map(weights, portfolio)2324# box constraints are violated but postion_limit is already satisfied25# issue because min vector does not have a zero value and weights[1] = 026# all constraints are satisfied so there should be no transformation27# Is it reasonable to expect the user to have a min vector with zeros when using position_limit constraints?28# I try to catch this and modify the tmp_min vector so this does not trigger29# violation of box constraints30weights <- c(0, 0.55, 0.3, 0.15)31sum(weights)3233fn_map(weights, portfolio)3435# group and position limit constraints are violated36weights <- c(0.1, 0.65, 0.1, 0.15)37sum(weights)3839fn_map(weights, portfolio)4041# normalize weights from the equal weights seed portfolio42weights <- portfolio$assets43sum(weights)4445fn_map(weights, portfolio)4647##### relaxing box constraints #####48pspec <- portfolio.spec(assets=funds)4950pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=T)51# make min infeasible and too restrictive52pspec <- add.constraint(portfolio=pspec, type="box", min=0.3, max=0.75, enabled=T)5354# weights satisfy leverage constraints but not box constraints55weights <- c(0.15, 0.05, 0.25, 0.55)56sum(weights)5758# min constraint needs to be relaxed59# note how min has been changed60fn_map(weights, pspec, TRUE)6162##### relaxing group constraints #####63pspec <- portfolio.spec(assets=funds)6465pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=T)66pspec <- add.constraint(portfolio=pspec, type="box", min=0.05, max=0.7, enabled=T)67# Make group constraints too restrictive68pspec <- add.constraint(portfolio=pspec, type="group", groups=list(1:2, 3:4),69group_min=c(0.05, 0.01), group_max=c(0.45, 0.5), enabled=T)7071# weights satisfy leverage and box constraints, but not group72weights <- c(0.15, 0.05, 0.10, 0.7)7374# group constraints needs to be relaxed75# note how cLO and cUP have been changed76fn_map(weights, pspec, TRUE)7778##### relaxing position limits constraints #####79pspec <- portfolio.spec(assets=funds)8081pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=T)82pspec <- add.constraint(portfolio=pspec, type="box", min=0.05, max=0.4, enabled=T)83# Make position limit constraint too restrictive84pspec <- add.constraint(portfolio=pspec, type="position_limit", max_pos=2, enabled=T)8586# weights satisfy leverage and box constraints, but not group87weights <- c(0.4, 0.05, 0.15, 0.4)8889# position limit constraint needs to be relaxed90# note how max_pos has been increased to 391fn_map(weights, pspec, TRUE)9293##### relaxing leverage exposure constraint #####94pspec <- portfolio.spec(assets=funds)95pspec <- add.constraint(portfolio=pspec, type="weight_sum",96min_sum=0.99, max_sum=1.01)97pspec <- add.constraint(portfolio=pspec, type="box", min=-0.4, max=1)98pspec <- add.constraint(portfolio=pspec, type="leverage_exposure", leverage=1.6)99100# weights satisfy leverage and box constraints, but not group101weights <- c(-0.4, 0.75, 0.25, 0.4)102sum(weights)103sum(abs(weights))104105# relax leverage exposure constraint106fn_map(weights, pspec, TRUE)107108rp_transform(weights, min_sum=0.99, max_sum=1.01,109min_box=rep(-0.3, 4), max_box=rep(0.6,4),110groups=NULL, cLO=NULL, cUP=NULL,111leverage=1.5, max_permutations=10000)112113pspec <- portfolio.spec(assets=funds)114pspec <- add.constraint(portfolio=pspec, type="weight_sum",115min_sum=0.99, max_sum=1.01)116pspec <- add.constraint(portfolio=pspec, type="box", min=-0.4, max=1)117pspec <- add.constraint(portfolio=pspec, type="leverage_exposure", leverage=1.6)118rp <- random_portfolios(pspec, 5000, eliminate=FALSE)119x <- apply(rp, 1, function(x) sum(abs(x)))120plot(x)121122123