我想使用4时刻最大化方法和R中的PortfolioAnalytics
包优化货币组合
这是我使用的代码:
PF<-data
ARG = xts(PF$ARGENTINA, order.by=as.Date(PF$DATE))
SING = xts(PF$SINGAPOR, order.by=as.Date(PF$DATE))
AFR = xts(PF$SOUTH.AFRICA, order.by=as.Date(PF$DATE))
IND = xts(PF$INDONESIA , order.by=as.Date(PF$DATE))
TAIW = xts(PF$TAIWAN , order.by=as.Date(PF$DATE))
RUSS = xts(PF$RUSSIA , order.by=as.Date(PF$DATE))
CHI = xts(PF$CHINA , order.by=as.Date(PF$DATE))
BRZ = xts(PF$BRAZIL , order.by=as.Date(PF$DATE))
MEX = xts(PF$MEXICO , order.by=as.Date(PF$DATE))
PF=merge(ARG, SING, AFR,IND, TAIW,RUSS,CHI, BRZ, MEX)
args(portfolio.spec)
args(add.constraint)
args(add.objective)
args(optimize.portfolio)
args(optimize.portfolio.rebalancing)
currencies <- colnames(PF)
CRRA <- function(PF, weights, lambda, sigma, m3, m4){
weights <- matrix(weights, ncol=1)
M2.w <- t(weights) %*% sigma %*% weights
M3.w <- t(weights) %*% m3 %*% (weights %x% weights)
M4.w <- t(weights) %*% m4 %*% (weights %x% weights %x% weights)
term1 <- (1 / 2) * lambda * M2.w
term2 <- (1 / 6) * lambda * (lambda + 1) * M3.w
term3 <- (1 / 24) * lambda * (lambda + 1) * (lambda + 2) * M4.w
out <- -term1 + term2 - term3
out
}
portf.crra <- portfolio.spec(currencies)
# Add constraint such that the weights sum to 1
portf.crra <- add.constraint(portf.crra, type="weight_sum",
min_sum=0.99, max_sum=1.01)
# Add box constraint such that no asset can have a weight of greater than
# 40% or less than 1%
portf.crra <- add.constraint(portf.crra, type="box",
min=0, max=0.4)
# Add objective to maximize CRRA
portf.crra <- add.objective(portf.crra, type="return",
name="CRRA", arguments=list(lambda=10))
portf.crra <- add.objective(portf.crra, type="return", name="mean", multiplier=0)
portf.crra <- add.objective(portf.crra, type="risk", name="ES", multiplier=0)
portf.crra <- add.objective(portf.crra, type="risk", name="StdDev", multiplier=0)
opt.crra <- optimize.portfolio(CT, portf.crra, optimize_method="DEoptim", search_size=5000, trace=TRUE, traceDE=0)
问题是当我运行最后一个命令进行优化时,我收到以下错误。
错误:&#34;包:DEOPtim&#34; %search in(%||) requireNamespace(&#34; DEoptim&#34 ;, ....不是真的
如果你能帮我解决这个问题,我将不胜感激