我PortfolioAnalytics
在 r 中使用并尝试使用预定义的协方差并返回矩阵。
例如,我的资产的估计回报是
returns <- matrix(c(0.316, 0.322, 0.288), ncol = 3)
并且可能的协方差矩阵估计如下:
cov_matrix <- matrix(c(0.240, 0, 0,
0, 0.217, 0,
0, 0, 0.202), ncol = 3, nrow = 3)
我尝试了一些示例,例如在没有 xts 对象的 PortfolioAnalytics 中创建有效前沿和Portfolio Analytics 包中的自定义预期回报,但似乎在这两种情况下仍然提供时间序列的回报并且时刻是仍然是估计的,而我的投资组合的预期收益和协方差矩阵已经给出。
按照示例,我尝试通过以下方式将我的数据强制转换为 xts(假设这是我需要做的):
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)
我不断得到
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
我想我确定我错过了一些东西。