Reputation: 177
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.
Upvotes: 2
Views: 503