2

I'm using PortfolioAnalytics in r and trying to use a predefined covariance and returns matrix.

For example, the estimated returns from my assets are

returns <- matrix(c(0.316, 0.322, 0.288), ncol = 3)

And a possible covariance matrix is estimated as the following:

cov_matrix <- matrix(c(0.240, 0, 0,
                   0, 0.217, 0,
                   0, 0, 0.202), ncol = 3, nrow = 3)

I've tried following a few examples, such as Create efficient frontier in PortfolioAnalytics without an xts object and Custom expected returns in the Portfolio Analytics package, but it seems as if in both cases a time series of returns is still provided and the moments are still estimated, while my portfolio's expected returns and covariance matrix is already given.

Following the examples, I tried coercing my data to xts (assuming that's what I need to do) the following way:

date <- "2020/03/20"
date <- as.Date(date, "%Y/%m/%d")
date
rownames(returns) <- date

returns <- xts(returns, order.by = date)

pf <- portfolio.spec(assets = colnames(returns))
pf <- add.constraint(portfolio = pf, type = "full_investment")
pf <- add.constraint(portfolio = pf, type = "long_only")
pf <- add.objective(portfolio = pf, type = "return", name = "mean")
pf
num_assets <- ncol(returns)
momentargs <- list()
momentargs$mu <- returns
momentargs$sigma <- cov_matrix
momentargs$m3 <- matrix(0, nrow = num_assets, ncol = num_assets ^ 2)
momentargs$m4 <- matrix(0, nrow = num_assets, ncol = num_assets ^ 3)

o <- optimize.portfolio(R = returns, portfolio = pf, momentargs = momentargs)

I keep getting

 Leverage constraint min_sum and max_sum are restrictive, 
              consider relaxing. e.g. 'full_investment' constraint should be min_sum=0.99 and max_sum=1.01
Error in `colnames<-`(`*tmp*`, value = colnames(seed)) : 
  attempt to set 'colnames' on an object with less than two dimensions

I think that I am sure I am missing something.

Amidavid
  • 177
  • 7
  • 1
    It might help if you posted the code you tried. If all else fails, you could create time-series that have the desired properties (mean and variance) and feed them to the functions. See https://stackoverflow.com/questions/60199112/portfolio-optimize-in-r-with-only-a-vector-of-mean-returns-and-covariance-matrix/60202000#60202000 and https://stackoverflow.com/questions/58293991/how-to-use-fportfolio-package-in-r-for-non-time-series-input/58302451#58302451 – Enrico Schumann Mar 22 '20 at 17:27
  • Thank you for your comment, @EnricoSchumann I have added the code I am trying to run. – Amidavid Mar 23 '20 at 06:57
  • @Amidavid did you figure out how to do it? – purpleblau May 25 '20 at 15:45
  • @purpleblau I have simulated a time series with the required metrics – Amidavid May 26 '20 at 13:52
  • @Amidavid can you post a solution to your own question? I am also interested in how this works. Are there any requirement for the return and cov matrix data? – purpleblau May 27 '20 at 20:38
  • 1
    I used something like ```t <- MASS::mvrnorm(50, mu = returns, Sigma = cov_matrix, empirical = TRUE)``` – Amidavid May 29 '20 at 08:30
  • I have the exaxt same problem. When I pass my own cov matrix it appears to be ingnored. – Tartaglia Jun 26 '23 at 18:10

0 Answers0