Path: blob/master/sandbox/applylocalsearch.R
1433 views
1library("PerformanceAnalytics")2#library("Rdonlp2");3warning("this code requires package Rdonlp2, which is no longer on CRAN due to licencing issues")4#source("localsearch.R")5detach(package:fEcofin)6data("edhec")7# 1. Load return data89R=edhec[,1:11]10summary(R);1112names.assets =colnames(edhec[,1:11]);1314# 2. Load the weight vectors1516weightgrid = read.csv( file = "weightingvectors_11_instr_5to50.csv" ,17header = FALSE, sep = ",", na.strings = "NA", dec = ".")18colnames(weightgrid)=colnames(edhec[,2:12])1920# header is FALSE, otherwise you skip 1 weight vector2122# For the 11 instrument portfolios, the portfolios are ordered in increasing concentration.23# The breakdown for the grid search is like this:24# Max Weight First Row Last Row Num of Portfolios25# Equal Wt 1 1 126# 10% max wt 2 56 5527# 15% max wt 57 19856 1980028# 20% max wt 19857 59951 4009529# 25% max wt 59952 81368 2141730# 30% max wt 81369 89233 786531# 35% max wt 89234 91543 231032# 40% max wt 91544 92148 60533# 45% max wt 92149 92258 11034# 50% max wt 92259 92269 113536# Based on the distribution of these portfolios, I suggest we constrain our portfolios to be 5% to 35%.37# The small number of possible portfolios with 40%-50% max weights would most likely lead to unstable,38# overly concentrated "corner" portfolios in the optimization,39# while the larger numbers of portfolios available with lower weights should lead to more balanced and favorable results.4041weightgrid = weightgrid[c(1:91543),] # 30\%42#weightgrid = weightgrid[c(1:89233),]4344lowerbound = rep(0.025,11);45upperbound = rep(0.35,11);4647# 3. Specify the estimation period4849# Because we require a training sample of at least 3 years,50# and the data is availaible from 1997,51# the first year we can calculate mean/risk analytics for is the year 20005253to = seq( from= 3*12 , to = 10*12 , by=12 );5455ind3yr=T56if(ind3yr){57from = to-3558# 4. Specify the names of the input and output files59names.input = c( "1999.3yr","2000.3yr","2001.3yr","2002.3yr","2003.3yr","2004.3yr","2005.3yr","2006.3yr" )60# 2004.3yr.csv is the input file containing for that year in column the criteria61names.output = c("GVaR.3yr","SR.GVaR.3yr","modVaR.3yr","SR.modVaR.3yr",62"GES.3yr","SR.GES.3yr","modES.3yr","SR.modES.3yr","StdDev.3yr","SR.StdDev.3yr")63}else{64from=rep(1,8)65# 4. Specify the names of the input and output files66names.input = c( "1999","2000","2001","2002","2003","2004","2005","2006" )67# 2004.csv is the input file containing for that year in column the criteria68names.output = c("GVaR.inception","SR.GVaR.inception","modVaR.inception","SR.modVaR.inception",69"GES.inception","SR.GES.inception","modES.inception","SR.modES.inception","StdDev.inception","SR.StdDev.inception")70# mVaR_inception.csv will be the output file containing for that criterion, for each year the optimal weights71}727374# 5. Specify the optimization criteria and in which column they are75# Available criteria =c( "StdDev" , "SR.StdDev" ,"GVaR", "SR.GVaR", "mVaR", "SR.mVaR", "GES", "SR.GES", "mES", "SR.mES", ... )7677criteria = c( "GVaR", "SR.GVaR", "mVaR", "SR.mVaR", "GES", "SR.GES", "mES", "SR.mES" , "StdDev" , "SR.StdDev")7879columns.crit = c(1:10);8081# output = read.csv( file = paste(names.input[1],".csv",sep=""), header = TRUE, sep = ",", na.strings = "NA", dec = ".")82# summary(output)83# summary(output[columns.crit])8485868788# 6. Optimization: number of starting values to try8990cMin = 10; # at least 29192localsearch(R=R, weightgrid=weightgrid, from=from, to=to, names.input=names.input, names.output=names.output, names.assets = names.assets,93cMin=cMin, criteria=criteria, columns.crit=columns.crit, p=0.95, lowerbound = lowerbound, upperbound = upperbound, EW=F)94959697# create equalweighted98cAssets = 11; cYears=length(names.input)99output = as.data.frame( matrix( rep(1/cAssets, cAssets*cYears) , nrow=cYears) );100rownames(output) = names.input;101colnames(output) = names.assets;102write.table( output , file = "equalweighted.csv", append = FALSE, quote = TRUE, sep = ",", eol = "\n", na = "NA", dec = ".", row.names = TRUE,103col.names = TRUE, qmethod = "escape")104105###############################################################################106# R (http://r-project.org/) Numeric Methods for Optimization of Portfolios107#108# Copyright (c) 2004-2014 Kris Boudt, Peter Carl and Brian G. Peterson109#110# This library is distributed under the terms of the GNU Public License (GPL)111# for full details see the file COPYING112#113# $Id$114#115###############################################################################116# $Log: not supported by cvs2svn $117# Revision 1.9 2009-09-22 21:24:14 peter118# - applied cvs log and licensing details119#120###############################################################################121122123