Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/riskbudgetpaper(superseded)/R_Allocation/main_riskbudgets.R
1433 views
1
2
# ! Set your working directory (folder containing the subfolders R_allocation, R_interpretation, data, weights, etc)
3
4
setwd("c:/Documents and Settings/Administrator/Desktop/risk budget programs")
5
# setwd("c:/Documents and Settings/n06054/Desktop/risk budget programs")
6
7
# Options:
8
9
# Length estimation period
10
estyears = 8
11
12
# Equal risk portfolio
13
cAssets = 4;
14
p = priskbudget = 0.95;
15
16
mincriterion = "mES" ; percriskcontribcriterion = "mES";
17
18
# Define your portfolio allocation strategy
19
# names = c( "EqualRisk" , "EqualWeight" , "MinRisk" , "MinRiskConc" ,
20
# "MinRisk_PositionLimit" , "MinRisk_RiskLimit" , "MinRisk_ReturnTarget",
21
# "MinRiskConc_PositionLimit" , "MinRiskConc_RiskLimit" , "MinRiskConc_ReturnTarget")
22
strategy = "MinRiskConc_PositionLimit" # "MinRiskConc_PositionLimit" , "MinRiskConc_RiskLimit"
23
24
# Load programs
25
26
source("R_Allocation/Risk_budget_functions.R");
27
library(zoo); library(fGarch); library("PerformanceAnalytics");
28
29
# Load the data
30
31
nominalreturns = T;
32
newdata = T;
33
firstyear = 1976 ; firstquarter = 1; lastyear = 2010; lastquarter = 2;
34
35
if(newdata){
36
data = read.table( file= paste("data/","data.txt",sep="") ,header=T)
37
# "Bond" "SP500" "EAFE" "SPGSCI" "TBill" "Inflation"
38
date = as.Date(data[,1],format="%Y-%m-%d")
39
data = zoo( data[,2:ncol(data)] , order.by = date )
40
# "Bond" "SP500" "EAFE" "SPGSCI" "TBill" "Inflation"
41
cAssets = ncol(data)-2; # number of risky assets
42
# The loaded data has monthly frequency
43
if(!nominalreturns){ monthlyR = data[,(1:(cAssets))]-data[,cAssets+2] }else{ monthlyR = data[,1:cAssets] }
44
plot(monthlyR)
45
if(nominalreturns){ save(monthlyR,file="monthlyR.RData") }else{ save(monthlyR,file="monthlyR_real.RData") }
46
}else{
47
if(nominalreturns){ load(file="monthlyR.RData") }else{ load(file="monthlyR_real.RData") }
48
}
49
50
# Define rebalancing periods:
51
52
ep = endpoints(monthlyR,on='quarters')
53
# select those for estimation period
54
ep.start = ep[1:(length(ep)-estyears*4)]+1
55
from = time(monthlyR)[ep.start]
56
from = seq( as.Date(paste(firstyear,"-01-01",sep="")), as.Date(paste(lastyear-estyears,"-07-01",sep="")), by="3 month")
57
ep.end = ep[(1+estyears*4):length(ep)]
58
to = time(monthlyR)[ep.end]
59
nsamples = length(from);
60
61
#names of quarters for which the forecast is made:
62
names.input = paste( c("Q1y_","Q2y_","Q3y_","Q4y_") , rep(seq( (firstyear+estyears),lastyear-1,1),each=4) , sep="" );
63
names.input = c( names.input , paste( c("Q1y_","Q2y_","Q3y_") , rep(lastyear,each=3) , sep="" ) );
64
65
# Construction of rebalanced portfolios:
66
# TRY on subset: from = from[1:2] ; to = to[1:2]; names.input = names.input[1:2]
67
out = findportfolio.dynamic( R=monthlyR, from=from, to=to, names.input=names.input, names.assets = colnames(monthlyR) ,
68
p = p , priskbudget = priskbudget , mincriterion = mincriterion ,
69
percriskcontribcriterion = percriskcontribcriterion ,
70
strategy , optimize_method = "DEoptim+L-BFGS-B" )
71
72
write.table( out[[1]] , file = paste("weights/",strategy,".csv",sep=""),
73
append = FALSE, quote = TRUE, sep = ",", eol = "\n", na = "NA", dec = ".", row.names = TRUE,col.names = TRUE, qmethod = "escape")
74
75
write.table( out[[2]] , file = paste("riskcont/",strategy,".csv",sep=""),
76
append = FALSE, quote = TRUE, sep = ",", eol = "\n", na = "NA", dec = ".", row.names = TRUE,col.names = TRUE, qmethod = "escape")
77
78
write.table( out[[3]] , file = paste("riskreturn/",strategy,".csv",sep=""),
79
append = FALSE, quote = TRUE, sep = ",", eol = "\n", na = "NA", dec = ".", row.names = TRUE,col.names = TRUE, qmethod = "escape")
80
81
82