Path: blob/master/demo/demo_random_portfolios.R
1433 views
# Demonstrate examples from script.workshop2012.R using the v2 specification12# The following optimization problems will be run3# mean-StdDev4# - maximize mean-to-volatility (i.e. reward-to-risk)5# - BUOY 16# minmETL7# - minimize modified Expected Tail Loss8# - BUOY 49# eqmETL10# - equal risk modified Expected Tail Loss11# - BUOY 61213# Implement BUOY 1, BUOY 4, and BUOY 6 from script.workshop2012.R1415# Note: The script.workshop2012.R examples use pamean and pasd, I will simply16# use mean and StdDev.1718# Include optimizer and multi-core packages19library(PortfolioAnalytics)20require(quantmod)21require(foreach)2223# The multicore package, and therefore registerDoMC, should not be used in a24# GUI environment, because multiple processes then share the same GUI. Only use25# when running from the command line26# require(doMC)27# registerDoMC(3)2829data(edhec)3031# Drop some indexes and reorder32edhec.R = edhec[,c("Convertible Arbitrage", "Equity Market Neutral","Fixed Income Arbitrage", "Event Driven", "CTA Global", "Global Macro", "Long/Short Equity")]3334# Define pamean function35# pamean <- function(n=12, R, weights, geometric=TRUE){36# as.vector(sum(Return.annualized(last(R,n), geometric=geometric)*weights))37# }3839# Define pasd function40# pasd <- function(R, weights){41# as.numeric(StdDev(R=R, weights=weights)*sqrt(12)) # hardcoded for monthly data42# }4344# Set some parameters45rebalance_period = 'quarters' # uses endpoints identifiers from xts46clean = "none" #"boudt"47permutations = 40004849##### v2: Initial Portfolio Object #####50## Set up an initial portfolio object with constraints and objectives using51## v2 specification5253# Create initial portfolio object used to initialize ALL the bouy portfolios54init.portf <- portfolio.spec(assets=colnames(edhec.R),55weight_seq=generatesequence(by=0.005))56# Add leverage constraint57init.portf <- add.constraint(portfolio=init.portf,58type="leverage",59min_sum=0.99,60max_sum=1.01)61# Add box constraint62init.portf <- add.constraint(portfolio=init.portf,63type="box",64min=0.05,65max=0.3)6667#Add measure 1, annualized return68init.portf <- add.objective(portfolio=init.portf,69type="return", # the kind of objective this is70name="mean", # name of the function71enabled=TRUE, # enable or disable the objective72multiplier=0 # calculate it but don't use it in the objective73)7475# Add measure 2, annualized standard deviation76init.portf <- add.objective(portfolio=init.portf,77type="risk", # the kind of objective this is78name="StdDev", # to minimize from the sample79enabled=TRUE, # enable or disable the objective80multiplier=0 # calculate it but don't use it in the objective81)8283# Add measure 3, ES with p=(1-1/12)84# set confidence for ES85p=1-1/12 # for monthly8687init.portf <- add.objective(portfolio=init.portf,88type="risk", # the kind of objective this is89name="ES", # the function to minimize90enabled=FALSE, # enable or disable the objective91multiplier=0, # calculate it but don't use it in the objective92arguments=list(p=p)93)94print(init.portf)95summary(init.portf)9697##### v2: BUOY 1 #####98### Construct BUOY 1: Constrained Mean-StdDev Portfolio99MeanSD.portf <- init.portf100# Turn back on the return and sd objectives101MeanSD.portf$objectives[[1]]$multiplier = -1 # pamean102MeanSD.portf$objectives[[2]]$multiplier = 1 # pasd103print(MeanSD.portf)104summary(MeanSD.portf)105106107##### v2: BUOY 4 #####108### Construct BUOY 4: Constrained Minimum mETL Portfolio109MinmETL.portf <- init.portf110# Turn back on the mETL objective111MinmETL.portf$objectives[[3]]$multiplier = 1 # mETL112MinmETL.portf$objectives[[3]]$enabled = TRUE # mETL113print(MinmETL.portf)114summary(MinmETL.portf)115116##### v2: BUOY 6 #####117### Construct BUOY 6: Constrained Equal mETL Contribution Portfolio118EqmETL.portf <- add.objective(init.portf,119type="risk_budget",120name="ES",121enabled=TRUE,122min_concentration=TRUE,123arguments = list(p=(1-1/12), clean=clean)124)125EqmETL.portf$objectives[[3]]$multiplier = 1 # min mETL126EqmETL.portf$objectives[[3]]$enabled = TRUE # min mETL127print(EqmETL.portf)128summary(EqmETL.portf)129130### Choose our 'R' variable131R=edhec.R # for monthlies132133# Generate a single set of random portfolios to evaluate against all constraint set134print(paste('constructing random portfolios at',Sys.time()))135rp = random_portfolios(portfolio=init.portf, permutations=permutations)136print(paste('done constructing random portfolios at',Sys.time()))137138start_time<-Sys.time()139print(paste('Starting optimization at',Sys.time()))140141##### v2: Evaluate BUOY 1 #####142MeanSD.RND <- optimize.portfolio(R=R,143portfolio=MeanSD.portf,144optimize_method="random",145trace=TRUE,146rp=rp)147print(MeanSD.RND)148print(MeanSD.RND$elapsed_time)149150# Evaluate the objectives with RP through time151# MeanSD.RND.t <- optimize.portfolio.rebalancing(R=R,152# portfolio=MeanSD.portf,153# optimize_method="random",154# trace=TRUE,155# rp=rp,156# rebalance_on=rebalance_period,157# training_period=36)158# MeanSD.w = extractWeights.rebal(MeanSD.RND.t)159# MeanSD=Return.rebalancing(edhec.R, MeanSD.w)160# colnames(MeanSD) = "MeanSD"161162print(paste('Completed meanSD optimization at',Sys.time(),'moving on to MinmETL'))163164##### v2: Evaluate BUOY 4 #####165MinmETL.RND <- optimize.portfolio(R=R,166portfolio=MinmETL.portf,167optimize_method="random",168trace=TRUE,169rp=rp)170print(MinmETL.RND)171print(MinmETL.RND$elapsed_time)172173# Evaluate the objectives with RP through time174# MinmETL.RND.t <- optimize.portfolio.rebalancing(R=R,175# portfolio=MinmETL.portf,176# optimize_method="random",177# trace=TRUE,178# rp=rp,179# rebalance_on=rebalance_period,180# training_period=36)181# MinmETL.w = extractWeights.rebal(MinmETL.RND.t)182# MinmETL=Return.rebalancing(edhec.R, MinmETL.w)183# colnames(MinmETL) = "MinmETL"184185print(paste('Completed MinmETL optimization at',Sys.time(),'moving on to EqmETL'))186187##### v2: Evaluate BUOY 6 #####188EqmETL.RND <- optimize.portfolio(R=R,189portfolio=EqmETL.portf,190optimize_method="random",191trace=TRUE,192rp=rp)193print(EqmETL.RND)194print(EqmETL.RND$elapsed_time)195196# Evaluate the objectives with RP through time197# EqmETL.RND.t <- optimize.portfolio.rebalancing(R=R,198# portfolio=EqmETL.portf,199# optimize_method="random",200# trace=TRUE,201# rp=rp,202# rebalance_on=rebalance_period,203# training_period=36)204# EqmETL.w = extractWeights.rebal(EqmETL.RND.t)205# EqmETL=Return.rebalancing(edhec.R, EqmETL.w)206# colnames(EqmETL) = "EqmETL"207208end_time<-Sys.time()209print("Optimization Complete")210print(end_time-start_time)211212213