我发现此代码在Mean-CVaR的上下文中用于投资组合优化,here可用。唯一担心的是,它不允许卖空。我想消除权重上非负的共鸣。
但是我从未使用过该软件包,因此我无法理解如何实现该版本。
你能帮我吗?
这是制定优化问题的函数。 使用Rsymphony软件包。
# mean-CVaR formulation with risk aversion lambda and target CVaR C.
MeanCVaR <- function(scen,mu.hat,lambda,CVaR_target,alpha=0.05){
# number of scenarios
noscen <- length(scen[,1])
###############################################
# LEFT-SIDE MATRIX
# dimensions of A matrix
width <- 3 + length(mu.hat) + noscen
hight <- 4 + noscen
# empty left-side matrix
f.con <- matrix(0, nrow=hight,ncol=width)
# definition of the expected return of the portfolio,
# which is a linear combination of the returns of each asset
f.con[1,1:(length(mu.hat)+1)] <- c(-1,mu.hat)
# Asset weights must sum to 1
f.con[ 2 ,2:(length(mu.hat)+1)] <- rep(1,length(mu.hat))
# CVaR constraint
f.con[3,(length(mu.hat)+2):(3 + length(mu.hat) + noscen)] <-
c(-1,1,rep(1/(noscen*alpha),noscen))
# VaR deviation constraints
f.con[4:(3+noscen),(width-noscen)] <- rep(-1,noscen)
f.con[4:(3+noscen),(width-noscen+1):width] <- -diag(noscen)
f.con[4:(3+noscen),2:(length(mu.hat)+1)] <- -scen
# CVaR_target definition
f.con[hight,(1+length(mu.hat)+1)] <- 1
########################################
# OBJECTIVE FUNCTION
# Lambda formulation for maximizing expected return, and minimizing CVaR
f.obj <- rep(0,width)
f.obj[1] <- lambda
f.obj[(1+length(mu.hat)+1)] <- -1*(1-lambda)
#########################################
# RIGHT HAND SIDE
f.rhs <- c(0,1,0,rep(0,noscen),CVaR_target)
########################################
# logical statements for the relationship between left and right side constraints
f.dir <- c(rep("==",3),rep("<=",(noscen)),"<=")
#########################################
# Upper and lower bounds on each variable
flow.bounds <- c(-Inf,rep(0,length(mu.hat)),-Inf,-Inf,rep(0,noscen))
fup.bounds <- c(Inf,rep(Inf,length(mu.hat)),Inf,Inf,rep(Inf,noscen))
f.bounds <- list(lower=list(ind = (1:length(flow.bounds)),val=flow.bounds),
upper=list(ind = (1:length(fup.bounds)),val=fup.bounds)
)
#########################################
# Variable types
f.types = rep("C",length(f.obj))
#########################################
# SOLVE the system
solution <- Rsymphony_solve_LP(obj=f.obj, mat=f.con, dir=f.dir,
rhs=f.rhs, bounds = f.bounds, types = f.types,max = TRUE)
# values for each parameter
allocation <- solution$solution
return(allocation)
}