R/EffFrontier.R

EffFrontier <- function (prices,type,bond.min,equity.max,no.fp,fees){

  returns <- apply(prices, 2, PriceToReturn)
  fp <- no.fp            # frontier points
  Anum <- length(colnames(returns))
  ef.w <- array(0,dim=c(Anum,fp)) # storage of optimal allocation

  expVar <- cov.shrink(returns,verbose = FALSE) # covariance matrix
  expSDM <- diag(expVar)
  ExpReturn <- apply(returns,2,mean)
  expR <- rep(0,fp)   # expected return
  expSD <- rep(0,fp)  # standard deviation

  # The risk neutral investor
  tolerance <- 0.999  # 1 is completely risk neutral
  minexp <- 0.000     # minimum expected return
  ef.w[,fp] <- markowitzTargetMU(prices,lambda=tolerance,maxexp=minexp,type,bond.min,equity.max,fees)  # MAD model

  expR[length(expR)] <- c(ef.w[,fp]%*%ExpReturn)  # expected return
  expSD[length(expR)] <- c( t(ef.w[,fp])%*%expVar%*%ef.w[,fp]) # expected risk

  # The risk adverse investor
  tolerance <- 0.001  # 0 is completely risk adverse
  minexp <- 0.000     # minimum exoected return
  ef.w[,1] <- markowitzTargetMU(prices,lambda=tolerance,maxexp=minexp,type,bond.min,equity.max,fees)  # MAD model
  expR[1] <- c(ef.w[,1]%*%ExpReturn)
  expSD[1] <- c(t(ef.w[,1])%*%expVar%*%ef.w[,1])

  # computes the efficient frontier with equal spacings between the points on the exp. return axis
  steps <- (expR[length(expR)] - expR[1])/(fp-1) # frontier steps using the return
  tolerance <- 0.00
  for(i in 2:(fp-1)){

    exptol <- (expR[1] + (i-1)*steps)*220 # minimum expected return
    ef.w[,i] <- markowitzTargetMU(prices,lambda=tolerance,maxexp=exptol,type,bond.min,equity.max,fees)
    expR[i] <- c(ef.w[,i]%*%ExpReturn)
    expSD[i] <- c(t(ef.w[,i])%*%expVar%*%ef.w[,i])
  }

  out <- cbind(t(ef.w),expR*220,sqrt(expSD)*sqrt(220),expR/sqrt(expSD)*sqrt(220))
  colnames(out) <- c(colnames(returns),"Annual.Exp.Return","Annual.Volatility","Sharpe.Ratio")
  return(as.data.frame(out))
}
Bjerring/BEWESO documentation built on May 6, 2019, 7:56 a.m.