Path: blob/master/sandbox/riskbudgetpaper(superseded)/R_Allocation/main_riskbudgets.R
1433 views
1# ! Set your working directory (folder containing the subfolders R_allocation, R_interpretation, data, weights, etc)23setwd("c:/Documents and Settings/Administrator/Desktop/risk budget programs")4# setwd("c:/Documents and Settings/n06054/Desktop/risk budget programs")56# Options:78# Length estimation period9estyears = 81011# Equal risk portfolio12cAssets = 4;13p = priskbudget = 0.95;1415mincriterion = "mES" ; percriskcontribcriterion = "mES";1617# Define your portfolio allocation strategy18# names = c( "EqualRisk" , "EqualWeight" , "MinRisk" , "MinRiskConc" ,19# "MinRisk_PositionLimit" , "MinRisk_RiskLimit" , "MinRisk_ReturnTarget",20# "MinRiskConc_PositionLimit" , "MinRiskConc_RiskLimit" , "MinRiskConc_ReturnTarget")21strategy = "MinRiskConc_PositionLimit" # "MinRiskConc_PositionLimit" , "MinRiskConc_RiskLimit"2223# Load programs2425source("R_Allocation/Risk_budget_functions.R");26library(zoo); library(fGarch); library("PerformanceAnalytics");2728# Load the data2930nominalreturns = T;31newdata = T;32firstyear = 1976 ; firstquarter = 1; lastyear = 2010; lastquarter = 2;3334if(newdata){35data = read.table( file= paste("data/","data.txt",sep="") ,header=T)36# "Bond" "SP500" "EAFE" "SPGSCI" "TBill" "Inflation"37date = as.Date(data[,1],format="%Y-%m-%d")38data = zoo( data[,2:ncol(data)] , order.by = date )39# "Bond" "SP500" "EAFE" "SPGSCI" "TBill" "Inflation"40cAssets = ncol(data)-2; # number of risky assets41# The loaded data has monthly frequency42if(!nominalreturns){ monthlyR = data[,(1:(cAssets))]-data[,cAssets+2] }else{ monthlyR = data[,1:cAssets] }43plot(monthlyR)44if(nominalreturns){ save(monthlyR,file="monthlyR.RData") }else{ save(monthlyR,file="monthlyR_real.RData") }45}else{46if(nominalreturns){ load(file="monthlyR.RData") }else{ load(file="monthlyR_real.RData") }47}4849# Define rebalancing periods:5051ep = endpoints(monthlyR,on='quarters')52# select those for estimation period53ep.start = ep[1:(length(ep)-estyears*4)]+154from = time(monthlyR)[ep.start]55from = seq( as.Date(paste(firstyear,"-01-01",sep="")), as.Date(paste(lastyear-estyears,"-07-01",sep="")), by="3 month")56ep.end = ep[(1+estyears*4):length(ep)]57to = time(monthlyR)[ep.end]58nsamples = length(from);5960#names of quarters for which the forecast is made:61names.input = paste( c("Q1y_","Q2y_","Q3y_","Q4y_") , rep(seq( (firstyear+estyears),lastyear-1,1),each=4) , sep="" );62names.input = c( names.input , paste( c("Q1y_","Q2y_","Q3y_") , rep(lastyear,each=3) , sep="" ) );6364# Construction of rebalanced portfolios:65# TRY on subset: from = from[1:2] ; to = to[1:2]; names.input = names.input[1:2]66out = findportfolio.dynamic( R=monthlyR, from=from, to=to, names.input=names.input, names.assets = colnames(monthlyR) ,67p = p , priskbudget = priskbudget , mincriterion = mincriterion ,68percriskcontribcriterion = percriskcontribcriterion ,69strategy , optimize_method = "DEoptim+L-BFGS-B" )7071write.table( out[[1]] , file = paste("weights/",strategy,".csv",sep=""),72append = FALSE, quote = TRUE, sep = ",", eol = "\n", na = "NA", dec = ".", row.names = TRUE,col.names = TRUE, qmethod = "escape")7374write.table( out[[2]] , file = paste("riskcont/",strategy,".csv",sep=""),75append = FALSE, quote = TRUE, sep = ",", eol = "\n", na = "NA", dec = ".", row.names = TRUE,col.names = TRUE, qmethod = "escape")7677write.table( out[[3]] , file = paste("riskreturn/",strategy,".csv",sep=""),78append = FALSE, quote = TRUE, sep = ",", eol = "\n", na = "NA", dec = ".", row.names = TRUE,col.names = TRUE, qmethod = "escape")79808182