Path: blob/master/sandbox/paper_analysis/insample/bondequity.R
1433 views
setwd("c:/Documents and Settings/Administrator/Desktop/risk budget programs/insample")12# Equal risk portfolio3cAssets = 4; # number of assets influences the cleaning4p = priskbudget = 0.95;56mincriterion = "mES" ; percriskcontribcriterion = "mES";78# Load programs910source("Risk_budget_functions.R");11library(zoo); library(fGarch); library("PerformanceAnalytics"); library("PortfolioAnalytics")12clean = TRUE; CC = T1314# Load the data15firstyear = 1976 ; firstquarter = 1; lastyear = 2010; lastquarter = 2;16data = read.table( file= paste(getwd(),"/data.txt",sep="") ,header=T)17date = as.Date(data[,1],format="%Y-%m-%d")1819monthlyR = zoo( data[,2:(1+cAssets)] , order.by = date )2021set.seed(1234)22if(clean){ monthlyR = clean.boudt2(monthlyR,alpha=0.05)[[1]] }23monthlyR = monthlyR[,1:2]24mu = apply(monthlyR,2,'mean')25sigma = cov(monthlyR)26# N = 2 : no need for CC27M3 = PerformanceAnalytics:::M3.MM(monthlyR-matrix( rep(as.numeric(mu),nrow(monthlyR)) , nrow=nrow(monthlyR) , byrow=TRUE) );28M4 = PerformanceAnalytics:::M4.MM(monthlyR-matrix( rep(as.numeric(mu),nrow(monthlyR)) , nrow=nrow(monthlyR) , byrow=TRUE) )2930N = ncol(monthlyR)3132# Summary stats individual assets3334apply(monthlyR,2,'mean')*1235apply(monthlyR,2,'sd')*sqrt(12)36apply(monthlyR,2,'skewness')37apply(monthlyR,2,'kurtosis')38394041mESfun2 = function( w ){ return( operPortMES(w,mu=mu,alpha=0.05,sigma=sigma,M3=M3,M4=M4)[[1]] ) }42assetCVaR = rep(0,2);43for( i in 1:2 ){44w = rep(0,2); w[i]=1;45assetCVaR[i] = mESfun2( as.matrix(w) )46}47assetCVaR484950#################################################################################51# Make Exhibit 2 Risk budget paper: weight and CVaR allocation static portfolios52#################################################################################5354indexes = monthlyR5556# Equal-weight portfolio57w5050 <- c(0.5,0.5)58sum( w5050*mu*12 ) ;59#VaR(R=indexes[,1:2], weights=w5050, portfolio_method="component")60ES(R=indexes[,1:2], weights=w5050, portfolio_method="component", mu = mu, sigma = sigma, m3=M3, m4=M4,invert=FALSE)6162# 60/40 bond equity allocation63w6040 <- c(0.6,0.4);64sum( w6040*mu*12 ) ;65#VaR(R=indexes[,1:2], weights=w6040, portfolio_method="component")66ES(R=indexes[,1:2], weights=w6040, portfolio_method="component", mu = mu, sigma = sigma, m3=M3, m4=M4,invert=FALSE)676869# Min CVaR portfolio707172library(DEoptim)73obj <- function(w) {74if (sum(w) == 0) { w <- w + 1e-2 }75w <- w / sum(w)76ES(R=indexes[,1:2],weights = matrix(w,ncol=1), mu = mu, sigma = sigma, m3=M3, m4=M4,invert=FALSE)77}78out <- DEoptim(fn = obj, lower = rep(0, 2), upper = rep(1, 2),79DEoptim.control(itermax=100))80wstar <- out$optim$bestmem81wMinCVaR <- wstar / sum(wstar)82print(wMinCVaR)83# par1 par284#0.96183602 0.0381639885# wMinCVaR = c( 0.96864323 , 0.03135677 )86print(sum(wMinCVaR*mu*12))87ES(R=indexes[,1:2], weights=matrix(wMinCVaR,ncol=1),portfolio_method="component", mu = mu, sigma = sigma, m3=M3, m4=M4)888990# Min CVaR Concentration portfolio9192obj <- function(w) {93if (sum(w) == 0) { w <- w + 1e-2 }94w <- w / sum(w)95CVaR <- ES(R=indexes[,1:2], weights=matrix(w,ncol=1),portfolio_method="component", mu = mu, sigma = sigma, m3=M3, m4=M4)96out <- max(CVaR$contribution)97}98out <- DEoptim(fn = obj, lower = rep(0, 2), upper = rep(1, 2),99DEoptim.control(itermax=100))100wstar <- out$optim$bestmem101wMinCVaRConc <- wstar / sum(wstar)102print(wMinCVaRConc)103#> print(wMinCVaRConc)104# par1 par2105#0.7751015 0.2248985106# wMinCVaRConc = c( 00.7700542 , 0.2299458 )107print(sum(wMinCVaRConc*mu*12))108ES(R=indexes[,1:2], weights=wMinCVaRConc, portfolio_method="component", mu = mu, sigma = sigma, m3=M3, m4=M4)109110111112# 60/40 Risk allocation portfolio113obj <- function(w) {114if (sum(w) == 0) { w <- w + 1e-2 }115w <- w / sum(w)116CVaR <- ES(R=indexes[,1:2], weights=matrix(w,ncol=1),portfolio_method="component", mu = mu, sigma = sigma, m3=M3, m4=M4)117tmp1 <- CVaR$MES118tmp2 <- max(CVaR$pct_contrib_MES - c(0.601,0.401 ) , 0)119tmp3 <- max(c(0.599,0.399 ) - CVaR$pct_contrib_MES , 0)120out <- tmp1 + 1e3 * tmp2 + 1e3 * tmp3121}122out <- DEoptim(fn = obj, lower = rep(0, 2), upper = rep(1, 2),123DEoptim.control(itermax=100))124wstar <- out$optim$bestmem125w6040riskalloc <- wstar / sum(wstar)126print(w6040riskalloc)127# par1 par2128#0.8193828 0.1806172129print(sum(w6040riskalloc*mu*12))130# w6040riskalloc = c( 0.7290461 , 0.2709539 )131ES(R=indexes[,1:2], weights=w6040riskalloc, portfolio_method="component", mu = mu, sigma = sigma, m3=M3, m4=M4)132133134135136137138139140141142143144