# Commands that generated the results of Section 2.2 of # "The Portfolio Probe User's Manual" # # These commands demonstrate testing the effect of constraint bounds # using random portfolios # # This depends on functions listed in 'pprobe_functions01.R' # The data are created in 'pprobe_R_data01.txt' # # The functions that create the graphs are in 'pprobe_graphFun01.R' # If you want hardcopies of the graphs, then you need to start a # graphics device (like 'pdf') and do 'dev.off()' after the graphs # are created. # things to CHANGE, possibly PP_LOC <- ".." # running this script to completion requires that # 'pprobe_R_varCache.txt' has been done # So test for that: cat("Testing to see if variance matrices are in place\n", " if you don't get an error from 'attach', it passes\n") attach(paste(PP_LOC, "/Varshr/varshr.2008-10-14.rda", sep="")) detach() # initial setup if(!exists("us.pricemat")) attach(paste(PP_LOC, "us.pricemat.rda", sep="/")) if(!exists("us.macdmat")) attach(paste(PP_LOC, "us.macdmat.rda", sep="/")) # specify quarter end locations quarter.ends <- c(251, 312, 375, 438, 502, 563, 627, 691, 755) names(quarter.ends) <- paste(rep(2006:2008, c(1,4,4)), 'Q', c(4,1:3), sep="") # create a price vector price.20061229 <- us.pricemat[quarter.ends[1], ] # create variance matrices varshr.Q <- vector("list", 8) names(varshr.Q) <- names(quarter.ends)[1:8] # calculate return matrix from prices: us.retmat <- diff(log(us.pricemat)) require(BurStFin) for(i in 1:8) { varshr.Q[[i]] <- var.shrink.eqcor(us.retmat[seq(to=quarter.ends[i]-1, length=250),]) } # create random portfolios without volatility constraint rp30w10 <- random.portfolio(number.rand = 10000, prices = price.20061229, gross.value = 1e+06, long.only = TRUE, port.size = 30, max.weight = 0.1) # create utility over 2007 for fixed portfolios rp30w10.mvu2.2007 <- pp.meanvarutil(valuation(rp30w10, us.pricemat[(quarter.ends[1]+1):quarter.ends[5],], collapse=TRUE), 2) # create random portfolios with volatility constraint rp30w10v08 <- random.portfolio(number.rand = 10000, prices = price.20061229, variance=varshr.Q[[1]], gross.value = 1e+06, long.only = TRUE, port.size = 30, max.weight = 0.1, var.constraint=.08^2/252) # create utility for portfolios with volatility constraint rp30w10v08.mvu2.2007 <- pp.meanvarutil(valuation(rp30w10v08, us.pricemat[(quarter.ends[1]+1):quarter.ends[5],], collapse=TRUE), 2) # do graph P.mvu2y2007(NULL) # This is where the series of cached variances is needed # see 'pprobe_R_varCache.txt' # generate rolling random portfolios and calculate utility # -- constant constraints rp30w10.mvu2.dat <- pp.date.meanvarutil(rownames(us.pricemat)[251:695], 60, 2, us.pricemat, us.macdmat, max.weight=.1, port.size=30, gross.value=1e6, long.only=TRUE, vardir=paste(PP_LOC, "Varshr", sep="/")) # find approximate minimum variance each day # we don't care about exact minimum, so restrict computation to 3 iterations rp30w10.minvar.dat <- pp.date.minvar(rownames(us.pricemat)[251:695], us.pricemat, max.weight=.1, port.size=30, gross.value=1e6, long.only=TRUE, iteration=3, vardir=paste(PP_LOC, "Varshr", sep="/")) # do graph P.minvol(NULL) # generate rolling random portfolios and calculate utility # -- varialbe volatility constraint rp30w10vv20.mvu2.dat <- pp.date.var.meanvarutil( rownames(us.pricemat)[251:695], 1.44 * rp30w10.minvar.dat, 60, 2, us.pricemat, us.macdmat, max.weight=.1, port.size=30, gross.value=1e6, long.only=TRUE, throw.error=FALSE, vardir=paste(PP_LOC, "Varshr", sep="/")) # do graph P.mvu2dat(NULL)