1

I want to optimize a currency portfolio using the 4 moment maximisation method and PortfolioAnalytics package in R. this is the code I use:

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)

The problem is when I run the last command for optimization, I get the following error.

Error: "package:DEoptim" %in% search() || requireNamespace("DEoptim", .... is not TRUE

I would be grateful if you could help me with this matter

4

0 回答 0