我正在尝试使用以下代码优化edhec数据,但仅获得NA作为优化权重
library(openxlsx)
library(PortfolioAnalytics)
library(PerformanceAnalytics)
library(plyr)
library(dplyr)
library(reshape2)
library(ROI)
library(ggplot2)
library(plotly)
library(car)
library(quantmod)
library(quadprog)
library(ROI.plugin.symphony)
require(ROI.plugin.glpk)
require(ROI.plugin.quadprog)
library(ROI.plugin.lpsolve)
library(Rglpk)
library(DEoptim)
library(fGarch)
library(pso)
library(GenSA)
library(nloptr)
data(edhec)
returns <- edhec[, 1:4]
colnames(returns) <- c("CA", "CTAG", "DS", "EM")
funds <- colnames(returns)
portf_maxret <- portfolio.spec(assets=funds)
portf_maxret <- add.constraint(portfolio=portf_maxret, type="return", return_target=.0075)
portf_maxret<-add.objective(portfolio=portf_maxret, type="risk", name="StdDev")
opt_maxret <- optimize.portfolio(R=returns, portfolio=portf_maxret,optimize_method="ROI", trace=TRUE)
opt_maxret的输出为
***********************************
PortfolioAnalytics Optimization
***********************************
Call:
optimize.portfolio(R = returns, portfolio = portf_maxret, optimize_method = "ROI",
trace = TRUE)
Optimal Weights:
CA CTAG DS EM
NA NA NA NA
Objective Measure:
StdDev
NA
任何数据都会发生同样的情况,并且只有当我尝试在optimize.portfolio
中设置type="risk"
时才会发生,即尝试根据与之相关的风险优化投资组合。
当您
尝试在以下行中为您的投资组合指定相同的名称时,可能会导致 propblem:
portf_maxret<-add.objective(portfolio=portf_maxret, type="risk", name="StdDev")
尝试将portf_maxret
更改为其他内容,并将其用作优化中的投资组合名称