I'm trying to calculate the efficient frontier (and the optimal portfolio at the Sharpe ratio) given two vectors for a portfolio: (1) expected returns and (2) historical standard deviations. I would like to be able to calculate this in R. Using the portfolioFrontier()
function of the fPortfolio
package in R, I have successfully calculated the efficient frontier and optimal portfolio allocation at the Sharpe ratio using a time series of historical returns. However, the fPortfolio
package only appears to allow back-testing on a time series. I would like to calculate the efficient frontier and optimal portfolio at the Sharpe ratio for future (i.e., expected) returns. How can I do this?
Ideally this would be implemented with a function in R. The closest resource I could find was from this website using Octave code. I successfully translated the code to R, but the efficient frontier doesn't appear to match (or be as accurate) as the one from the R package.
Here's my attempt in R (translating Octave code from the above website):
expectedReturns <- c(4, 2, 13, 10)
covarianceMatrix <- matrix(c(185, 86.5, 80, 20, 86.5, 196, 76, 13.5, 80, 76, 411, -19, 20, 13.5, -19, 25), nrow=4, ncol=4, byrow=TRUE)
# Calculate Efficient Frontier
unity <- rep(1, length(expectedReturns))
A <- unity %*% solve(covarianceMatrix) %*% unity
B <- unity %*% solve(covarianceMatrix) %*% expectedReturns
C <- expectedReturns %*% solve(covarianceMatrix) %*% expectedReturns
D <- A*C-B^2
mu = seq(0, 30, by=.1)
minVar = ((A*mu^2)-2*B*mu+C)/D
minSD = sqrt(minVar)
plot(minSD, mu)
mvFrontier
, in the devel version of packageNMOF
, does what you want: github.com/enricoschumann/NMOF/blob/master/R/… As for inputs,m
are the expected returns andvar
is the covariance matrix. $\endgroup$