Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/applylocalsearch.R
1433 views
1
2
library("PerformanceAnalytics")
3
#library("Rdonlp2");
4
warning("this code requires package Rdonlp2, which is no longer on CRAN due to licencing issues")
5
#source("localsearch.R")
6
detach(package:fEcofin)
7
data("edhec")
8
# 1. Load return data
9
10
R=edhec[,1:11]
11
summary(R);
12
13
names.assets =colnames(edhec[,1:11]);
14
15
# 2. Load the weight vectors
16
17
weightgrid = read.csv( file = "weightingvectors_11_instr_5to50.csv" ,
18
header = FALSE, sep = ",", na.strings = "NA", dec = ".")
19
colnames(weightgrid)=colnames(edhec[,2:12])
20
21
# header is FALSE, otherwise you skip 1 weight vector
22
23
# For the 11 instrument portfolios, the portfolios are ordered in increasing concentration.
24
# The breakdown for the grid search is like this:
25
# Max Weight First Row Last Row Num of Portfolios
26
# Equal Wt 1 1 1
27
# 10% max wt 2 56 55
28
# 15% max wt 57 19856 19800
29
# 20% max wt 19857 59951 40095
30
# 25% max wt 59952 81368 21417
31
# 30% max wt 81369 89233 7865
32
# 35% max wt 89234 91543 2310
33
# 40% max wt 91544 92148 605
34
# 45% max wt 92149 92258 110
35
# 50% max wt 92259 92269 11
36
37
# Based on the distribution of these portfolios, I suggest we constrain our portfolios to be 5% to 35%.
38
# The small number of possible portfolios with 40%-50% max weights would most likely lead to unstable,
39
# overly concentrated "corner" portfolios in the optimization,
40
# while the larger numbers of portfolios available with lower weights should lead to more balanced and favorable results.
41
42
weightgrid = weightgrid[c(1:91543),] # 30\%
43
#weightgrid = weightgrid[c(1:89233),]
44
45
lowerbound = rep(0.025,11);
46
upperbound = rep(0.35,11);
47
48
# 3. Specify the estimation period
49
50
# Because we require a training sample of at least 3 years,
51
# and the data is availaible from 1997,
52
# the first year we can calculate mean/risk analytics for is the year 2000
53
54
to = seq( from= 3*12 , to = 10*12 , by=12 );
55
56
ind3yr=T
57
if(ind3yr){
58
from = to-35
59
# 4. Specify the names of the input and output files
60
names.input = c( "1999.3yr","2000.3yr","2001.3yr","2002.3yr","2003.3yr","2004.3yr","2005.3yr","2006.3yr" )
61
# 2004.3yr.csv is the input file containing for that year in column the criteria
62
names.output = c("GVaR.3yr","SR.GVaR.3yr","modVaR.3yr","SR.modVaR.3yr",
63
"GES.3yr","SR.GES.3yr","modES.3yr","SR.modES.3yr","StdDev.3yr","SR.StdDev.3yr")
64
}else{
65
from=rep(1,8)
66
# 4. Specify the names of the input and output files
67
names.input = c( "1999","2000","2001","2002","2003","2004","2005","2006" )
68
# 2004.csv is the input file containing for that year in column the criteria
69
names.output = c("GVaR.inception","SR.GVaR.inception","modVaR.inception","SR.modVaR.inception",
70
"GES.inception","SR.GES.inception","modES.inception","SR.modES.inception","StdDev.inception","SR.StdDev.inception")
71
# mVaR_inception.csv will be the output file containing for that criterion, for each year the optimal weights
72
}
73
74
75
# 5. Specify the optimization criteria and in which column they are
76
# Available criteria =c( "StdDev" , "SR.StdDev" ,"GVaR", "SR.GVaR", "mVaR", "SR.mVaR", "GES", "SR.GES", "mES", "SR.mES", ... )
77
78
criteria = c( "GVaR", "SR.GVaR", "mVaR", "SR.mVaR", "GES", "SR.GES", "mES", "SR.mES" , "StdDev" , "SR.StdDev")
79
80
columns.crit = c(1:10);
81
82
# output = read.csv( file = paste(names.input[1],".csv",sep=""), header = TRUE, sep = ",", na.strings = "NA", dec = ".")
83
# summary(output)
84
# summary(output[columns.crit])
85
86
87
88
89
# 6. Optimization: number of starting values to try
90
91
cMin = 10; # at least 2
92
93
localsearch(R=R, weightgrid=weightgrid, from=from, to=to, names.input=names.input, names.output=names.output, names.assets = names.assets,
94
cMin=cMin, criteria=criteria, columns.crit=columns.crit, p=0.95, lowerbound = lowerbound, upperbound = upperbound, EW=F)
95
96
97
98
# create equalweighted
99
cAssets = 11; cYears=length(names.input)
100
output = as.data.frame( matrix( rep(1/cAssets, cAssets*cYears) , nrow=cYears) );
101
rownames(output) = names.input;
102
colnames(output) = names.assets;
103
write.table( output , file = "equalweighted.csv", append = FALSE, quote = TRUE, sep = ",", eol = "\n", na = "NA", dec = ".", row.names = TRUE,
104
col.names = TRUE, qmethod = "escape")
105
106
###############################################################################
107
# R (http://r-project.org/) Numeric Methods for Optimization of Portfolios
108
#
109
# Copyright (c) 2004-2014 Kris Boudt, Peter Carl and Brian G. Peterson
110
#
111
# This library is distributed under the terms of the GNU Public License (GPL)
112
# for full details see the file COPYING
113
#
114
# $Id$
115
#
116
###############################################################################
117
# $Log: not supported by cvs2svn $
118
# Revision 1.9 2009-09-22 21:24:14 peter
119
# - applied cvs log and licensing details
120
#
121
###############################################################################
122
123