Как убрать неотрицательное ограничение на переменные с помощью Rsymphony_solve_LP - PullRequest
0 голосов
/ 05 мая 2019

Я нашел этот код для оптимизации портфеля в контексте Mean-CVaR, доступен здесь . Единственная проблема заключается в том, что он не учитывает короткие продажи. Я хотел бы убрать сложную неотрицательность на весах.

Но я никогда не использовал этот пакет, поэтому я не могу понять, как реализовать вариант.

Не могли бы вы мне помочь?

Это функция, формулирующая задачу оптимизации. Пакет 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)
}
...